Thread: don't understand why this code doesn't work properly

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    don't understand why this code doesn't work properly

    I can't figure this out
    this is the code







    Code:
    #include <stdio.h>
    
    float Total_Revenue(float,float);
    
    int main(void)
    {
        float price = 0.00;
        float quantity = 0.00;
        float revenue;
    
    
    
        printf("Enter price:  ");
        scanf("%.2f" , price); 
        printf("\nEnter quantity:  ");
        scanf("%.0f", quantity);
    
        revenue = Total_Revenue(price,quantity);
    
        printf("The total revenue is %.2f" , revenue);
    
        return 0;
    }
    
    float Total_Revenue(float x, float y)
    {
        float price;
        float quantity;
    
        return  price * quantity;
    }
    Here's the output


    ------------------------------------------------------------------------------------------------------------
    Enter price: 5.00

    Enter quantity: The total revenue is 0.00Press any key to continue...

    ------------------------------------------------------------------------------------------------------------
    why won't it let me enter the quantity

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You want to put the value into the address of (&) the variable, eg:
    Code:
    scanf("%.2f" , &price);
    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

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I tried this but it still gives me the same output I changed the code got rid of the function








    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
    	float price;
    	float quantity;
    	float revenue;
    
    
    
    	printf("Enter price:  ");
    	scanf("%.2f" , &price); 
    	printf("\nEnter quantity:  ");
    	scanf("%.0f", &quantity);
    
    	revenue = price * quantity;
    
    	printf("The total revenue is %.2f" , revenue);
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You need to assign values to your variables in your revenue function.

    Or, just return x*y directly.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I got rid of the function why won't this simple code work that I posted above

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Take in all decimal points in your scanf call and you should be fine as far as I can tell. Then restrict it with printf like you do already.

    Also if quantity should have zero decimals it might make sense to use an int there instead.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Because you are using printf() format specifiers, in scanf().

    Remove the .2 and .0 in your two scanf()'s calls, and all will be fine.


  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    remember those variables are local to your function so if u want you can either return them or put the variable in a ptr and use them later

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Adak your a genius it worked thanks man

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    hey thanks everybody I learned something new that I didn't realize before or forgot

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Subsonics you were right I just didn't think I could mix a int with a float but I guess you can

    I should already know this stuff but I must of forgot about it
    thanks for helping me understand it better now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. should code snippets work ?
    By kryptkat in forum C++ Programming
    Replies: 11
    Last Post: 12-16-2009, 01:08 PM
  2. Shouldn't this code work? (select())
    By azjherben in forum Networking/Device Communication
    Replies: 9
    Last Post: 07-20-2009, 04:02 PM
  3. how to properly call an executable from C code?
    By remy06 in forum C Programming
    Replies: 3
    Last Post: 05-14-2009, 03:48 AM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Exactly what do I need to reinstall to ensure Java will work properly with my browser
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-09-2002, 07:10 PM