473,794 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preventing user mistakes

2 New Member
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 1446
Ganon11
3,652 Recognized Expert Specialist
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
joexoxox
2 New Member
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 Recognized Expert Contributor
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.co m 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
4617
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 the result of the user clicking the Back or Reload buttons in the browser? --Bruce
7
2116
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 adding users, and one form for removing users. I want to be able to add a user, click the submit button, add another user, click the submit button again, and so on, BUT, disallow adding a user and then hitting refresh (which would add the same...
5
613
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. - The page does a read of the existing record and loads it into the gui fields for edit.
9
4581
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 existing projects. So that meant dealing with the traditional table layouts (i.e. using tables for the entire structure of a page). I'm just not getting to use DIVs solely for the layout of a page. The one problem I'm having is that I'm trying to setup...
8
2343
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 various techniques in the past (depending on circumstances) but I'd be interested in the techniques you guys currently use. Thanks --
18
27722
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) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , NOT NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
3
1768
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 mozilla and MSIE - is this a good way to solve the problem? Is there a better alternative? Have I done anything stupid? My aim was to disable the normal submit process then use javascript to submit which appears to bypass that and work...
1
1611
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 the files. A File Copy function is called as part of a loop to copy a series of files from a variety of locations that are stored in the database. All the files are copied to a folder specified by the user. Once the user has used this folder (to...
95
5429
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 ____________________________________ | | | ------------------ | | | BUTTON | | | ...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.