我该如何重置我的程序? (不使用转到)

时间:2015-06-18 12:49:30

标签: c++ loops reset goto

基本上我已经为我的小弟弟完成了一些桌面测验的编程。我是一个非常新的编程,我不知道如何将我的程序重新设置回到开始。任何人都可以对此有所了解吗?

#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 个答案:

答案 0 :(得分:3)

使用do while循环将代码括起来并将条件设置为您想要的条件。

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

答案 1 :(得分:2)

这是你的程序在while循环中。我还建议不要使用endl,而是使用\ n,这样你就不用这么多刷新流。

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