Click here to Skip to main content
15,305,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to make a quiz game and error i am getting is in if statement compiler is not reading complete string and showing else only if i input complete string. it works for single word but not for string

What I have tried:

C++
#include <iostream>
using namespace std;
#include <string>
int quizshow()
{
 	int a;
 	cout<<"Select a category by pressing its corresponding number"<<endl;
 	cin>>a;
 	switch(a)
 	{
 		case 1:
 			string a;
 			{
 				cout <<"What is a comma used after th";
 				cout <<endl;
				 cin>>a;
 				if (a == "Oxford comma")
 				{				 
 					cout <<"You are winner";
 				}
 				else 
 					cout <<"You are loser";
			}
	}
}

int main()
{
	quizshow();
}
Posted
Updated 30-Dec-21 7:24am
v3
Comments
KarstenK 30-Dec-21 13:12pm
   
fix your indentions to understand the code. You missed some braces in the else.
0x01AA 30-Dec-21 13:16pm
   
Nope, nothing is missing. The code compiles and runs. The only warning is the missing return <int> in main ;)
Try it e.g. on https://www.onlinegdb.com/online_c++_compiler[^]

1 solution

To read the whole line you can use getline
//cin>>a; // Comment out or delete your current input
cin.ignore(10000,'\n');  // 'Empty' input
getline(cin, a);


I hope this helps.
   
Comments
k5054 30-Dec-21 11:30am
   
While its not likely that a single input line is going to be over 10,000 chars long, you can guarantee that you won't end up with in the middle of some ginormously stupid input by using std::numeric_limits<std::streamsize>::max()
0x01AA 30-Dec-21 11:36am
   
Thank you, of course you are right, but I even not explained that line and even more I don't like to overwhelm OP with such things. There are plenty of other problems in OP's code ;)
Hamza Jameel 30-Dec-21 13:29pm
   
Hi, Thanks for answering. it worked perfectly :)
0x01AA 30-Dec-21 13:31pm
   
Thank you for accepting ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900