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.
and here is a sample runCode:#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("%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", £) == 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 } */
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.



LinkBack URL
About LinkBacks


