# Thread: Minute to hour/minute conversion program

1. ## 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);
}```

Remius

2. 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.

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. 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.

5. Ok thanks, must be a bad book then (Practical C Programming by Steve Oualline).

6. 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. Originally Posted by Todd Burch
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.

8. 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