Repeating...going back

Hello,
I am very new to C++ and I need some help. My problem is probably simple for you, but for me...it's quite a challenge and I can't seem to find a solution...
So to explain my situation:

I want to program a question/answer quiz. If the person's answer is right they move on, and if the person's answer is wrong, they have a choice to try again.

And that's when I run into problems. How can I program it that when I ask if they want to try again and they say yes, that it goes back to the original question?? I don't want to write the same question again all the time.

Your help is greatly appreciated!

Thanks
Use a function to ask the question. Pass the question, answers, and correct answer as arguments. The function can give the user a number of tries and return when done with a score. Then all you have to do is call the function once for each question and tally all the results.

Hope this helps.
Last edited on
Hey,

I appreciate your reply and help...except I don't really understand. I am very new to C++, so I am really confused. Could you explain it a bit differently? Could you maybe give me some sort of example, if you can?


Thanks
This is slightly different to Duoas's suggestion (I'm not passing in the answers) but the principle is the same

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
bool ask_question(int q_num, int answer)
{
    int users_answer = 0;
    bool right_wrong = false;

    switch (q_num)
    {
        case 1:
	   // ask question 1 & and show answers
	   break;
	case 2:
	   // ask question 2 & show answers
	   break;
	default:
	   // Bad question number
   }

    //get users answer

   if (answer == users_answer)
   {
       right_wrong = true;
   }

   return right_wrong;
}


int main()
{
  int max_tries = 3;
  int num_tries = 0;
  bool right_answer = false;
  int q_num = 1;
  int answer = 2;

  while (num_tries < max_tries  && !right_answer)
  {
      right_answer = ask_question(q_num, answer);

      if (!right_answer)
      {
		  num_tries++;
      }
      else
      {
           // setup q_num and answer for next question
           // update any variables used to track progress
           num_tries = 0;
      }
}
 
  return 0; 
}
Hello,

thanks for all your help. I finally figured it out. It works :)

I used a totally different way. I changed some of my stuff so all I had to use was a simple goto function.

Thanks again!
Last edited on
Aaaiiiiieeeeeeeeeeeeeee!
XD Duoas! @G05 FYI, gotos are considered bad practice and should be avoided whenever possible.
Topic archived. No new replies allowed.