Thread: Question with If-then Statements?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Question with If-then Statements?

    I have this written code below and I am doing an If-then statement. For the first If-then, I get it to where it tells me "Try Again" but when I do it again and I enter the same number (just to check to see if it says "Try Again" for a 2nd time), it goes on to the next question. I want it to were if you keep entering the wrong number it keeps saying "Try Again" until you enter the right one.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int hour, spots, parking_permits; 
        float time;
        
        printf ("What hour are you looking for parking? \n");
        scanf ("%d", &hour);
        if (hour<0 || hour>23)
           {
                   printf ("Invalid. Try Again. Time must be between 0 - 23 \n", hour);
                   }
        
        printf ("How many spots are available this semester? \n");
        scanf ("%d", &spots);
        
        printf ("How many parking permits were given this semester? \n");
        scanf ("%d", &parking_permits);    
        
        time = ( 12 - abs(12 - hour) )*(parking_permits/spots);
        printf ("You will have to wait %.2f minutes to find parking\n", time );
        
        getch ();
        
        return 0;
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You'll have to use while loop(or do while), in order to keep it saying the error. Something like this:
    Code:
    do
    	{
        printf ("What hour are you looking for parking? \n");
        scanf ("%d", &hour);
        if (hour<0 || hour>23)
                   printf ("Invalid. Try Again. Time must be between 0 - 23 \n", hour);
    	else
    		break;
    	}while(1);
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While Statements Question
    By thekautz in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 02:48 PM
  2. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 03:06 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM