我应该在这里使用goto声明吗?

时间:2013-03-01 15:17:59

标签: c++ goto

一位优秀的绅士告诉我,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陈述道歉。

7 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:4)

嗯,使用正确的循环结构,whilefor等。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

您可以轻松避免代码中的“转到”。只需将其划分为函数:

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实际上是一个更好的选择。

答案 4 :(得分:0)

  • goto使得难以追踪执行的来源以及执行的位置。

  • goto鼓励使用spagetti代码,除非你严格限制使用它(例如,你可能会认为你只是将它用于清理块,但是这样的论证在RAII存在时没有任何意义)。

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

  • 它模糊不清,因此使您的代码对其他人不太可用。

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

答案 5 :(得分: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':

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

使代码更清晰 - 对a是否更改值以及另一个if语句是否可行也没有疑惑。

答案 6 :(得分: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;
}
相关问题