I'm new to C an programming and I'm trying a piece of code from a book. When I try to build and run it, I get errors and warnings that unable me to run the program. Not sure why. My code is written verbatim. I'm also using codeBlocks for PC.
#include <stdio.h>
int main()
{
char choice;
printf("Are you filing a single, joint or ");
printf("Married return (s, j, m)? ");
do
{
scanf(" %c ", &choice);
switch (choice)
{
case ('s') : printf("You get a $1,000 deduction.\n");
break;
case ('j') : printf("You geta 1 $3,000 deduction.\n");
break;
case ('m') : printf("You geta $5,000 deduction.\n");
break;
default : printf("I don't know the ");
printf("option %c.\n, choice");
printf("Try again.\n");
break;
}
}while ((choice != 's') && (choice != 'j') && (choice != 'm');
return 0;
}
The error is because of the missing )
in While
statement.
Currently it is: while ((choice != 's') && (choice != 'j') && (choice != 'm');
it should be
while ((choice != 's') && (choice != 'j') && (choice != 'm'));
Apart from that you have issues with your scanf
and printf
statements.
Currently they are: scanf(" %c, &choice");
and
printf("option %c.\\n, choice");
These should be changed to : scanf(" %c", &choice);
and
printf("option %c.\\n", choice);
These type of issues can be easily avoided if taken care while writing the code.
You have several syntactical problems.
In your scanf
line, the closing double quote is in the wrong place, it should be after %c
.
scanf(" %c", &choice);
In you while
line, you are missing a closing parenthesis at the end of the line.
} while ((choice != 's') && (choice != 'j') && (choice != 'm'));
Fixing both of these errors causes the program to compile and run fine for me.
In function 'main':
Line 25: error: expected ')' before ';' token
Line 27: error: expected ';' before '}' token
Line 27: error: expected declaration or statement at end of input
First of all, you don't close the braces of your do-while loop. You need to add the braces at the end.
while ((choice != 's') && (choice != 'j') && (choice != 'm'));
Additionally, as other people have mentioned you need to change your scanf statement to
scanf(" %c", &choice);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.