Variable help

Hello everyone. I need help with a variable.Here is my code. For some reason the string variable isn't working when i type the correct answer. I think it's because the answer contains two words, but i don't know how to make it work. If someone knows, pls tell me. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>

using namespace std;

int main()
{
    string answer1;


    cout << "Hi There! This is a Movie Quiz #1. Hope you enjoy it. Let's get started!" << endl;
    cout<<"We all know the movie called 'Friday The 13th' aren't we?Tell me the first and the last name of the killer in this movie"<<endl;
    cin>>answer1;
    if (answer1=="Pamela Voorhees")
        cout<<"True! Jason was ruled by his mother, so she is the real killer!"<<endl;
    else
    {
        cout<<"False! Jason was ruled by his mother, so this make her the real killer in the movie!"<<endl;
    return 1;
    }
    cout<<"Next question!"<<endl;
    return 0;
}
Last edited on
Try printing answer1 after line 12.

cin>> is delimited by whitespace, so it only captures the first word.
To get both words (the whole line, with spaces), use getline

1
2
string answer1;
getline(cin, answer1);
Probably i am making some kind of mistake, but i got the same result. Here is how i changed the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
    cout << "Hi There! This is a Movie Quiz #1. Hope you enjoy it. Let's get started!" << endl;
    cout<<"We all know the movie called 'Friday The 13th' aren't we?Tell me the first and the last name of the killer in this movie"<<endl;
    string answer1;
    cin>>answer1;
    getline(cin, answer1);
    if (answer1=="Pamela Voorhees")
        cout<<"True! Jason was ruled by his mother, so she is the real killer!"<<endl;
    else
    {
        cout<<"False! Jason was ruled by his mother, so this make her the real killer in the movie!"<<endl;
    return 1;
    }
    cout<<"Next question!"<<endl;
    return 0;
}
Remove line 10. Use getline instead of cin>>.
Last edited on
Worked. Thanks a lot! Appreciate it.
Topic archived. No new replies allowed.