user318095
user318095

Reputation: 1

Switch Statement C++ - error C2046: illegal case, error C2043: illegal break

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

}

Upvotes: 0

Views: 12286

Answers (3)

Jasmeet
Jasmeet

Reputation: 2352

You forgot the curly braces around case statements after switch.

Upvotes: 0

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76601

You need braces for your switch:

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

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490623

You're missing the open brace after the switch (TorD), so the 'break' is outside any statement to break from (i.e. a break has to be inside a loop or switch so it has something to break out of). The switch statement should look like:

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

    // ...and so on.  
}

Upvotes: 8

Related Questions