Word Variables with If-Else Statements

Hello,

Complete beginner here, really enjoying learning to code. Currently trying to fix up a fun program for my dad for Father's Day. I have already written an introduction of sorts with a few multiple choice questions; however, I am having trouble writing a "free response" question of sorts.

Basically, what I want to do is ask a question like "Who is my favorite actor?" Then have him input an answer choice, eliciting a statement depending on whether he answers right or wrong.

Something like this:

"Who is my favorite actor?"

if Tom Hanks cout << "Nice job!"
else cout << "That's too bad..."

Despite hours of messing with it and searching for an answer, my newbness cannot get it right. I see that I can't 'cin' a two word answer with integers, so I tried getline (), but I am not sure how to if-else with that. So if you could help, that'd be awesome!

I know there are many things wrong with this particular segment of code. But this was my base, and every direction I try to go from here fails as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include<windows.h>
    #include <iostream>
    #include <string>

using namespace std;

string actorstr;
cout << "\n\n\t 1. Who is my favorite actor?";

     getline (cin, actorstr);

        if (actorstr == Tom Hanks) cout << "Nice job!";
        else cout << "Ooo, wrong again...";


Sidenote: I have already been using the cin command with variables and getline () earlier in the program, and I heard mixing the two isn't preferable, and when I try to use getline () again, it doesn't even show on output.

Sorry for the lengthy post! I appreciate any input!
You managed to use the correct syntax at least three times in the above code, for example "Nice job!". You just need to do the same with Tom Hanks, i.e. put "Tom Hanks" in quotes.

Also, you don't seem to have a main() function, as in the "hello world" example:
http://www.cplusplus.com/doc/tutorial/program_structure/

main() is always required, it's how the program knows where to begin executing when it is run.

As for mixing cin >> with getline(), there are plenty of occasions when you will need to do that. The answer is to clear the trailing newline character from the input buffer after using cin >>.
example:
1
2
3
4
5
    int age;
    string name;
    cin >> age;
    cin.ignore(1000, '\n');   // ignore up to 1000 character, or until a newline is found
    getline(cin, name);
Last edited on
closed account (j3Rz8vqX)
Also remember to use cin.clear() to remove error flags associated with cin.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
    int data=0, total=0;
    do
    {
        while(cout<<"Enter the data (-1 to exit): " && cin>>data && data!=-1)
            total+=data;
        cin.clear();
        cin.ignore(255,'\n');
        if(data!=-1)
            cout<<"Error, non numeric. Try again.\n";
    }while(data!=-1);
    cout<<"Total: "<<total<<"\n\nPress <enter> to exit console: ";
    cin.get();
    return 0;
}
Enter the data (-1 to exit): 2
Enter the data (-1 to exit): 5
Enter the data (-1 to exit): 6
Enter the data (-1 to exit): d
Error, non numeric. Try again.
Enter the data (-1 to exit): 3
Enter the data (-1 to exit): -2
Enter the data (-1 to exit): -1
Total: 14

Press <enter> to exit console:

If not, when it fails on a character entry, it'll produce the below behavior:
1
2
3
4
5
6
7
//Same code as above with cin.clear() commented.
Enter the data (-1 to exit): d
Error, non numeric. Try again.
Enter the data (-1 to exit): Error, non numeric. Try again.
Enter the data (-1 to exit): Error, non numeric. Try again.
Enter the data (-1 to exit): Error, non numeric. Try again.
Enter the data (-1 to exit): Error, non numeric. Try again.
//...Repeated infinitely.
Thanks so much guys, I got it working. I had been using main (), just forgot to add it when copying down the code. The quotes around Tom Hanks, the cin.clear() and cin.ignore() got it working. I didn't know how that all worked exactly. Final code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<windows.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
//after a good bit of code
cin.clear();
cin.ignore(255,'\n');

string actor;
cout << "\n\n 1. Who is my favorite actor?\n\n";
cout << "Answer:"; getline (cin, actor);
    if (actor == "Tom Hanks") cout << "\nYes! He's just great isn't he?\n\n";
    else cout << "Nope! Tom Hanks!";
system("pause");
system("cls");
}


Topic archived. No new replies allowed.