如何解决此错误:跳转到案例标签跨越初始化

use*_*274 31 c++ variables

我的计算机代码中存在以下错误,并且不了解如何更正它.请提出任何建议.

错误:错误:跳转到案例标签[-fpermissive] | 错误:越过'int sum'|的初始化 错误:在此范围中未声明'exit'

码:

#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);
  }
Run Code Online (Sandbox Code Playgroud)

kfs*_*one 59

您在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:;
}
Run Code Online (Sandbox Code Playgroud)

以下是修复方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

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


And*_*ess 20

交换机的"大小写"不会创建范围,因此,如错误所示,如果选项不是1,则跳过"sum"的初始化.

您需要在交换机外部声明sum和diff,或者为每种情况创建带{}的块.


Nas*_*sim 5

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

  • 因为“sum”和“diff”仅在各自的情况下使用,所以最好在“case”内添加一个范围,如最受好评的答案中所述。一般来说,最好将变量范围限制在尽可能小的范围。 (4认同)