Thread: switch_case_not_working :S

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    29

    Question switch_case_not_working :S

    hi, i want to make a calculator. ive done it before but now after some years i come back to c++ and it doesnt work
    here my code

    Code:
    #include <iostream.h>
    
    int main()
    {
    	char option[1];
    	cout<<"Welcome to PicoCalc ;0)"<<'\n';
    	cout<<"Choose your function: +,-,* or /"<<'\n';
    
    	cin>>option;
    	switch(option);
    	{
    	case '+':
    		cout<<"Enter first number:"<<'\n';
    		int num1;
    		cin>>num1;
    		cout<<"Enter second number:"<<'\n';
    		int num2;
    		cout<<"Result is "<<num1+num2<<'\n';
    		break;
    	case '-':
    		cout<<"Enter first number:"<<'\n';
    		int num1;
    		cin>>num1;
    		cout<<"Enter second number:"<<'\n';
    		int num2;
    		cout<<"Result is "<<num1-num2<<'\n';
    		break;
    	case '*':
    		cout<<"Enter first number:"<<'\n';
    		int num1;
    		cin>>num1;
    		cout<<"Enter second number:"<<'\n';
    		int num2;
    		cout<<"Result is "<<num1*num2<<'\n';
    		break;
    	case '/':
    		cout<<"Enter first number:"<<'\n';
    		int num1;
    		cin>>num1;
    		cout<<"Enter second number:"<<'\n';
    		int num2;
    		cout<<"Result is "<<num1/num2<<'\n';
    		break;
    	default:
    		cout<<"Invalid choise!"<<'\n';
    	}
    
    	cout<<"test";			//here i will put option to make another calculation..
    
    	return (0);
    }
    i compile using visual c++ and get following errors:

    Code:
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(14) : error C2450: switch expression of type 'char [1]' is illegal
            Integral expression required
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(14) : warning C4060: switch statement contains no 'case' or 'default' labels
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(16) : error C2046: illegal case
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(23) : error C2043: illegal break
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(24) : error C2046: illegal case
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(26) : error C2086: 'num1' : redefinition
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(29) : error C2086: 'num2' : redefinition
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(31) : error C2043: illegal break
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(32) : error C2046: illegal case
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(34) : error C2086: 'num1' : redefinition
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(37) : error C2086: 'num2' : redefinition
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(39) : error C2043: illegal break
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(40) : error C2046: illegal case
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(42) : error C2086: 'num1' : redefinition
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(45) : error C2086: 'num2' : redefinition
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(47) : error C2043: illegal break
    C:\Documents and Settings\user\Desktop\example4\example4.cpp(48) : error C2047: illegal default
    Error executing cl.exe.
    
    example4.exe - 16 error(s), 1 warning(s)
    any ideas?
    cheers

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    ok , lets start cleaning up this mess :

    Code:
    #include <iostream.h>
    Although its not an error , but you're following the old C++ standard. The new way to do this is either :
    Code:
    #include <iostream>
    using namespace std;
    or better :
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    The second option is recommended to avoid naming conflicts , not a biggie though.


    Code:
    char option[1];
    If you're going to store one character only , then why didn't you just declare a regular character instead of a one-character-array?

    This messed up the switch statement because 'option' has the memory adress of the first element of your array , whereas 'option[0]' has the first value (remember when referring to elements you start counting from 0).

    Your switch statement took a memory adress instead of a value , therefore none of the cases matched it. But wait !! did the switch statement even work? no. Becuase you terminated it right away with a semi-colon here :

    Code:
    switch(option);
    	{
    if you remove that semi-colon you'll get rid of some of the errors. If you still insist on using s single character array though , just replace the input with 'cin >> option[0];' and use 'option[0]' within the switch statement.



    This error :
    'num1' : redefinition
    showed up because you defined 'num1' more than once. You should've defined num1 and num2 before the switch statement then use them within the cases.


    another error here :
    Code:
    cout<<"Enter second number:"<<'\n';
    		int num2;
    Seems like you typed 'int' instead of 'cin >>' by mistake. But you repeated that with the rest of the cases.

    fix those errors and your program should work again.


    Here's a big hint : read what the error(s) say
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    hi thx ur reply mate.
    i read wat the errors said, but just "illegal case/break" etc didnt give me any tip. obviously there was a problem with the switch statement, i'll remove the colon and it should be fine

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You're defining variables inside the case statement. You can't in general do that.
    For instance, if you have

    Code:
    switch(a) {
    case 5:
         int c;
    case 6:
         int d;
         c = 5;
    }
    Whether c = 5 in case 6 makes "sense" is whether case 5 dropped into case 6. But C++ determines scope statically. If you really need to define variables within the cases, you can do

    Code:
    switch(a) {
    case 4: 
    {
          int c;
    }
    
    }
    But this, looks ugly. Your're better off either defining the variables outside of the switch statement or creating a new function.

Popular pages Recent additions subscribe to a feed