Loop and Case statements

This is a discussion on Loop and Case statements within the C Programming forums, part of the General Programming Boards category; I have been working on a simple c program using miracle C as my compiler. I have been able to ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Question Loop and Case statements

    I have been working on a simple c program using miracle C as my compiler. I have been able to get to this point:
    Code:
    //// Brian Sirrine
    //// POS 370
    
    #include <stdio.h>
    int main(void)
    {
    //// Define sales tax for locations
    float DELMAR = .0725;
    float ENCINITAS = .0750;
    float LAJOLLA = .0775;
    
    
    //// Define purchase price
    float PURCHASE = 0.00;
    //// Define a restart point
    Start:
    printf("***********************************************************************\n") ;
    printf("            Kudler Fine Foods Purchase Price w/tax\n");
    printf("***********************************************************************\n\n") ;
    printf("Please type the purchase price and then press enter.\n\n");
    scanf("%f", &PURCHASE);
    
    //// Start of if statements
    	if(PURCHASE<=0.0) 
    	{
    		printf("Please enter a number greater than Zero\n");
    		//// This will redirect the program back to the Start:
    		goto Start;
    	}
    	 else
    	{
    		////if (typeof(PURCHASE) == typeof(int))
    		{
    			//// Display to the user the tax calculations from all three locations
    
    			printf("***********************************************************************\n") ;
    			printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    			printf("Delmar's Tax   -----> +	$%.2f\n" , (DELMAR*PURCHASE) );
    			printf("			=======\n") ;
    			printf("Total With Tax ----->	$%.2f\n" , (DELMAR*PURCHASE)+PURCHASE );
    			printf("***********************************************************************\n") ;
    	
    	
    	
    			printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    			printf("Encinita's Tax -----> +	$%.2f\n" , (ENCINITAS*PURCHASE) );
    			printf("			=======\n") ;
    			printf("Total With Tax ----->	$%.2f\n" , (ENCINITAS*PURCHASE)+PURCHASE );
    			printf("***********************************************************************\n") ;
    	
    
    	
    			printf("\n") ;
    			printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    			printf("Lajolla's Tax  -----> +	$%.2f\n" , (LAJOLLA*PURCHASE) );
    			printf("			=======\n") ;
    			printf("Total With Tax ----->	$%.2f\n" , (LAJOLLA*PURCHASE)+PURCHASE );
    			printf("***********************************************************************\n\n\n") ;
    		}
    	}
    
    		printf("Press any key to quit") ;
    		
    
    getchar();
    return 0 ;
    }
    However, now I am trying to get a loop with a case statement for a menu prompt and this is where I stumble. Here is the code so far:
    Code:
    //// Brian Sirrine
    //// POS 370
    
    #include <stdio.h>
    int main(void)
    {
    //// Define sales tax for locations
    float DELMAR = .0725;
    float ENCINITAS = .0750;
    float LAJOLLA = .0775;
    
    
    //// Define purchase price
    float PURCHASE = 0.00;
    //// Define a restart point
    Start:
    printf("***********************************************************************\n") ;
    printf("            Kudler Fine Foods Purchase Price w/tax\n");
    printf("***********************************************************************\n\n") ;
    printf("Please type the purchase price and then press enter.\n\n");
    printf("Press Q to End Program \n\n") ;
    scanf("%f", &PURCHASE);
    
    //// Start of Do While statement
    if	(PURCHASE <=0.0)
    	{
    	pintf("enter a number Greater than 0 \n") ;
    	goto Start;
    	}
    		else 
    		{
    		if	(PURCHASE >=0)
    	
    		////if (typeof(PURCHASE) == typeof(int))
    		{
    		char menu;
    		printf("A: --> Delmar \n") ;
    		printf("B: --> Encinitas \n") ;
    		printf("C: --> LAJOLLA \n") ;
    		printf("Choose a location. A, B, or C? \n");
    		scanf(" %c", &menu);
    		menu = toupper(menu);
    	
    		/* case */
    		switch (menu)
    			{
    				case 'A':	printf("You have selected A \n");
    						printf("***********************************************************************\n") ;
    						printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    						printf("Delmar's Tax   -----> +	$%.2f\n" , (DELMAR*PURCHASE) );
    						printf("			=======\n") ;
    						printf("Total With Tax ----->	$%.2f\n" , (DELMAR*PURCHASE)+PURCHASE );
    						printf("***********************************************************************\n") ;
    				break;
    				case 'B': 	printf("You have selected B \n");
    						printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    						printf("Encinita's Tax -----> +	$%.2f\n" , (ENCINITAS*PURCHASE) );
    						printf("			=======\n") ;
    						printf("Total With Tax ----->	$%.2f\n" , (ENCINITAS*PURCHASE)+PURCHASE );
    						printf("***********************************************************************\n") ;
    				break;
    				case 'C': 	printf("You have selected C \n");
    						printf("\n") ;
    						printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    						printf("Lajolla's Tax  -----> +	$%.2f\n" , (LAJOLLA*PURCHASE) );
    						printf("			=======\n") ;
    						printf("Total With Tax ----->	$%.2f\n" , (LAJOLLA*PURCHASE)+PURCHASE );
    						printf("***********************************************************************\n\n\n") ;
    				break;
    				default: Printf("Invalid selection \n");
    			}
    			/* end case */
    		}
    	}
    			}
    	 else
    
    		printf("Press any key to quit") ;
    		
    
    getchar();
    return 0 ;
    }
    I cant figure out where I am messing up. Any help would be great. Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Typing
    Code:
    //// Start of Do While statement
    does not actually insert a do while statement into your program.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Attack of the University of Phoenix assignments!

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    12
    Code:
    else 
    		{
    		if	(PURCHASE >=0)
    ...
    }
    You probably want an "else if" there. And you have an extra close brace "}" also.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Changed it up a bit

    Ok, figured it out. This is where I stand, but I can use a few suggestions for what I would like to do.

    I would like to have it loop back to the prompt for a price, however quit if you enter Q. If a letter is entered for the price, you get a nasty loop.
    How can I avoid the two goto statements that I have?

    This the code to point.

    Code:
    //// Brian Sirrine
    //// POS 370
    
    #include <stdio.h>
    int main(void)
    {
    //// Define sales tax for locations
    float DELMAR = .0725;
    float ENCINITAS = .0750;
    float LAJOLLA = .0775;
    
    
    //// Define purchase price
    float PURCHASE = 0.00;
    //// Define a restart point
    Start:
    printf("***********************************************************************\n") ;
    printf("            Kudler Fine Foods Purchase Price w/tax\n");
    printf("***********************************************************************\n\n") ;
    printf("Please type the purchase price and then press enter.\n\n");
    printf("Press Q to End Program \n\n") ;
    scanf("%f", &PURCHASE);
    
    //// Start of Do While statement
    	while	(PURCHASE <=0.00)
    	{
    	printf("enter a number Greater than 0 \n") ;
    	goto Start;
    
    	}
    	////while	(PURCHASE >0.00)
    		////{
    		Prompt:
    		{
    		char (menu);
    		printf("A: --> Delmar \n") ;
    		printf("B: --> Encinitas \n") ;
    		printf("C: --> LAJOLLA \n") ;
    		printf("Choose a location. A, B, or C? \n");
    		scanf(" %c", &menu);
    		menu = toupper(menu);
    	
    		/* case */
    		switch (menu)
    			{
    				case 'A':	printf("You have selected A \n");
    						printf("***********************************************************************\n") ;
    						printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    						printf("Delmar's Tax   -----> +	$%.2f\n" , (DELMAR*PURCHASE) );
    						printf("			=======\n") ;
    						printf("Total With Tax ----->	$%.2f\n" , (DELMAR*PURCHASE)+PURCHASE );
    						printf("***********************************************************************\n") ;
    				break;
    				case 'B': 	printf("You have selected B \n");
    						printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    						printf("Encinita's Tax -----> +	$%.2f\n" , (ENCINITAS*PURCHASE) );
    						printf("			=======\n") ;
    						printf("Total With Tax ----->	$%.2f\n" , (ENCINITAS*PURCHASE)+PURCHASE );
    						printf("***********************************************************************\n") ;
    				break;
    				case 'C': 	printf("You have selected C \n");
    						printf("\n") ;
    						printf("Purchase Price ----->	$%.2f\n" , (PURCHASE) ); 
    						printf("Lajolla's Tax  -----> +	$%.2f\n" , (LAJOLLA*PURCHASE) );
    						printf("			=======\n") ;
    						printf("Total With Tax ----->	$%.2f\n" , (LAJOLLA*PURCHASE)+PURCHASE );
    						printf("***********************************************************************\n\n\n") ;
    				break;
    				default: printf("Invalid selection \n");
    				goto Prompt;
    			}
    			/* end case */
    			getchar();
    		}
    	////}
    
    		printf("Press any key to quit") ;
    	
    
    getchar();
    return 0 ;
    }
    Thanks everyone.
    Last edited by Nexcompac; 06-25-2008 at 11:45 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    //// Define purchase price
    float PURCHASE = 0.00;
    //// Define a restart point
    Start:
    printf("***********************************************************************\n") ;
    printf("            Kudler Fine Foods Purchase Price w/tax\n");
    printf("***********************************************************************\n\n") ;
    printf("Please type the purchase price and then press enter.\n\n");
    printf("Press Q to End Program \n\n") ;
    scanf("%f", &PURCHASE);
    
    //// Start of Do While statement
    	while	(PURCHASE <=0.00)
    	{
    	printf("enter a number Greater than 0 \n") ;
    	goto Start;
    
    	}
    Why are you using while AND goto here? If you MUST use goto, use an if to make the printf conditional.

    I personally would put a do-while around the whole block above, and make that loop end if the purchase is > 0.0f (you still need an if-statement), and remove the goto and label.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    What you do from here is get rid of all gotos and start learning loops and upgrade your compiler to a better, more standards compliant one.
    Oh and if you're using void main, don't! Use int main.
    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. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 08:42 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 08:27 PM
  5. Help me in while loop with case menu
    By playboy1620 in forum C Programming
    Replies: 1
    Last Post: 02-20-2002, 10:07 PM

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