This is a part of my quiz program in which i ask the user to input question and 4 options and the correct option after which in new funtion i ask the quiz. but although it is asking me "option-1", its skipping the "Ques-"

int quizquestion()
      {
                  
                   cout<<"\nQues-    ";
                   cin.getline(stmt,100); 
                   
                   cout<<"Option-1 ";
                   cin.getline(opt1,30);  
                   
                   cout<<"Option-2 ";
                   cin.getline(opt2,30); 
                   
                   cout<<"Option-3 ";
                   cin.getline(opt3,30); 
                   
                   cout<<"Option-4 ";
                   cin.getline(opt4,30); 
                   
                   cout<<"Correct Option number:";
                   cin>>correct;
                   cin.ignore(numeric_limits<streamsize>::max(),'\n');
                   c++;
                   return c;
                   
      }

Edited 5 Years Ago by Narue: Added code tags

although it is asking me "option-1", its skipping the "Ques-"

My crystal ball says that the caller of quizquestion() is doing input using the >> operator, which leaves a newline in the stream for cin.getline(stmt,100) to terminate on immediately. Try placing a cin.ignore() at the beginning of quizquestion(); I'm willing to bet that will solve the problem.

Edited 5 Years Ago by Narue: n/a

Thank you friends! it worked as you said "bet that will solve the problem!!!"

OK, now try to see what you just did as a Band-Aid rather than a true solution. The problem didn't go away, you simply hid it by extracting the remaining junk from the stream. The correct solution is not to have junk in the stream to begin with.

This article has been dead for over six months. Start a new discussion instead.