Thread: Beginner If Statement Help

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    Question Beginner If Statement Help

    Hello! I am in my first year in the CS major at a university and I am a little confused about what's going wrong here in this code. Basically my issue is for example: The first if statement in the code states that if the input value is over 1000, it asks the user for a new value. BUT, if you enter another value over 1000 the second time, it just moves on to the next section of the code. This repeats itself in different situations through this code. The equations and everything in the problem work fine, this is my only problem. Can anyone help / explain?
    Code:
    #include <stdio.h>
    int main()
    
    {
        int room_length, room_width;
        int table_length, table_width;
        int space, people_table;
    
    
        printf("What is the length of the room in feet?\n");
        scanf("%d", &room_length);
        if(room_length > 1000)
        {
            printf("Sorry, the room is too long, please give another length.\n");
            scanf("%d", &room_length); 
        }
    
    
        printf("What is the width of the room in feet?\n");
        scanf("%d", &room_width);
        if (room_width > 1000)
        {
            printf("Sorry, the room is too wide, please give another width.\n");
            scanf("%d", &room_width);
        }
    
    
        printf("What is the length of each of the tables in feet?\n");
        scanf("%d", &table_length);
        if (table_length > 1000)
        {
            printf("Sorry, the tables are too long, please give another length.\n");
            scanf("%d", &table_length); 
        }
    
    
        printf("What is the width of each of the tables in feet?\n");
        scanf("%d", &table_width);
        if (table_width > 1000)
        {
            printf("Sorry, the tables are too wide, please give another width.\n");
            scanf("%d", &table_width);
        }
    
    
        printf("How much space is required between each table in feet?\n");
        scanf("%d", &space);
        if (space > 10)
        {
            printf("Sorry, the space between each table is too large, please give another length.\n");
            scanf("%d", &space);
        }
    
    
        int tables_across = (room_length-space)/(table_length+space); 
        int tables_wide = (room_width-space)/(table_width+space); 
    
    
        printf("How many people can each table seat?\n");
        scanf("%d", &people_table);
        if (people_table > 21)
        {
            printf("Sorry, that is too many people per table, please give another amount of people.\n");
            scanf("%d", &people_table); 
        }
    
        if (people_table < 3)
    
        {
            printf("Sorry, that is too many people per table, please give another amount of people.\n");
            scanf("%d", &people_table); 
        }
    
    
        printf("This arrangement seats %d people.\n", tables_across*tables_wide*people_table);
    
    
        return 0;
    }
    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "if" is not a loop -- perhaps you meant "while"?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to use loops... not if statments ... if statements do not loop...

    Code:
    do 
      {  printf("What is the length of each of the tables in feet?\n");
         scanf("%d", &table_length); }
    while (table_length > 1000);

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Thank you. I actually just flipped a little forward in my textbook and found "while".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Problem with If-Else Statement
    By matrixx333 in forum C Programming
    Replies: 3
    Last Post: 08-16-2009, 09:36 PM
  2. Beginner question about the Return statement
    By lucidrave in forum C Programming
    Replies: 3
    Last Post: 08-07-2009, 05:19 PM
  3. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  4. Beginner needs help with If Statement
    By Sahaal in forum C++ Programming
    Replies: 7
    Last Post: 12-23-2006, 04:17 PM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM