Help with quiz game

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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
    string answer;
    cout <<"Welcome to Quiz Game\n\n\n\n";
    cout <<"What is this game called?\n";
    cin >> answer;
    if (answer == "Quiz Game")
    {
        cout<<"That's correct!\n";

    }
    else
    {
        cout<<"That's incorrect!\n";

    }


    system("pause");
    return 0;

}



My problem is that I can't get the correct answer because there is a space in "Quiz Game" whatever I write it goes to else instead of if.
It works when I don't have a space but what do I do to make it work with the space?

Thanks
Last edited on
You could use a getline function
cin.getline();

That would be my best guess.
You will need to use:
getline(cin, answer);

This will store everything into answer until you press enter.

Note: That is supposed to replace the cin >> answer; statement.
Thank you so much!
Now I'll just try to figure out what the diffrence is.
getline does exactly as it says, it waits until you end the line, or press enter.

The way cin works is that it stops "recording", if you will, once a space or return is detected.

Here is some reference for the getline function:
http://www.cplusplus.com/reference/string/getline/
getline's delimiter is an optional third argument that defaults to '\n'
Last edited on
Topic archived. No new replies allowed.