confused (switch help)

This is a discussion on confused (switch help) within the C Programming forums, part of the General Programming Boards category; Ok so I'm refreshing my C by going through an old Primer book before my upcoming classes this fall and ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    39

    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. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    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. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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 ?
    Last edited by ZuK; 07-15-2007 at 12:39 PM.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    just compiling it in xcode (os X)

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    &#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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Just noticed that the program behaves like yours if I remove the 'h' from the format.
    With the 'h' it "works ".
    Kurt

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    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 subscribe to a feed

Similar Threads

  1. function switch help
    By etbjr182 in forum C Programming
    Replies: 9
    Last Post: 02-20-2009, 02:51 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. unwanted number in switch
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2004, 05:43 AM
  4. Can a switch have poor quality?
    By Xei in forum Tech Board
    Replies: 1
    Last Post: 10-27-2003, 05:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21