I have to use a do-while loop to force the user to enter a value within the listed range. Also, if the user does enter a value outside the range, use an "if" conditional to display a message indicating "Out of range".
The prompt, scanf, and the "if" conditional must all reside within the do-while.
Here is what I have so far:
[code]/* Seconds.c by Ryan 09/13/2006 */
/* This program will be able to calculate the number of seconds that
have elapsed on a given day or for a given event. */
#include <stdio.h>
int main()
{
/* Declaration */
long hours, minutes, seconds, total_seconds;
/* Input Section */
printf("Second Calculator \n\n\n\n");
do
{
printf("Hours (0-23) ");
scanf("%ld", &hours);
if (hours > (23)) {
printf(" Out of range");
} /* end if */
}
while (hours < 0 || hours > 23);
printf("\nMinutes (0-59) ");
scanf("%ld", &minutes);
printf("\nSeconds (0-59) ");
scanf("%ld", &seconds);
/* Processing Section */
total_seconds = hours * 3600 + minutes * 60 + seconds;
/* Output Section */
printf("\n\n\n\nTotal Seconds = %ld\n", total_seconds);
return 0;
}
The problem I'm having is once I type in a value higher than 23 for hours it ends up saying Out of rangeHours (0-23) instead of just Out of range.
Any help would be great. Thank you.