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