Thread: Problem with simple c program

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

    Problem with simple c program

    My problem is simple the program asks for 2 numbers. Then takes the two numbers Price and Quantity and stores them in TotalRevenue. The program terminates before it asks for the second number Quantity. It won't let me enter it in. here's the code. There's no errors in the code that's one reason I can't figure it out. If someone has a clue as to what's going on it would be much appreciated. I've been trying to figure this out for two days now.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      float Price = 0;
      int Quantity = 0;
      float TotalRevenue; 
      
      printf("\nEnter Price: ");
      scanf("%.2f",&Price);
      printf("\nEnter Quantity: ");
      scanf("%d",&Quantity);
      
      TotalRevenue = Price * Quantity;
      
      printf("\nThe total revenue is: $%.2f\n",TotalRevenue);
      
      system("PAUSE");	
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you aren't receiving any compiler warnings, you need to turn up the warning levels.

    Code:
    main.c||In function 'main':|
    main.c|11|warning: unknown conversion type character '.' in format|
    main.c|11|warning: too many arguments for format|
    ||=== Build finished: 0 errors, 2 warnings ===|
    Unlike "printf()", you don't need to use a precision modifier with "scanf()". Try just using "%f".

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Actually, your code works fine for me. I just got rid of the .2 part of your first scanf because when I compiled with it, it didn't like it.

    Here's my output:
    Code:
    
    Enter Price: 123
    
    
    Enter Quantity: 34
    
    
    The total revenue is: $4182.00
    sh: PAUSE: command not found
    I think the PAUSE thing is an OS difference thing but the code finishes.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Quote Originally Posted by MutantJohn View Post
    Actually, your code works fine for me. I just got rid of the .2 part of your first scanf because when I compiled with it, it didn't like it.

    Here's my output:
    Code:
    
    Enter Price: 123
    
    
    Enter Quantity: 34
    
    
    The total revenue is: $4182.00
    sh: PAUSE: command not found
    I think the PAUSE thing is an OS difference thing but the code finishes.
    [QUOTE]
    Hey thanks, man you figured that out in one second and I couldn't figure it out for two days dam I feel like a moron!

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Hey thanks guys I didn't think about scanf it isn't like the printf like you said Wow I wish that would have occured to me

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It's easy to see your logic though. You were trying to only scan in 2 decimal places, weren't you?

    Well, either way, you live and learn and the good news is, now you really know how it works.

    Edit: And to turn up compiler warnings, I think you use something like
    Code:
    gcc -W -Wall -Wextra
    Last edited by MutantJohn; 08-14-2013 at 06:43 PM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Hey thanks, man you figured that out in one second and I couldn't figure it out for two days
    Allow me to repeat myself - maximize your compiler warnings. You could have figured this out yourself in a second had you done so.

  8. #8
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by Matticus View Post
    Allow me to repeat myself - maximize your compiler warnings. You could have figured this out yourself in a second had you done so.
    i hear this from time to time in this forum. how do you do so? i run both OSx and Linux therefore im just
    Code:
    gcc -o foo foo.c
    what do i need to add to "maximize" my compiler warnings?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    -Wall (or -Wextra)
    -Werror
    -Wpedantic
    ...etc

    (source)

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    To not think about flags every time I compile sample - I created simple Makefile and put all flags I want into it.

    So after changing my c-file contents in the sample directory - I just do make
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. Problem with simple program
    By geoffrey in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2010, 04:15 PM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Problem with simple C program....
    By OC2233 in forum C Programming
    Replies: 5
    Last Post: 09-20-2007, 09:29 PM