C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-03-2009, 05:39 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 1
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
redened is offline   Reply With Quote
Old 10-03-2009, 07:02 PM   #2
Registered User
 
Join Date: Oct 2006
Location: Canada
Posts: 848
reading input is a common beginners problem, see FAQ: How do I get a number from the user (C)
nadroj is offline   Reply With Quote
Reply

Tags
loop, problem, programming

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Poll event loop rogster001 C++ Programming 2 09-17-2009 04:28 AM
need help with a loop Darkw1sh C Programming 19 09-13-2009 09:46 PM
funny-looking while loop Aisthesis C++ Programming 3 08-30-2009 11:54 PM
nested loop, simple but i'm missing it big_brother C Programming 19 10-23-2006 10:21 PM
loop issues kristy C Programming 3 03-05-2005 09:14 AM


All times are GMT -6. The time now is 01:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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