Strings and Ifs

Basicaly i have a string with text like "who are you". After using cin to get a users input, i use an if statment to see if the input and a string match. when i put the words together like this "whoareyou" it works, but otherwise it fails.

heres my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
  Name: Project Josh
  Copyright: 2011
  Author: Widget360
*/

#include <iostream>
#include <string>
using namespace std;

int Name()
{
    char name[20];
    cin >> name;
    string a = "Hello";
    string b = "hello";
    string c = "Is anybody there";
    string d = "is anybody there";
    if (name == a || name == b || name == c || name == d) {
             cout<<"yes\n";
             }
}
    
int Talka()
{
    char Talka[30];
    cin >> Talka;
    string a = "who is this";
    string b = "who are you";
    string c = "whats your name?";
    if (Talka == a || Talka == b || Talka == c) {
              cout <<"josh\n";
              }
    else {
         cout <<" error \n";
         }
}
int main()
{
    Name();
    Talka();
    cin.get();
    return 0;    
}


how do i fix it... Also, I have cin.get() after the talka(), so why does the program just close if it fails?
Oh, also.... Is there a better way to define the accepted texts other than making strings?
Use std::getline to get the user input.
ahh... thnx.... I got it working

Now, was there a better way to make these strings?
You could just type the strings after the comparison operation instead of the string identifier. Also, encase each comparison in parentheses.
I assume you mean like this:
string a = (("Hello") , ("hello"));

Is there a limit, or are there multiple ways to form this, pherhaps a vertical way?

ps. Ive never done anything like this, im learning by trial and error right now..
even better... can i make it where "There is" and "there is" will be the same.
No, I was talking about these lines:

1
2
3
4
5
6
string a = "Hello";
string b = "hello";
string c = "Is anybody there";
string d = "is anybody there";
if (name == a || name == b || name == c || name == d) {
    cout<<"yes\n";


Do something like this instead:

 
if ((name == "Hello")||(name == "hello")||(name == "Is anybody there")||("is anybody there"))


To condense your code a little bit.

Also, instead of having two comparison for the capital at the beginning of the sentence, just convert the entire user string to upper or lower and compare it to an upper/lower string.

Also, what you proposed isn't valid syntax.
Last edited on
yea, i found that out after trying it...
Heres the new code, why isn't it working? It wont print the result...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

/*
  Name: Project Josh
  Copyright: 2011
  Author: Widget360
*/

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int Name()
{
    char name[20];
    cin.getline(name,20);
    if ((name == "hello")||("is anybody there")||("wake up")||(" ")) {
              cout<<"yes\n";
              }
}
    
int talk()
{ 
    int End_Chat = 1;
    char talk[20];
    while (End_Chat = 1) {
          cin.getline(talk,20);
          if ((talk == "whats going on")||(talk == "whats up")||(talk == "what you been up to")||(talk == "hey buddy")) {
                    cout<<"oh, nothing much realy\n"<<"how about you\n";
                    }
          if ((talk == "who are you")||(talk == "whats your name")) {
                    cout<<"I am everything\n";
                    }
          if ((talk  == "are you god")||(talk == "are you jesus")||(talk == "god")||(talk == "are you buddah")) {
                     cout<<"God is dead\n"<<"I shot him\n";
                     }
          }
          
}

int test()
{
    char Talkb[20];
    cin.getline(Talkb,20);
    string g = "yo yo";
    string z = "y y";
    string i = (("meme"),("momo"));
    if (Talkb == g || Talkb == z || Talkb == i) {
              cout<<"O";
              }
}
int main()
{
    Name();
    talk();
    test();
    cin.get();
    return 0;    
}


and yes, i know my while loop is infinite...
EDIT: You're comparing a char array to a string. Change talk to a string.
Last edited on
Topic archived. No new replies allowed.