我怎样才能重置我的程序? (不使用 goto)
How can I reset my program? (without using goto)
基本上我已经为我的孩子完成了一些乘法表测验的编程 brother.I 我对编程还很陌生,我不知道如何将我的程序重新设置为开始。任何人都可以阐明这一点吗?
#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;
}
使用 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);
}
这是你的 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;
}
基本上我已经为我的孩子完成了一些乘法表测验的编程 brother.I 我对编程还很陌生,我不知道如何将我的程序重新设置为开始。任何人都可以阐明这一点吗?
#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;
}
使用 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);
}
这是你的 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;
}