# Thread: confused (switch help)

1. ## confused (switch help)

Ok so I'm refreshing my C by going through an old Primer book before my upcoming classes this fall and I'm having trouble.

The book is asking me to make a program that let's you choose what item your ordering, enter the pounds, then when you're done entering the various weights it will print out the totals, shipping, and what you ordered. Most of this is easy and I can handle all of the computations and such but for some reason my switch isn't working right.

It seems like the breaks aren't working right or something. For some reason the overlying while loop wants to read the input given for the order function. I've been tweaking it and got it to appear to work right when you first choose an item and then enter the weight, but it breaks down to multiple reading again after that. I'm not even sure quite how my changes made it work better, and am even more confused that it only works once.

Thanks for your help, and if this isn't the right forum please let me know of one I can go to for advice.

Code:
```#include <stdio.h>
#define ARTS 1.25
#define BEETS 0.65
#define CARROTS 0.89
float order(float inp);
void ship(float a, float b, float c);
int main(void)
{
char ch;
float pa =0, pb =0, pc =0;

printf("Enter your selection.\n");
printf("a) Artichokes \t b) Beets\n");
printf("c) Carrots \t q) quit\n");

//	while((ch = getchar()) != 'q')
while(scanf("&#37;ch", &ch) == 1 && ch != 'q' && ch != 'Q')
//	if(scanf("%ch", &ch) == 1)
{
switch(ch)
{
case 'A':
case 'a':	pa = order(pa);
break;
case 'B':
case 'b':	pb = order(pb);
break;
case 'C':
case 'c':	pc = order(pc);
break;
case 'Q':
case 'q':
break;
default:
printf("Invalid input\n");
}
printf("Anything else?\n");
}
printf("Here is your order");
//	ship(pa, pb, pc);
printf("\n%.2f %.2f %.2f\n", pa, pb, pc);

return 0;
}

float order(float inp)
{
float pound;

printf("Please enter how many pounds you would like\n");

if(scanf("%f", &pound) == 1)
pound += inp;
else
printf("Please enter only numbers\n");

return(pound);
}
/*
void ship(float a, float b, float c)
{
float total =0;
float tw;

tw = a+b+c;
// still finishing this part obviously
}
*/```
and here is a sample run
Code:
```[Session started at 2007-07-15 13:25:40 -0400.]
Enter your selection.
a) Artichokes 	 b) Beets
c) Carrots 	 q) quit
a
Please enter how many pounds you would like
12
Anything else?
b
Invalid input
Anything else?
Please enter how many pounds you would like
12
Anything else?

Invalid input
Anything else?
q
Invalid input
Anything else?
Here is your order
12.00 12.00 0.00

Prime has exited with status 0.```

2. Code:
`while(scanf("%ch", &ch)`
do you really want to scan a char followed by an 'h' ?
guess you want
Code:
`while(scanf("%c", &ch)`
Kurt

3. ah true thanks, though as you can see with the // I originally was using a while loop with getchar(), and even tried an if statement. Of course far as I know I'll have to stick with While so it keeps going until the user quits.

even with that fix here's another sample run

Code:
```[Session started at 2007-07-15 14:19:15 -0400.]
Enter your selection.
a) Artichokes 	 b) Beets
c) Carrots 	 q) quit
a
Please enter how many pounds you would like
12
Anything else?
Invalid input
Anything else?
b
Please enter how many pounds you would like
12
Anything else?
Invalid input
Anything else?
12
Invalid input
Anything else?
Invalid input
Anything else?
Invalid input
Anything else?
q
Here is your order
12.00 12.00 0.00

Prime has exited with status 0.```

4. Sorry I cannot reproduce your problem.
Runs as expected for me ( even without the "fix" ).
Kurt

EDIT: How are you executing that program ? Could it be that you run it via a remote connection ?

5. just compiling it in xcode (os X)

6. &#37;c format leaves the white space in the input stream
so - because you press <Enter> after you enter your char - it is read on the next iteration

7. > if(scanf("&#37;f", &pound) == 1)
The scanf %c which follows this will read the '\n', which will go through the switch/case as unrecognised input.

8. I think flushing the your problem was that you wernt flushing the new lines in the input buffer. When I teseted you code it screwed up after the first loop, I made a small change and now it works for me (in bold).
Code:
```#include <stdio.h>
#define ARTS 1.25
#define BEETS 0.65
#define CARROTS 0.89
float order(float inp);
void ship(float a, float b, float c);
int main(void)
{
char ch;
float pa =0, pb =0, pc =0;

printf("Enter your selection.\n");
printf("a) Artichokes \t b) Beets\n");
printf("c) Carrots \t q) quit\n");

while(scanf("%c", &ch) == 1 && ch != 'q' && ch != 'Q')
{
switch(ch)
{
case 'A':
case 'a':	pa = order(pa);
break;
case 'B':
case 'b':	pb = order(pb);
break;
case 'C':
case 'c':	pc = order(pc);
break;
case 'Q':
case 'q':
break;
default:
printf("Invalid input\n");
}
printf("Anything else?\n");
while ((ch = getchar()) != '\n');
}
printf("Here is your order");
//	ship(pa, pb, pc);
printf("\n%.2f %.2f %.2f\n", pa, pb, pc);

return 0;
}

float order(float inp)
{
float pound;

printf("Please enter how many pounds you would like\n");

if(scanf("%f", &pound) == 1)
pound += inp;
else
printf("Please enter only numbers\n");

return(pound);
}```

9. Just noticed that the program behaves like yours if I remove the 'h' from the format.
With the 'h' it "works ".
Kurt

10. thanks guys, and especially on the little addition. The book had a similar line in an example program but they use it to force it to only read the first character (i.e. someone inputs "dab" instead of "d") so I didn't think to use it. I forgot about the whitespace problem.

Thanks again guys, hopefully I can get the shipping function working without any problems.

Popular pages Recent additions