Error checking with loop

This is a discussion on Error checking with loop within the C Programming forums, part of the General Programming Boards category; OK guys I'm back! This was actually a problem I asked you guys about before, but now I am trying ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Error checking with loop

    OK guys I'm back! 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 )
    				
    				{
    				printf("Not a valid hour time! Please enter a new one! "); /* If not, output */
    			    }
    		   
    		while (tfseconds > 60 || tfseconds < 0)
                {
                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!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Consider the following lines of code:

    Code:
    while (tfseconds > 60 || tfseconds < 0)
    {
      printf("Not a valid seconds time! Please enter a new one! \n");
    }
    There is no way the value of tfseconds can change in that loop. Therefore, the loop becomes an infinite loop if the codition is true and will get continue to loop indefinitely.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: Error checking with loop

    Originally posted by CrackerJack
    OK guys I'm back! 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.
    Last edited by WaltP; 10-21-2003 at 08:45 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    while ( scanf("%d:%d", &tfhour, &tfseconds) == tfhour <=0 || tfhour > 24 )
    I think you're just trying to do too much with this statement, and end up doing nothing that you intended. I would advise slowing down a little. Try this instead...

    Code:
      printf( "Enter a 24-hour time (eg 22:23): " );
    
      while( 1 )
      {
        scanf( "%d:%d", &tfhour, &tfminutes );
    
        if( ( tfhour < 0 )
         || ( tfhour > 23 ))
        {
          printf( "Not a valid hour time! Please enter a new one! " ); 
          continue;
        }
        
        if( ( tfminutes < 0 )
         || ( tfminutes > 59 ))
        {
          printf( "Not a valid hour time! Please enter a new one! " ); 
          continue;
        }
    
        break;
      }
    More lines of code, but it makes it easier to read and follow.
    There is no such thing as a humble opinion

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    I figured this one out! After I went through the logic on paper like a million times I finally realized my problem...

    I had to use a second scaf inside the second 'while' loop to make the loop stop continiously running. Worked like a charm!

    On to my next problem...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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