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;
                   
      }

Recommended Answers

All 4 Replies

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.

Yup I agree. Copy line 21 and place it in line 3

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.