473,399 Members | 2,478 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,399 software developers and data experts.

Help with cin.fail()

Hi,

I need some help with cin.fail()

I'm making a program that accepts an integer and asks for in to be put in again if it is not an integer. My code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.      do
  3.      {
  4.         valid = true;
  5.  
  6.          cout << "Enter an integer: ";
  7.         cin >> number;
  8.  
  9.         if(cin.fail())
  10.         {
  11.             cout << "Error! Please enter integer value! " << endl;
  12.             cin.clear();
  13.             cin.ignore(50, '\n');
  14.             valid = false;
  15.         }
  16.     }
  17.     while(!valid);
  18.  
  19.  
works partially.

It only works if the first 'charater' (?) is a character,
i.e. number = i123

but if number = 123i321

then is takes number ase 123, which is not what i want. I want it to ask for a new input.

I hope its possible, i'm assuming it'll be a simple fix, thanks for any help given.
Sep 21 '06 #1
1 6856
Banfa
9,065 Expert Mod 8TB
This is a common problem with these functions.

You will have to read the user input as a string (use getline or something) and then validate and convert the function to binary yourself.

This is not as hard as it sounds as the c function

strtoul

performs pretty much everything you require
Sep 21 '06 #2

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

Similar topics

2
by: Luis | last post by:
the assignment is to count the number of words in a txt file here is my code #include <iostream> #include <fstream> #include <cassert> using namespace std; int main () {
5
by: Ciar?n | last post by:
I have about 7 tables I need to join, but am having a lot of difficulty with the joins, that I need some help on. I'll provide the details of four tables to illustrate the scenario. I have one...
2
by: Stedak | last post by:
C# I am writing a function that allows the user to enter fractions as answers (i.e. how many asprin tablets for a dosage?). I have the first half of the Regex figured out but don't know how to...
3
by: Marcia Hon | last post by:
Hi, I am trying to use the select() socket programming command to select between stdin and a connection. Currently, I have a listening stream and stdin that I insert into the fd_set. The problem...
2
by: Chua Wen Ching | last post by:
Hi there, SetCommState fail to work in C#, i keep gettting the error of 87, invalid parameter. I use this GetLastError to get the value 87. There is a difference between GetLastError and...
0
by: =?Utf-8?B?Sm9obkJhdGVz?= | last post by:
This is my first custom installer and essentially I needed to create one that installed windows installer 3.1 then installed .Net 2.0 Service Pack 1. I could not find a pre-packaged .Net 2.o SP 1...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
5
by: mcfly1204 | last post by:
I am attempting to use WebRequest to access a page that requires a login/password to access. My last WebRequest continues to timeout. Any help or thoughts would be appreciated. namespace...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.