Ivanism
Ivanism

Reputation: 11

A simple C switch statement program error. I cant build and run it. I don't know why

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

Upvotes: 0

Views: 2087

Answers (3)

heretolearn
heretolearn

Reputation: 6545

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.

Upvotes: 4

Adrian Jandl
Adrian Jandl

Reputation: 3015

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

http://codepad.org/tXK1DlsJ

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

Upvotes: 0

Bad Wolf
Bad Wolf

Reputation: 8349

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.

Upvotes: 3

Related Questions