Thread: switch structure question

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question switch structure question

    I was trying to put an if... else statement in a switch structure. But when I compile the program, I get an error to the effect of the "else" missing the "if". Here's the program. Does anyone know my error? And if you can't put if... else statements into a switch, does anyone know how else I could do what I'm trying to accomplish here?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int num1, num2;
    	char operation;
    
    	cout << "Enter two integers: ";
    	cin >> num1 >> num2;
    	cout << endl;
    
    	cout << "Enter operator: + (addition), - (subtraction), "
    		 << "* (multiplication), / (division): ";
    	cin >> operation;
    	cout << endl;
    
    	switch (operation)
    	{
    	case '+': cout << num1 << ' ' << operation << ' '; 
    			  cout << num2 << ' ' << '=' << ' ' << num1 + num2;
    		break;
    	case '-': cout << num1 << ' ' << operation << ' '; 
    			  cout << num2 << ' ' << '=' << ' ' << num1 - num2;
    		break;
    	case '*': cout << num1 << ' ' << operation << ' '; 
    			  cout << num2 << ' ' << '=' << ' ' << num1 * num2;
    		break;
    	case '/': if (num2 = 0)
    				 cout << num1 << ' ' << operation << ' ';
    		         cout << num2 << ' ' << '=' << ' ' << "ERROR";
    				 cout << "Cannot divide by zero" << endl;
    			  else 
    		         cout << num1 << ' ' << operation << ' '; 
    			     cout << num2 << ' ' << '=' << ' ' << num1 / num2;
    		break;
    	default: cout << num1 << ' ' << operation << ' '; 
    		     cout << num2 << ' ' << '=' << ' ' << "Illegal operation";
    	}
    	cout << endl;
    
    	return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The rule is a simple one. For the body of a loop or conditional, you can omit braces if there's only a single statement:
    Code:
    if ( condition )
      cout<<"booga!\n";
    
    for ( int i = 0; i < 10; i++ )
      cout<< i <<'\n';
    But, if there is more than one statement, you must use braces or only the first statement will be treated as the body:
    Code:
    for ( int i = 0; i < 10; i++ )
      cout<<"i is: ";
      cout<< i <<'\n';
    The above loop will print:
    Code:
    i is: i is: i is: i is: i is: i is: i is: i is: i is: i is: 10
    Surrounding the loop body in braces fixes the problem:
    Code:
    for ( int i = 0; i < 10; i++ ) {
      cout<<"i is: ";
      cout<< i <<'\n';
    }
    Thus printing:
    Code:
    i is: 0
    i is: 1
    i is: 2
    i is: 3
    i is: 4
    i is: 5
    i is: 6
    i is: 7
    i is: 8
    i is: 9
    Your problem is that the else keyword expects the body of an if statement immediately before it. With accurate formatting, this is your conditional:
    Code:
    if (num2 = 0)
      cout << num1 << ' ' << operation << ' ';
    cout << num2 << ' ' << '=' << ' ' << "ERROR";
    cout << "Cannot divide by zero" << endl;
    else 
      cout << num1 << ' ' << operation << ' '; 
    cout << num2 << ' ' << '=' << ' ' << num1 / num2;
    Because there are two calls to cout's << operator between the if body and the else keyword, the else has no if to pair with, and the compiler flags an error. The solution is to use braces:
    Code:
    if (num2 = 0) {
      cout << num1 << ' ' << operation << ' ';
      cout << num2 << ' ' << '=' << ' ' << "ERROR";
      cout << "Cannot divide by zero" << endl;
    }
    else {
      cout << num1 << ' ' << operation << ' '; 
      cout << num2 << ' ' << '=' << ' ' << num1 / num2;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Now the program is running, but when i type " 4 0" and the operation "/".... the program shuts down. Try it... and let me know if you know what to do to fix it.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int num1, num2;
    	char operation;
    
    	cout << "Enter two integers: ";
    	cin >> num1 >> num2;
    	cout << endl;
    
    	cout << "Enter operator: + (addition), - (subtraction), "
    		 << "* (multiplication), / (division): ";
    	cin >> operation;
    	cout << endl;
    
    	switch (operation)
    	{
    	case '+': cout << num1 << ' ' << operation << ' '; 
    			  cout << num2 << ' ' << '=' << ' ' << num1 + num2;
    		break;
    	case '-': cout << num1 << ' ' << operation << ' '; 
    			  cout << num2 << ' ' << '=' << ' ' << num1 - num2;
    		break;
    	case '*': cout << num1 << ' ' << operation << ' '; 
    			  cout << num2 << ' ' << '=' << ' ' << num1 * num2;
    		break;
    	case '/': if (num2 = 0) {
    				 cout << num1 << ' ' << operation << ' ';
    		         cout << num2 << ' ' << '=' << ' ' << "ERROR";
    				 cout << "Cannot divide by zero" << endl;
    			  }
    		      else { 
    		         cout << num1 << ' ' << operation << ' '; 
    			     cout << num2 << ' ' << '=' << ' ' << num1 / num2;
                  }
    		break;
    	default: cout << num1 << ' ' << operation << ' '; 
    		     cout << num2 << ' ' << '=' << ' ' << "Illegal operation";
    	}
    	cout << endl;
    
    	return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if (num2 = 0)
    My bad, I forgot to mention that = is assignment and == is comparison. Here you're assigning 0 to num2, not comparing it against 0. The long and short of it is that you end up doing the division, and division by zero causes your program to shut down in this case.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thank you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  3. i need alil help with my switch question
    By datainjector in forum C Programming
    Replies: 17
    Last Post: 06-29-2002, 08:14 PM
  4. Light switch question
    By C_Coder in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-05-2001, 07:15 PM