Click here to Skip to main content
16,016,962 members
Please Sign up or sign in to vote.
1.20/5 (3 votes)
See more:
/* program that uses the switch statement
*/
#include <stdio.h>

int main()
{
char oper;

printf("enter a mathematical operator \n");

oper=getchar();

//use the switch statement
switch(oper)
{
case'+':
{
printf("you entered a plus \n");
break;
}

case'-':
{
printf("you entered a minus \n");
break;
}

case'*':
{
printf("you entered a multiply \n");
break;

}

case'/':
{
printf("you entered a divide \n");
break;
}

default:
{
printf("invalid operator entered \n");
break;
} //end default

} //end switch
return 0;


} //end main


What I have tried:

Dont even know where to start. I'm just so confused.
Posted
Updated 13-Nov-16 11:05am
v2

The error you have noticed is simple: You forgot the semicolon at the end of this line:
C++
char goAgain='y'
Adding it will fix the compilation problem:
C++
char goAgain='y';

But...then you have more problems.
1) You don't read the choice from the user.
2) You break out of the switch regardless of the user selection of "y" or "n".
3) Your condition doesn't check the user PIN is 1234, it assigns it. "=" is assignment, "==" is comparison.
4) You should be using your "goAgain" variable in the loop termination condition.
 
Share this answer
 
Comments
Suvendu Shekhar Giri 12-Nov-16 11:29am    
Ah! I am too missed noticing that :P
My 5!
You have used the variable choice before initializing the value and you have also forgotten to write the default case.

The possible solution could be to accept user input just before the switch statement.

Something like-
C++
//--your other previous stuffs
printf("4. Exit \n");
scanf("%d",choice); //accept choice from user

    switch(choice)
    {
       //---rest of the logic


Hope, it helps :)
 
Share this answer
 
Comments
Suvendu Shekhar Giri 12-Nov-16 11:30am    
Please consider the first solution and if you still get any issue, let us know.
Please ignore my solution for now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900