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

1.6k views Asked by At

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

There are 2 answers

5
Sourav Kanta On BEST ANSWER

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);
        }
7
Jason Ball On

Here is your program in a while loop. I would also recommend not using endl and instead using \n so you dont have to flush the stream so much.

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

using namespace std;
int main()
{
    bool done = false;
    while (!done) {
        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 */
        //====================================//
        cout << "\nWould you like to try again? (y/n)";
        char answer;
        cin >> answer;
        tolower(answer);
        if (answer == 'n') done = true;
    }
    return 0;
}