Thread: What am I doing wrong.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    32

    What am I doing wrong.

    The assignment calls for :

    DIRECTIONS: Write a C program which prompts the user
    for a start time and an end time. Each time has the
    format hh:mm (hours: minutes). Your program should
    compute and display the elapsed time difference
    in seconds. This should be followed immediately by
    'PROGRAM ENDS' Do not include the single quotes.


    Heres what I have:
    insert
    Code:
         7  #include <stdio.h>
         8
         9   int main(void)
        10  {
        11
        12  int i, j, k, l, m;
        13
        14  m = (3600*k + 60*l) - (3600*i + 60*j);
        15
        16  printf("Please enter a start time in hh:mm format.\n");
        17  scanf("%d:%d", &i, &j);
        18  printf("Please enter an end time in hh:mm format.\n");
        19  scanf("%d:%d", &k, &l);
        20  printf("elapsed_time : %d\n", m);
        21  printf("PROGRAM ENDS\n");
        22                                                      
        23  return 0;
        24  }
    insert
    Whenever I run the prgram, it gives me the value -28860 ? What is wrong?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You're attempting to compute m BEFORE setting the values of i, j, k, and l.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    So the equation for m needs to go before the the last printf statement?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It needs to go after the two scanf() statements which input i, j, k, and l, and before the printf() statement which prints m (in other words, between current lines 19 and 20). You can't print m until you've computed it, and you can't compute it until you set i, j, k, and l.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Ok thank you very much. Your recommendation worked. Sorry for the newbie question.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I also suggest you give the variables proper names.
    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. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM