473,385 Members | 1,569 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,385 software developers and data experts.

Ensuring input is an integer (or converting string to an int)

Hi,

I'm trying to write a bit of code to ensure that a user inputs only digits (an int) when promtped.

I have been successful using a char array and tested it using isdigit, but then i need to pass it to a function as only an integer.

I'd appreciate any help with either how to test whether an input is an interger or to change a char array to an int. (Note, I'm not asking for someone to write the program for me, just for suggestions and explanations of how to use them)

Thanks for any help.
Sep 20 '06 #1
2 4055
Keep on accessing each element of the string and check whether it falls in the range 48 to 57. Because ASCII code of '0' character is 48 and '9' character is 57. If it doesn't fall in the above mentioned range then given input is not a number.

If the given string is a number then use atoi() API to convert it to an integer and atol() to convert it to long integer.
Sep 20 '06 #2
Banfa
9,065 Expert Mod 8TB
use the function strtoul (or strtol for signed values).

This will simultaneously convert the digits to an integer and provide a check that all characters are valid in any base.

Declaration

unsigned long strtoul( const char *nptr, char **endptr, int base );

usage

Expand|Select|Wrap|Line Numbers
  1. char digits[] = "12345";
  2. char *pEnd;
  3. int value;
  4.  
  5. value = (int)strtoul( digits, &pEnd, 10 );
  6.  
The pointer pEnd is set to point at the character on which the conversion terminated, in this case it will point to the string terminator because all digits are valid. I cast the output to the integer type I require.

It the string was "1234Hello" then pEnd will end up pointing to the H of hello so if the user is only supposed to put in valid digits the conversion will always end of the string terminator for a valid string.
Sep 20 '06 #3

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

Similar topics

2
by: Tomas Deman | last post by:
Hi, I need a fast method for converting an int array to a byte array. At the moment, I'm using this: public static byte Int2ByteArray(int array) { byte lbytRetval = new byte; int lintIdxHi;...
6
by: AC 74 | last post by:
I read from a command line using g++. I changed the argv to integers using atoi() in order to add the numbers. Now I need to change them from int to string and itoa is not supported. Can...
13
by: sam | last post by:
Hi, Does STL has Integer class? I want to convert an integer to string. Of course I can use sprintfs C API, but I m wondering whether STL has Integer class to be utilsed. Thanks Sam.
11
by: Peter Olcott | last post by:
Does C++ have anything like this?
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
2
by: shaiful | last post by:
Hi all, I have a small problem with converting from Int to String. Such as I have 3 variables. dim a as integer, dim b as integer, dim c as string. a=12, b=10 I want to see c=12,13 as a string?...
3
by: Krupa Kiran | last post by:
Hi i have function in script. I did a small calculation in that function when i try to pass a value to in Field thet only passes integer value. But i need to pass both String and the integer value....
4
by: mthread | last post by:
Hi, I would like to know the method to convert integer to string. Thanx in advance.
2
by: hili | last post by:
hello, im a starter in C++, for my assignment i have encountered the following warning which stops me from compiling, can any1 help please? any suggestion would be greatly appreciated. thanks alot...
1
by: ahmee | last post by:
this my program code these are my errors below please help me out....guys operater % cannot be applied to java.lang.String.int operater / cannot be applied to java.lang.String.int operater +...
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.