Re: Error checking with loop
Quote:
Originally posted by CrackerJack
OK guys I'm back! :p This was actually a problem I asked you guys about before, but now I am trying to add a looped error checking. I want the program to say if the hour is wrong, if the seconds are wrong, or both. Then let the user retry it. I got the hour to work, but the other two are acting wierd. This is what I have so far:
Code:
#include <stdio.h>
main ()
{
int tfhour; /* Intialize tfhour, tfseconds, & twhour */
int tfseconds;
int twhour;
/* Prompt user for input of military time */
printf("Enter a 24-hour time (ex. 22:23): ");
/* Check to see if "seconds" input is valid */
/* and contines to check the hour */
while ( scanf("%d:%d", &tfhour, &tfseconds) == tfhour <=0 || tfhour > 24 )
I highly suspect this while() statement. Since scanf() returns the
number of values scanned, it will return 2. That makes this statement:
while ( 2 == tfhour <=0 || tfhour > 24 )
{
printf("Not a valid hour time! Please enter a new one! "); /* If not, output */
}
while (tfseconds > 60 || tfseconds < 0)
seconds cannot be equal to 60
{
printf("Not a valid seconds time! Please enter a new one! \n");
}
/* If between 1 & 12, make that input twhour */
if (tfhour <= 12 && tfhour > 0)
twhour = tfhour;
/* If between 13 & 24, subtract 12. Then place in */
/* twhour's postion */
if (tfhour >= 13 && tfhour <= 24)
twhour = (tfhour - 12);
/* Print twhour & seconds value with output */
printf("Equivalent 12-hour time: %d:%.2d ", twhour, tfseconds);
/* Checking for AM or PM. If the inputed value gives */
/* a 1 off when divided then it equals PM */
if (tfhour / 12 == 1)
printf("PM\n");
else
printf("AM\n");
return;
}
Any advice?
Thanks!
Also, work on your formatting. The idea behind indentation is to make things line up. Understandably, when posting code on the board, things can get a little wierd, so use only spaces or tabs and not both. Also, the "preview" button is your friend. Check what you have before posting and make corrections if the post looks like crap.