Jun 18, 2012 at 11:28pm Jun 18, 2012 at 11:28pm UTC
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 18, 2012 at 11:48pm UTC
Jun 19, 2012 at 12:05am Jun 19, 2012 at 12:05am UTC
You could use a getline function
cin.getline();
That would be my best guess.
Jun 19, 2012 at 12:06am Jun 19, 2012 at 12:06am UTC
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 Jun 19, 2012 at 12:07am UTC
Thank you so much!
Now I'll just try to figure out what the diffrence is.
Jun 19, 2012 at 12:24am Jun 19, 2012 at 12:24am UTC
getline's delimiter is an optional third argument that defaults to '\n'
Last edited on Jun 19, 2012 at 12:24am Jun 19, 2012 at 12:24am UTC