I am trying to make a questionnaire program. It works the first time but when I try to make it repeat the process a second time it completely skips a line of code.
1 2 3 4 5 6 7
if (restart == 1)
{
cout << "This is a quiz to see how well somebody knows you. . .
cout << "Enter quiz topic.\n";
getline (cin, topic); //this is where it skips the second time
cout << "Enter question 1\nDon't forget the `?'\n1. ";
getline (cin, q1);
This is the output the first time.
Enter quiz topic.
TOPIC_
Enter question 1
Don't forget the `?'
1. _
The second time.
Enter quiz topic.
Enter question 1
Don't forget the `?'
1. _
can you print more code? You're code looks fine. I don't see any problems with it...
if it's long code, who cares... it actually throws you off when the keywords are comment color..
This is the first part of the code that I've already shown.
Don't bother scrolling to the end. It's just useless cout text.
1 2 3 4 5 6 7 8 9 10
while (restart < 3)
{
correct = 0;
if (restart == 1)
{
cout << "This is a quiz to see how well somebody knows you.\nEnter 10 questions with four possible answers to each.\nThen, enter the letter choice of the correct answers.\nWhen you're done, have someone else try to answer the questions.\nYou're questions and answers are deleted when you quit this program\n\n";
cout << "Enter quiz topic.\n"; //This is the part it skips
getline (cin, topic);
cout << "Enter question 1.\nDon't forget the `?'\n1. ";
getline (cin, q1);
This is where it goes back to the first part of the code.
1 2 3 4
cout << "You are " << correct / 10 * 100 << "% correct\n\n";
cout << "Enter 1 to restart\nEnter 2 to retry the same quiz\nEnter 3 to quit\n";
cin >> restart;
}
I'm surprised no one has told you this after your first post.
The problem is that you are mixing formatted and unformatted input. If you must do that, make sure to fix things after using cin >> foo statements using a cin.ignore() statement.