【问题标题】:How do I resolve this error: jump to case label crosses initialization [duplicate]如何解决此错误:跳转到案例标签交叉初始化[重复]
【发布时间】:2014-05-12 01:18:20
【问题描述】:

我的计算器代码中有以下错误,不知道如何更正。请任何建议都会有所帮助。

错误: 错误:跳转到案例标签 [-fpermissive]| 错误:跨过“int sum”的初始化| 错误:未在此范围内声明“退出”|

代码:

#include <iostream>
#include <cmath>
using namespace std;         
void display_menu(); 
int get_menu_choice();
void get_two_numbers(int &a, int &b);
int add(int a, int b);
int subtract(int a, int b);


int main()
 {
 int choice;

  do
   {
    display_menu();
    choice = get_menu_choice();
    int x, y;
    switch (choice)
    {
        case 1: get_two_numbers(x, y);
                int sum = add(x, y);
                cout << x << " + " << y << " = " <<  sum << endl;
                break;
        case 2: get_two_numbers(x, y);
                int diff = subtract(x, y);
                cout << x << " - " << y << " = " <<  diff << endl;
                break;
        default:;
    }

     } while (choice != 3);

     cout << "Good bye...now." << endl;

     return 0;
       }


 void display_menu()
  {
   cout << endl;
   cout << "Simple Calculator Menu" << endl;
   cout << "----------------------" << endl;
   cout << " 1. Addition (+) " << endl;
   cout << " 2. Subtraction (-) " << endl;
   cout << " 3. Quit to exit the program" << endl;
   cout << endl;
  }

 int get_menu_choice()
  {
   int choice;
   cout << "Enter your selection (1, 2, or 3): ";
   cin >> choice;

  while(((choice < 1) || (choice > 3)) && (!cin.fail()))
   {
    cout << "Try again (1, 2, or 3): ";
    cin >> choice;
    }
  if (cin.fail())
    {
      cout << "Error: exiting now ... " << endl;
      exit(1);
     }
   return choice;
    }

 void get_two_numbers(int &a, int &b)
  {
    cout << "Enter two integer numbers: ";
    cin >> a >> b;
  }


 int add(int a, int b)
  {
   return (a + b);
  }

 int subtract(int a, int b)
  {
    return (a - b);
  }

【问题讨论】:

  • 只需更改 int sum = add(x, y);整数;总和=加(x.y);这一定是有史以来最糟糕的警告。

标签: c++ variables


【解决方案1】:

您在 case 语句中声明新变量而不创建封闭范围:

switch (choice)
{
    case 1: get_two_numbers(x, y);
            //* vv here vv *
            int sum = add(x, y);
            //* ^^ here ^^ */
            cout << x << " + " << y << " = " <<  sum << endl;
            break;
    case 2: get_two_numbers(x, y);
            //* vv here vv */
            int diff = subtract(x, y);
            //* ^^ here ^^ */
            cout << x << " - " << y << " = " <<  diff << endl;
            break;
    default:;
}

解决方法如下:

switch (choice)
{
    case 1:
        {
            get_two_numbers(x, y);
            int sum = add(x, y);
            cout << x << " + " << y << " = " <<  sum << endl;
        }
        break;
    case 2:
        {
            get_two_numbers(x, y);
            int diff = subtract(x, y);
            cout << x << " - " << y << " = " <<  diff << endl;
        }
        break;
    default:
        break;
}

当然,括号和缩进的确切格式取决于您。

【讨论】:

  • 把break语句放在花括号里面会不会有问题?
  • @VishalSharma 将 break 放在大括号内很好,但可能会导致可读性问题/错误,因为可以保证存在一个无条件的 break 来防止“失败”到另一个案例。这样解读:case 2: { do some things }; whatever happens stop now; default: ...
  • 抱歉,我不明白它到底什么时候会导致错误?
  • 这样想,break 有三个版本:case-break、for-break 和 while-break。那么你认为这段代码的输出会是什么:gcc.godbolt.org/z/11v9do
  • 如果循环中存在循环以及 switch 案例,我认为中断将从 switch/while/for 循环堆栈的最顶层帧执行。你的示例似乎如下这也是如此,我仍然想念你的意思。
【解决方案2】:

开关的“案例”不会创建范围,因此,正如错误所说,如果选择不是 1,您将跳过“sum”的初始化。

您要么需要在 switch 之外声明 sum 和 diff,要么为每种情况创建带有 { } 的块。

【讨论】:

    【解决方案3】:

    您应该在 switch 语句之外而不是在 case 中声明变量。特别是 sumdiff 在您的代码中存在问题。一旦这些变量在 switch 语句之外初始化,您就可以设置它们的值。

    【讨论】:

    • 因为sumdiff 仅在各自的情况下使用,所以最好在case 内添加范围,如评价最高的答案中所述。一般来说,最好将变量范围限制在尽可能小的范围内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2012-05-10
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多