Thread: Why isn't this looping?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    Why isn't this looping?

    Hey guys

    I got the this program to do what I want but it doesn't loop until 'Q' is entered. I am just not understanding this, can someone tell me why this doesn't loop?

    Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    // Declare variables
    
    float sales = 0.0;
    float trateDelmar = .0725;
    float trateEncinitas = .0750;
    float trateLajolla = .0775;
    float staxDelmar = 0.0;
    float totamtDelmar = 0.0;
    float staxEncinitas = 0.0;
    float totamtEncinitas = 0.0;
    float staxLajolla = 0.0;
    float totamtLajolla = 0.0;
    char input[10];
    char selection;
    
    	// Program header
    	printf("\nName:\t\t Brandon McHenry\n");
    	printf("Class:\t\t POS 370\n");
    	printf("Assignment:\t Workshop 5\n");
    	printf("--------------------------------\n");
    	printf("\nTax calculation program for Kudler stores\n");
    	printf("\nEnter Purchase Amount: ");
    	
    	// Read the input and assign it to sales
    	scanf("%s", input);
    	sales = atof(input);
    			
    	if(sales <= 0.0)
    		{
    			printf("Invalid input\n");
                    	return 1;
            	}
            
            // Calculating the sales tax and total amount
            staxDelmar = trateDelmar * sales;
            totamtDelmar = staxDelmar + sales;
            staxEncinitas = trateEncinitas * sales;
            totamtEncinitas = staxEncinitas + sales;
            staxLajolla = trateLajolla * sales;
            totamtLajolla = staxLajolla + sales;
            
            // Displaying the store selection menu 
            // Loop until 'Q' is selected
            do
            {             	
    	printf("\n           Store Selection Menu\n");
    	printf("\t--------------------------\n");
    	printf("\n\tD) Del Mar\n"); 
    	printf("\tE) Encinitas\n"); 
    	printf("\tL) LaJolla\n"); 
    	printf("\tQ) Quit\n");
    	printf("\n\tSelect D, E, L, or Q :");
    	
    	// Read the selection input and display the selected output until Q is selected
    	
    	scanf("%c", &selection);
    	
    	
    	
    		if(selection == 'D')
    		{
    			printf("\n\tStore name: Del Mar\n");
    			printf("\tTax rate: %.2f\n", trateDelmar * 100);
    			printf("\tPurchase amount: %.2f\n", sales);
    			printf("\tTax amount: %.2f\n", staxDelmar);
    			printf("\tTotal sale amount: %.2f\n", totamtDelmar);
    			fflush(stdin);
    			getch();
    			
    		}
    		if(selection == 'E')
    		{
    			printf("\n\tStore name: Encinitas\n");
    			printf("\tTax rate: %.2f\n", trateEncinitas * 100);
    			printf("\tPurchase amount: %.2f\n", sales);
    			printf("\tTax amount: %.2f\n", staxEncinitas);
    			printf("\tTotal sale amount: %.2f\n", totamtEncinitas);
    			fflush(stdin);
    			getch();
    			
    		}
    		if(selection == 'L')
    		{
    			printf("\n\tStore name: LaJolla\n");
    			printf("\tTax rate: %.2f\n", trateLajolla * 100);
    			printf("\tPurchase amount: %.2f\n", sales);
    			printf("\tTax amount: %.2f\n", staxLajolla);
    			printf("\tTotal sale amount: %.2f\n", totamtLajolla);
    			fflush(stdin);
    			getch();
    			
    		}
    	}
    		while(selection == 'Q');		
    					
    	return 0;	
    }
    
    // End of program

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by mauhdeeb View Post
    Hey guys

    I got the this program to do what I want but it doesn't loop until 'Q' is entered. I am just not understanding this, can someone tell me why this doesn't loop?
    Because that's what you want.

    Code:
    while(selection == 'Q');
    Would it help if it is written in more English like?
    Do as long as selection is equal to 'Q'
    Perhaps you meant selection != 'Q'?
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Try this

    Code:
    ....
    while (1)
    {
         	scanf("&#37;c", &selection);
            if (selection == 'D')
            {
                .......
            }
            else if (selection == 'Q')
                   break;
     
    }
    return 0;
    ....
    You could use a switch too.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    I see, I am reading it wrong. I think I understand now.

    Thanks

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You loop while the entered value is Q, while you probably want to loop while it isn't Q.
    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. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM