Minute to hour/minute conversion program

This is a discussion on Minute to hour/minute conversion program within the C Programming forums, part of the General Programming Boards category; Hi I just started to learn programming and while doing an exercise in the book I'm reading I decided to ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Minute to hour/minute conversion program

    Hi I just started to learn programming and while doing an exercise in the book I'm reading I decided to expand it a little. The exercise was to code a program that converts a number of minutes into a number of hours/minutes. The features I tried to add was 1) that when I enter the letter 'q' I would like the program to quit, and 2) when I enter everything else than 'q' or a number I would like the program to print: <char entered> is not a number!. The first thing I figured out a solution to but I'm not shure if the way I have done it is a good solution? The second thing I can't figure out a solution for but I thought if it is possible that maybe you could make the program check if the entered is an integer or something like that? It would be very nice if someone could help me with a way to do this.

    Below is my code:
    Code:
    #include <stdio.h>
    char line[100];
    int hour = 60;
    int time;
    int hours;
    int minutes;
    char quit;
    
    int main()
    {
        while (1) {
              
              printf("Enter time in minutes: ");
        
              fgets(line, sizeof(line), stdin);
              sscanf(line, "%d", &time);
              sscanf(line, "%c", &quit);
        
              hours = time/hour;
              minutes = time%hour;
        
              if (quit == 'q') {
                 break;
              }
                                          
              if (hours != 1 && minutes != 1) {
                  printf("This is the same as %d hours and %d minutes.\n\n", hours, minutes);
              }
        
              else if (hours != 1 && minutes == 1) {
                  printf("This is the same as %d hours and %d minute.\n\n", hours, minutes);
              }
          
              else if (hours == 1 && minutes != 1) {
                  printf("This is the same as %d hour and %d minutes.\n\n", hours, minutes);
              }
           
              else if (hours == 1 && minutes == 1) {
                  printf("This is the same as %d hour and %d minute.\n\n", hours, minutes);
              }
              
        }
        return(0);
    }
    Thanks in advance!

    Remius

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    There are several ways to see if input is a number or not. My typical approach is to use atoi to convert the input to a number. Then you can check if atoi returns 0 (indicates failure unless the input string was "0"), and check if the input string isn't "0", then it's an invalid number:

    Code:
    char buf[50];
    fgets(buf, sizeof(buf), stdin);
    if (atoi(buf) == 0 && buf[0] != '0') /* Not a number */
    Also, avoid global variables - move them inside main.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Thanks that did it. But could you please explain to me why I should avoid global variables and put them inside main? Because in the book I'm reading they always have them global outside main.

    Remius

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Because it's prone to errors - any function can modify them whenever. It's considered bad practice. Not to mention global variables have lifetime existence, hogging memory all the way.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Ok thanks, must be a bad book then (Practical C Programming by Steve Oualline).

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Sometimes global variables are practical.

    If all functions of your program need access to a variable, then that's what global variables were created for.

    It is easy to get lazy and just make all variables global, but from an software engineering standpoint, that's bad practice, as was mentioned.

    Todd

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    Quote Originally Posted by Todd Burch View Post
    Sometimes global variables are practical.

    If all functions of your program need access to a variable, then that's what global variables were created for.
    Perhaps, but that's not a very frequent case, is it? The typical approach is to just pass around all the variables those functions need instead.
    I do agree that global variables must sometimes be used, but as you say, you shouldn't be lazy and overuse them. I find that they are very rarely needed.
    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.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    No, it's not frequent.

    In a program I am writing now, one of the passed switches is whether or not to trace. I keep this as a global, since adding it to every function prototype, function call and function definition would be a PITA.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Phone number to word conversion program
    By lostmyshadow in forum C Programming
    Replies: 12
    Last Post: 04-21-2009, 02:31 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Minute Program output problems
    By Cobalt in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2003, 10:16 AM
  4. Currency Conversion Program
    By kgraw21 in forum C Programming
    Replies: 5
    Last Post: 04-19-2002, 08:39 AM

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