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

Distinguish between chararters and integers??

if you type "123e31" in the command line.

the computer may read only "123"

i mean, how to make the system know that the input is wrong.
Apr 5 '07 #1
3 1233
sicarie
4,677 Expert Mod 4TB
if you type "123e31" in the command line.

the computer may read only "123"

i mean, how to make the system know that the input is wrong.
I think you're asking how to do input validation, am I correct?

If you want to make sure what is read in is only a number, I would recommend a while loop.
Expand|Select|Wrap|Line Numbers
  1. input initialized to be out of range
  2. while (input is not in some range) {
  3.     input = readline/getline/getinput
  4. }
  5.  
If a string (e) is put in to an int, it won't be in that range, and the user will be re-prompted for the input.

Does that answer your question?
Apr 5 '07 #2
but there's one question here.
--------------------------------------------
int m;
cin>>m;
--------------------------------------------
if you typed something other than an integer,
the system will break.

i mean , how to prevent the system from breaking down and
how to validate what you have typed wasn't an integer.
Apr 6 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Remember, the >> operator means FORMATTED input. That menas you know ahead of time what's being entered.

This code:

int m;
cin >> m;

requires an integer to be entered. When you enter 123e31, you get 123 in the variable m. The cursor in the inout buffer is pointing at the e. If you cin>> m again, a fail but is set because e is not an intger. Once the fail bit is set all of your >> operations will fail because the first thing that's checked is the fail bit.

You then need to cin.clear() to reset the fail bit AND you need to flush the input buffer so you can start over. This may require an operatong system call since C++ does not own the input buffer. Miscrosoft uses __flushall() for this.

For input editing, you have have yoiu use cin.get() instead so you can look at each individual byte in the buffer.
Apr 6 '07 #4

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

Similar topics

29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
4
by: Neal Becker | last post by:
I can do this with a generator: def integers(): x = 1 while (True): yield x x += 1 for i in integers():
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
16
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
5
by: Jon Maz | last post by:
Hi there, I am experimenting with the FileSystemWatcher object. I have set the NotifyFilter as follows: myFileSystemWatcher.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime...
1
by: calvin | last post by:
Can anyone write a code for this? Searching a set of Integers You are given two sets of integers. S1 and S2. The size of S1 is less than sizeof S2, i.e. the number of integers in S1 is less...
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
7
by: mathon | last post by:
hi, i have the following recursive function: unsigned int sum_odds(unsigned int n) { if(n==1) return 1; else
9
by: Mark Morss | last post by:
I would like to construct a class that includes both the integers and None. I desire that if x and y are elements of this class, and both are integers, then arithmetic operations between them,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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.