User Input

I am trying to implement a function orWords(string one, string two) where it searches for a user's words. In main the user must type in two words with an "or" in the middle. For example: "run or fast" I cannot figure out what I'm am doing wrong. It doesn't like the bool operators and I can't find how to fix it.

1
2
3
4
5
6
7
8
9
10
11
    string x, y;
string w = "or";
cout << "What are your words" << endl;
    cin >> x;
    cin >> y;
    cin >> w;

    if (x && w && y)
    {
    orWords(x, y);
    }
Last edited on
if (x && w && y) ¿what are you trying to check there?
You are overwriting the value of 'w' in line 6
Oh, I didn't know I was overwriting "w." Thanks. I've taken out :

cin << w;

but these are the errors I get:

ProjectDS.cpp:208: error: no match for 'operator&&' in 'x && w'
ProjectDS.cpp:208: note: candidates are: operator&&(bool, bool) <built-in>

That's because strings don't have a defined && operator.

If you'd like to concatenate two strings together, you can use the + operator.
If you'd like to compare two strings, you can use the == operator.

^Those are well defined for strings, and if I understand what you'd like to do, they'll work for what you're hoping to do. :)

-Albatross
This doesn't work either:

if (x + w + y)

My program asks the user to type in words to search for in a group of files, so the user will type in something like "run OR fast" or "run AND fast" so I need to create an if/else statement to determine which function to use to either search for both words are in the documents (AND) or to search whether one or the other word is in the documents (OR).
An if statement is checking something... a good example would be

if(x + w + y = 5)

of course x,w,y would be int.

that code( if(x+w+y) ) doesn't make sense unless it's a boolean expression, which it isn't in this case. if you're wondering how to check if it's and or or, try

1
2
3
4
if(w == "and")
//do whatever;
else if(w=="or")
//do something else; 
Last edited on
You might want to make sure to read in w before you read y, so that x is your first word, w is your and/or, and y is your second word. I think I see what you were trying to do with that cin. :)

-Albatross
Topic archived. No new replies allowed.