Switch Statement C ++ - 错误C2046:非法情况,错误C2043:非法中断

时间:2010-04-15 23:54:35

标签: c++ switch-statement

#include <iostream> 
#include <string> 

using namespace std;

//void multiply(int b);

int main()
{ 
 float total = 0;
 float b = 0;
 cout << "Enter number: " << endl;
 cin >> b;


 char TorD;
 cout << "Would you like to times (*), divide (/), add (+) or minus (-) this number?" << endl;
 cin >> TorD;

 switch (TorD)

  case '*' : 
 {
  int c=0;
  cout << "by how many?" << endl;
  cin >> c;
  total = b * c;
  cout << b << " * " << c << " = " << total << endl;

 }
 break;
  case '/' :
   {
    int c=0;
    cout << "by how many?" << endl;
    cin >> c;
    total = b / c;
    cout << b << " / " << c << " = " << total << endl;

   }
   break;

  case '+' :
   {
    int c=0;
    cout << "by how many?" << endl;
    cin >> c;
    total = b + c;
    cout << b << " + " << c << " = " << total << endl;

   }
   break;

  case '-' :
   {
    int c=0;
    cout << "by how many?" << endl;
    cin >> c;
    total = b - c;
    cout << b << " - " << c << " = " << total << endl;

   }
   break;

  default:

   cout << "You did not correctly enter /, *, +, or - !!" << endl;

   //multiply(b);

   system("pause"); 
   return 0;

}

3 个答案:

答案 0 :(得分:8)

你在switch (TorD)之后错过了开放式大括号,所以'break'超出了任何要断开的语句(即,在一个循环或开关内的断点)它有一些东西可以突破)。 switch语句应如下所示:

switch (TorD) { 
    case '*': {
        // ...
    }
    break;
    case '/': {
       // ...
    }
    break;

    // ...and so on.  
}

答案 1 :(得分:2)

您的开关需要大括号:

switch (...)
{  // your forgot this
    ...
}  // and this

答案 2 :(得分:0)

切换后你忘记了case语句周围的花括号。

相关问题