0

一位好先生告诉我 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言论道歉。

4

7 回答 7

8

do这是一个使用/while循环结构的清理和正确格式的版本:

using namespace std;

int main()
{   
    int x, y, z, a, b;

    do {
        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);
        }
        else if (a == 2) {
            z = sub(x, y);
        }
        else if (a == 3) {
            z = mul(x, y);
        }
        else 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;
    } while (b != 0);
    cout << "Happy trails!" << endl;
    return 0;
}
于 2013-03-01T15:22:48.037 回答
4

Erm ,使用适当的循环结构whilefor等等。

于 2013-03-01T15:20:29.273 回答
1

在这种情况下,“更普遍接受”的方法是do {......} while(b==1);但编译的结果可能是相同的。

于 2013-03-01T15:23:15.247 回答
0

goto不会自动坏。不可读的代码很糟糕。每当你发现自己需要一些像“goto”这样晦涩难懂的编程结构时,这通常意味着你的代码要么写得不好,要么你的程序设计有缺陷。

解决方案几乎总是更多功能。例如:

bool run_program();
int  prompt_user_begin();
int  prompt_user_again();
int  prompt_operation_type();
bool prompt_continue();


int main()
{
  while(run_program())
  {}

  cout << "Happy trails!" << endl;
  return 0; 
}


bool run_program()
{
  int first;
  int second;
  int operation_type;
  int result;

  first  = prompt_user_begin();
  cout << "Excellent!" << endl;

  second = prompt_user_again();
  cout << "Excellent!" << endl;

  operation_type = prompt_operation_type();

  switch(operation_type)
  {
    case 1: result = add(first, second); break;
    case 2: result = sub(first, second); break;
    case 3: result = mul(first, second); break;
    case 4: result = div(first, second); break;
  }

  cout << "The answer to your math question is ";
  cout << result << endl;

  return prompt_continue();
}

int prompt_user_begin ()
{
  int x;
  cout << "To begin, type a number" << endl;
  cin >> x;

  return x;
}

int prompt_user_again ()
{
  int x;
  cout << "Now you need to type the second number" << endl;
  cin >> x;
  return x;
}

int prompt_operation_type ()
{
  int x;
  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 >> x;
  return x;
}

bool prompt_continue ()
{
  int x;
  cout << "Do you want to enter another question?" << endl;
  cout << "Type 1 for yes" << endl;
  cout << "Type 0 for no" << endl;
  cin >> x;
  return x==1;
}
于 2013-03-01T15:44:02.843 回答
0

对实际问题的简短回答:不,您不应goto在此代码中使用。没有必要。

的使用goto应该是“当它使代码更清晰或更安全时”。“使代码更清晰”的典型例子是当有多层嵌套循环时,并且某些特殊情况需要离开所有嵌套层级,并且添加“我们是否要退出循环”会使代码更加复杂。“让它更安全”的一个例子是如果一个函数持有一个锁,打开一个文件或类似的东西,并且需要提前返回 - 但你还需要关闭文件或释放锁,使用“goto exit_now;” 比试图记住持有哪些锁、文件等然后执行return;.

这个:

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

是您应该使用“开关”的经典案例:

switch(a)
{
   case 1:
     z = add(x, y);
     break;
   case 2:
     z = sub(x, y);
     break;
  ....
}

使代码更清晰 - 也不会混淆a更改值是否可行,并且可能另一个if语句变得可行。

于 2013-03-01T15:31:34.963 回答
0

您可以在代码中轻松避免“goto”。只需将其划分为功能:

using namespace std;

void question () {
  cout << "To begin, type a number" << endl;
  cin >> x;

  // put rest of the code here
}

int main () {
  int ask = 1;
  while ( ask == 1 ) {
    question();
    cout << "Do you want to enter another question?" << endl;
    cout << "Type 1 for yes" << endl;
    cout << "Type 0 for no" << endl;
    cin >> ask;
  }

  return 0;
}

编辑:如评论中所述,使用 do-while 实际上是一个更好的选择。

于 2013-03-01T15:22:51.020 回答
0
  • goto使得很难跟踪执行的来源和去向。

  • goto鼓励 spagetti 代码,除非您严格限制使用它的位置(例如,您可以争辩说您只将其用于清理块,但在存在 RAII 的情况下这样的论点毫无意义)。

  • 您正在使用 goto 来模拟循环。你为什么不写一个循环呢?

  • 它晦涩难懂,因此使其他人无法使用您的代码。

  • goto使得跟踪对象的生命周期变得更加困难。

于 2013-03-01T15:27:38.587 回答