Thread: While Statement Issue.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    While Statement Issue.

    In the below code I have put together a text example of a shopping cart whereby the user selects a commodity to purchase and based upon a menu selection, tells the program which leg in my switch statement to execute. After a selection is made, the program drops out of the switch statement and proceeds to the bottom of the while statement where "ordercom" tells the while statement to continue taking orders. This is obviously an un-ending loop but is coded this way to trouble shoot the issue at hand. I am testing the code with code breaks. What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement.
    Can someone help me with this issue?

    Code:
    #include <stdio.h>
    #define Artichoke 1.25
    #define Beets .65
    #define Carrots .89
    #define Discount .05
    #define Shipping1 10.00
    #define Shipping2 8.00
    
    int main(void)
    	{
    	char ordercom;
    	char type;
    	float pounds;
    	float gross;
    	float poundTot = 0;
    	ordercom = 'Y';
    	while(ordercom == 'Y')
    		{
    		printf("*******************************************************************************************************************\n");
    		printf("Determine What You Want To Buy\n");
    		printf("1) (a)\t	Artichokes\t		3) (c)\t   Carrots\n");
    		printf("2) (b)\t	Beets\t			4) (q)\t   Quit\n");
    		printf("*******************************************************************************************************************\n");
    		printf("\n");
    		printf("Select The Type Of Vegetable You Want And The Pounds Of The Vegetable You Want Or (q) To Quit :");
    		scanf("%c ""%f",&type,&pounds);
    		
    		switch(type)
    			{
    			case 'a' :
    				{
    				gross = pounds * Artichoke;
    				break;
    				}
    			case 'b' :
    				{
    				gross = pounds * Beets;
    				break;
    				}
    			case 'c' :
    				{
    				gross = pounds * Carrots;
    				break;
    				}
    			case 'q' :
    				{
    				break;
    				}
    			default  :
    				{
    				break;
    				}
    			}
    		ordercom = 'Y';
    		}
    	
    	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this supposed to be C or C++?

    Quote Originally Posted by mcertini
    What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement.
    On the second iteration, the %c probably matches the newline resulting from the enter for the previous input pair. You can avoid this by adding a space before the %c.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Corrected Version.

    Laserlight,

    Thank you for your reply. The following includes corrections:
    Code:
    #include <stdio.h>
    #include <conio.h> 
    
    #define Artichoke 1.25
    #define Beets .65
    #define Carrots .89
    #define Discount .05
    #define Shipping1 10.00
    #define Shipping2 8.00
    
    int main(void)
    	{
    	char ordercom;
    	char type;
    	float pounds;
    	float gross;
    	float poundTot = 0;
    	ordercom = 'Y';
    	while(ordercom == 'Y')
    		{
    		printf("*******************************************************************************************************************\n");
    		printf("Determine What You Want To Buy\n");
    		printf("1) (a)\t	Artichokes\t		3) (c)\t   Carrots\n");
    		printf("2) (b)\t	Beets\t				4) (q)\t   Quit\n");
    		printf("*******************************************************************************************************************\n");
    		printf("\n");
    		printf("Select The Type Of Vegetable You Want:\n");
    		scanf("%c",&type);
    		fflush(stdin);
    		printf("Select The Amount Of Pounds You Want To Purchase:\n");
    		scanf("%f",&pounds);
    		fflush(stdin);
    		switch(type)
    			{
    			case 'a' :
    				{
    				gross = pounds * Artichoke;
    				ordercom = 'Y';
    				break;
    				}
    			case 'b' :
    				{
    				gross = pounds * Beets;
    				ordercom = 'Y';
    				break;
    				}
    			case 'c' :
    				{
    				gross = pounds * Carrots;
    				ordercom = 'Y';
    				break;
    				}
    			case 'q' :
    				{
    				ordercom = 'N';
    				break;
    				}
    			default  :
    				{
    				ordercom = 'N';
    				break;
    				}
    			}
    		}
    	
    	}

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    fflush(stdin);
    FAIL. fflush(stdin) results in undefined behavior.
    Cprogramming.com FAQ > Why fflush(stdin) is wrong
    Cprogramming.com FAQ > Flush the input buffer

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Is this supposed to be C or C++?
    This. You have failed to answer this one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    27
    Elysia,

    It was put together in C++.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It was put together in C.
    If printf, scanf and #define is all you know, then you need to get a C++ book instead of a C book.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  2. Can repeated characters be printed with a C statement?
    By Pete_2000 in forum C Programming
    Replies: 4
    Last Post: 05-05-2006, 07:47 PM
  3. If statement....
    By TheProphecy in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2005, 02:24 PM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. embedding variable inside sql statement
    By WaterNut in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2004, 02:43 PM