While Loops.... why does it ask me to enter the value twice?

This is a discussion on While Loops.... why does it ask me to enter the value twice? within the C Programming forums, part of the General Programming Boards category; Hey guys, been trying to figure this while loop stuff out. Just started learning C, and I am new to ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Unhappy While Loops.... why does it ask me to enter the value twice?

    Hey guys, been trying to figure this while loop stuff out. Just started learning C, and I am new to programming all together. Basically what I have are 3 products each a a specific price, but I want to change the price of the product by a specific percent, and must be less than 50%. The problem is when it asks me how much to increase the product, I have to enter it twice, any ideas on why.
    Code:
    float appleChange;
    float apples;
    float newAppleIncrease;
    apples=2.2;
    newAppleIncrease=0; 
    
    /*Some other stuff here*/
    
    printf("Please type in the new percent price change to be applied to apples\n");
    	
    	scanf("%f\n", &appleChange);
    	if (appleChange > .5){		
    		while (appleChange > .5){
    			printf ("Please make a change that is less than .5");
    			scanf("%f  \n", &appleChange);
    			}
    	}		
    	while (newAppleIncrease<=2.2){	
    	newAppleIncrease = (apples * appleChange)+apples; 
    	
    	
    	printf("The new price for the apples is  %.2f \n",newAppleIncrease);
    	}

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should look at the FAQ on how to get a line from the user. Basically it has to do with the way you are using scanf, and the way you are actually entering data. You are scanning for:

    float space newline

    Is that what you are entering?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Do you mean it actually asks the question twice?

    A couple of things:

    Code:
    	if (appleChange > .5){		
    		while (appleChange > .5){
    You don't have to do that. You can get rid of the "if", since the while will only happen "while appleChange > .5" -- if appleChange is <= 0.5, it won't happen.

    The other thing (that may not be so important to you here and now) is that it is a bad idea to use floats for money, because money is actually fixed precision, not floating point precision, and floats can only accurately represent numbers that can be created by dividing one by two (ie, 0.5, 0.25, 0.125, 0.0625, et al). Notice "0.1" and many other change type numbers are not in this list. Rounding will help, but it you'll need to do it pretty often. To see what I mean, compile and run this:
    Code:
    #include <stdio.h>
    
    int main() {
            float i;
            for (i=0.0f; i<20; i+=0.1f) {
                    printf("%f\n",i);
            }
            return 0;
    }
    What you think will happen, does not happen.

    Use an int for the number of cents instead. Since C rounds down, you can then get the change with modulus -- cents%100 and the dollars with cents/100.

    wrt to scanf issue, read this:

    http://206.251.36.107/programming/stdin_buffer.mhtml
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Smile Thanks guys

    Thanks guys, I got rid of the if and realized I placed \n on the scanf, that's why I was having to enter the information on twice. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to enter additional input after validation
    By deleted_user in forum C Programming
    Replies: 22
    Last Post: 11-20-2008, 05:09 PM
  2. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 01:35 PM
  3. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  4. i dunno how to do loops and functions together
    By Noobie in forum C++ Programming
    Replies: 30
    Last Post: 02-03-2003, 06:05 PM
  5. I need help badly
    By taz_jiggy in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 10:36 PM

Tags for this Thread


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