473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Preventing user mistakes

Here is the deal , I'm writing a function that makes the user guess for a number already set by me , the user will enter a single digit number , and if it exists , it's printed in its right position on the screen, like hangman but for numbers :P
I am reading the input of the user using scanf and i dont want to use gets, so plz let ur answers differ from that ....
Im just checking if the user_number is between 0 and 9 to make sure that it's a single digit number, the code is simple :
if(user_number >= 0 || user_number <= 9) // if correct, enter the function ...
{

}
else printf("the number does not exist");

Note that the user has a certain amount of tries to guess ...
But the deal is that how can i prevent the user to type 'a' for example, if he types 'a' or any other character, the program will print the message of "the number does not exist " but will also instantly go through all the number of tries loop and finish it.

Can anyone tell me how can i prevent that?? is there a way to flush the screen and preventing the loop to happen??

PS: I dont want to use the gets function to read the input, i must stick to an array of integers to store my number in.

Thx
Feb 1 '08 #1
3 1426
Ganon11
3,652 Expert 2GB
Well, I know you can use the return value of scanf to determine if your input was correct (i.e. an integer). scanf returns an integer, which holds the number of values properly read. For example:

Expand|Select|Wrap|Line Numbers
  1. int try1 = scanf("%d", &mynum); //User enters 23, try1 holds 1
  2. int try2 = scanf("%f %d %f", &myfloat1, &myint, &myfloat2); //User enters 3.14 10 MYBAD, try2 holds 2
  3. int try3 = scanf("%d", &user_number); //User enters 'a', try3 holds 0
See if this will work for you.
Feb 1 '08 #2
Thx for that , but it didn't solve the problem ...

it's knowing that the value is not between 0 and 9 and it's printing the right message.
The problem is that it's in a loop, and when it's supposed to prompt the user for a number each time, it's just assuming that the user is enterin 'a' each time , so it's printing the message 10 times then exists...this doesnt occur if the user types '33' in that case, its working just fine.
Feb 1 '08 #3
oler1s
671 Expert 512MB
The problem is that it's in a loop, and when it's supposed to prompt the user for a number each time, it's just assuming that the user is enterin 'a' each time , so it's printing the message 10 times then exists...this doesnt occur if the user types '33' in that case, its working just fine.
Not surprising. By the way, we almost never, ever, ever recommend using scanf. If you want to convert numbers, the recommended way is fgets then strtol.

Anyway, what you need to realize about scanf is how it deals with failed conversions. When scanf fails to convert something into a number, it leaves whatever that is on the input stream.

So if you had in the input:
a123

Scanf would fail on a. But it would leave a in the input stream. So the next time you run scanf, you pick up the a again. And fail. And again. And fail. See the problem.

You need to flush the input stream. See cprogramming.com on how to do this.
Feb 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bruce | last post by:
I have a number of forms that do significant work based on variables POSTed from the form. What is the common method of detecting and preventing this work from being done when the form is POSTed as...
7
by: Matt | last post by:
I have an interactive web page that I need to prevent refreshes on. The problem is that I want to ALLOW resubmissions, but only via the submit button. My web page has two forms on it, one form for...
5
by: Fred | last post by:
Hi out there, I have problems finding a way to warn a user that another user intends soon to update the same specific row. Let me explain. User 1 get to a JSP "update customer record" page....
9
by: Preston Crawford | last post by:
I know this is probably a dumb question so please be patient with me. I've been doing HTML since 1994, but mostly for projects that had to be as completely compatible as possible and mostly...
8
by: CJM | last post by:
How do people go about preventing the user from submitting a form for a 2nd time? For example, the user submits a form, clicks on the back button, and the submits the form again. I have used...
18
by: Elroyskimms | last post by:
I have a table using an identity column as its Primary Key and two columns (table reduced for simplicity) EmployeeNumber and ArrivalTime. CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (10)...
3
by: shortbackandsides.no | last post by:
I've been having trouble preventing users pressing Enter part way down a form so the incomplete form gets submitted. I came up with a possible solution - the code below seems to work in both...
1
by: Eike | last post by:
Hi, I am unable to delete a subfolder that I have created programatically. I am using a modification of the apiSHFileOperation by Dev Ashish (http://www.mvps.org/access/api/api0026.htm) to copy...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.