Help with quiz game

Jun 18, 2012 at 11:28pm
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 Jun 18, 2012 at 11:48pm
Jun 19, 2012 at 12:05am
You could use a getline function
cin.getline();

That would be my best guess.
Jun 19, 2012 at 12:06am
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.
Jun 19, 2012 at 12:07am
Thank you so much!
Now I'll just try to figure out what the diffrence is.
Jun 19, 2012 at 12:12am
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/
Jun 19, 2012 at 12:24am
getline's delimiter is an optional third argument that defaults to '\n'
Last edited on Jun 19, 2012 at 12:24am
Topic archived. No new replies allowed.