Search code examples
c++loopsresetgoto

How can I reset my program? (without using goto)


Basically I have finished programming a little times tables quiz for my little brother.I am very new to programming and I have no idea how to reset my program back to the start. Can anyone shed some light on this?

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{
 int sum ;  
 int question = 0;
 int Uanswer ;
 int score = 0;
 int *score_pointer = &score;
//============================================================//
 cout<<"= Hello Welcome to Your Times Tables   ="<<endl;
 cout<<"= Please Select A Table To Learn Below ="<<endl;
 cout<<"========================================"<<endl;       
    cout<<"####################################"<<endl;          /* THE MENU */
    cout<<"####|1|2|3|4|5|6|7|8|9|10|11|12|####"<<endl;
    cout<<"####################################"<<endl;
//============================================================//
    cout<<">>:";
    cin>> sum;
    cout<<"You Selected "<< sum <<endl;                        /*User Input For Table */
    cout<<"Time to Learn Brain Power Go!"<<endl;                                     
//============================================================//
    while (question  <12){
           question = question +1;
           cout<< question;
           cout<< "x";
           cout<< sum ;
           cout<< "=:";
           cin>> Uanswer;
           int Aanswer = (sum * question);

    if (Aanswer == Uanswer){
      cout<<"Correct: Well done Ben :)"<<endl;
       *score_pointer = *score_pointer+1; 
    } else {
        cout<<"Incorrect: Are you even trying? :("<<endl;
    } 
         }

   //====================================//
      cout<<"Well done You scored " <<  score << " Out of 12"<<endl; /*Tally of total score */
   //====================================//

    return 0;
}

Solution

  • Use a do while loop to enclose your code and set the condition to what you want.

      #include <iostream>
      #include <string>
      #include <fstream>
    
      using namespace std;
      int main()
          {
           int flag=1;
           do
             {
    
              //your code that needs to be repeated 
              //when you need to exit set flag=0 
    
              } while(flag);
            }