Very easy code I know...

This is a discussion on Very easy code I know... within the C Programming forums, part of the General Programming Boards category; it is giving me problems though, I am a beginner and cannot find the reason for this code to not ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Very easy code I know...

    it is giving me problems though, I am a beginner and cannot find the reason for this code to not work properly, it compiles but the output it gives is way off

    Code:
    //convereting hours to seconds
    
    #include <stdio.h>
    
    //define constants
    #define SECONDS_IN_HOUR 3600
    
    int main () {
        
        //introduce variables
        float hours_waited, seconds_waited;
        
        //get user input
        printf("How many hours were you waiting?\n");
        scanf("%.2f", &hours_waited);
        
        seconds_waited = hours_waited*SECONDS_IN_HOUR;
        
        
        printf("You waited for %.2f seconds!!\n", seconds_waited);
        
        
        
        system("PAUSE");
        return 0;
        
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Code:
    scanf("%.2f", &hours_waited);
    You can't use .2 in there. Just use %f.

    If you compile your code with warnings turned up, you would find these sorts of problems:

    Code:
    ucf.c:15: warning: unknown conversion type character ‘.’ in format
    ucf.c:15: warning: too many arguments for format

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Ah thank you, I am using Dev C++ how do I turn my warnings up?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by UCFuser View Post
    Ah thank you, I am using Dev C++ how do I turn my warnings up?
    Thought by default wanring was turned on Dev-C++. Anyway this the way you do it, to override the compiler setting on the GUI.

    Tools -> Compiler Options

    In the Add the following commands when calling compiler test box type
    -W -Wall

    To turn on the warning.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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