繁体   English   中英

我应该在这里使用goto语句吗?

[英]Should I use goto statement here?

一位先生告诉我,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声明,我深表歉意。

这是使用结构的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;
}

呃,使用适当的循环结构, whilefor

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

您可以轻松避免在代码中使用“ 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实际上是一个更好的选择。

  • goto使得跟踪执行源和执行源变得困难。

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

  • 您正在使用goto模拟循环。 为什么不编写循环呢?

  • 它晦涩难懂,因此使您的代码对其他人的可用性降低。

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

实际问题的简短答案:不,您不应在此代码中使用goto 不需要它。

goto的使用应“在使代码更清晰或更安全时”。 “使代码更清晰”的典型示例是当存在多层嵌套循环时,并且某些特殊情况需要保留所有嵌套级别,并添加“我们要退出循环”会使代码更加复杂。 “使它更安全”的一个例子是,如果一个函数持有一个锁,打开一个文件或类似的东西,并且需要提早返回-但是您还需要使用“ goto exit_now;”关闭文件或释放锁。 比尝试记住持有什么锁,文件等然后return;还可以更安全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”:

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

使代码更清晰-也没有关于是否混淆a变化值,也许另一if声明变得可行。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM