C ++如何让程序在到达终点后重新启动?
C ++ How to make the program restart once you've reached the end?
我已经编程了大约一个星期左右,但我无法弄清楚如何在完成后用 C++ 重新启动程序。有人能帮我吗?
我写了这个初学者字母等级程序,它根据你的分数告诉你,你得到了什么字母等级。但正如我所说,一旦你到达终点并得到答案,程序就会像预期的那样结束,但我希望它重新启动并让我输入另一个数字。谢谢!
#include <iostream>
using namespace std;
int main()
{
long int grade;
string x = " ";
cout << "Please, enter your grade. (0-100)" << endl;
cin >> grade;
int P =grade == 100;
int A =grade >= 90;
int B =grade >= 80;
int C =grade >= 70;
int D =grade >= 60;
int F =grade >= 0;
if(P){cout << "You have a perfect score." << endl << endl;}else{
if(A){cout <<"You have an A." << endl<< endl; }else{
if(B){cout <<"You have a B." << endl << endl;}else{
if(C){cout <<"You have a C." << endl;}else{
if(D){cout <<"You have a D." << endl<< endl;}else{
if(F){cout <<"You have an F."<< endl<< endl;}else{
cout << "Invalid input.";
}
}
}
}
}
}
cout << "Enter x to finish the program" << endl << endl;
cin >> x;
}
将整个代码放在一个 do while 循环中,直到用户输入 0 停止程序。
像这样:
int input = 0;
do
{
//You need to put your entire code here!
cin >> input;
}while(input != 0)
我已经编程了大约一个星期左右,但我无法弄清楚如何在完成后用 C++ 重新启动程序。有人能帮我吗?
我写了这个初学者字母等级程序,它根据你的分数告诉你,你得到了什么字母等级。但正如我所说,一旦你到达终点并得到答案,程序就会像预期的那样结束,但我希望它重新启动并让我输入另一个数字。谢谢!
#include <iostream>
using namespace std;
int main()
{
long int grade;
string x = " ";
cout << "Please, enter your grade. (0-100)" << endl;
cin >> grade;
int P =grade == 100;
int A =grade >= 90;
int B =grade >= 80;
int C =grade >= 70;
int D =grade >= 60;
int F =grade >= 0;
if(P){cout << "You have a perfect score." << endl << endl;}else{
if(A){cout <<"You have an A." << endl<< endl; }else{
if(B){cout <<"You have a B." << endl << endl;}else{
if(C){cout <<"You have a C." << endl;}else{
if(D){cout <<"You have a D." << endl<< endl;}else{
if(F){cout <<"You have an F."<< endl<< endl;}else{
cout << "Invalid input.";
}
}
}
}
}
}
cout << "Enter x to finish the program" << endl << endl;
cin >> x;
}
将整个代码放在一个 do while 循环中,直到用户输入 0 停止程序。
像这样:
int input = 0;
do
{
//You need to put your entire code here!
cin >> input;
}while(input != 0)