Thread: Help with C Programming (while loop)

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Question Help with C Programming (while loop)

    Hello, I am fairly new to programming and need some help with the while loop.

    Here is the part that I am having trouble with.

    Code:
    #include <stdio.h>
    
    int main ( ) {
       int veh;
    
    
       printf("\nDo you have a car? (1:Yes, 2:No):  ");
       scanf("%d", &veh);
       while (veh < 1 || veh > 2) {
          printf("Please select 1 for Yes or 2 for No: ");
          scanf("%d", &veh);
       }
    return 0;
    }

    The problem is is that when I type in a letter instead of a number, it displays a wall of "Please select 1 for Yes or 2 for No: ". Is there a way to fix this?

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    reading input is a common beginners problem, see FAQ: How do I get a number from the user (C)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. 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
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread