一位好先生告诉我 goto 语句不好,但我不明白我怎么不能在这里使用它:
int main()
{
using namespace std;
int x;
int y;
int z;
int a;
int b;
Calc: //How can i get back here, without using goto?
{
cout << "To begin, type a number" << endl;
cin >> x;
cout << "Excellent!" << endl;
cout << "Now you need to type the second number" << endl;
cin >> y;
cout << "Excellent!" << endl;
cout << "Now, what do you want to do with these numbers?" << endl;
cout << "Alt. 1 +" << endl;
cout << "Alt. 2 -" << endl;
cout << "Alt. 3 *" << endl;
cout << "Alt. 4 /" << endl;
cin >> a;
if (a == 1) {
z = add(x, y);
}
if (a == 2) {
z = sub(x, y);
}
if (a == 3) {
z = mul(x, y);
}
if (a == 4) {
z = dis(x, y);
}
}
cout << "The answer to your math question is ";
cout << z << endl;
cout << "Do you want to enter another question?" << endl;
cout << "Type 1 for yes" << endl;
cout << "Type 0 for no" << endl;
cin >> b;
if (b == 1) {
goto Calc;
}
cout << "Happy trails!" << endl;
return 0;
}
如您所见,它是一个计算器。另外,如果您愿意,您能否建议一种更好的方法(如果存在)让用户选择操作(+ - * /)。头文件在控制之下。我为很多cout
言论道歉。