Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# validating user input

Posted on 2004-10-27
Medium Priority
206 Views
Can someone PLEASE show me how to accept only integer input from the user?

I have tried this...

string s;
int intVersion = atoi(s.c_str());

do
{
cout << "Enter the Fibonacci number to compute: " << endl;
cin >> s;
if (intVersion == 0)
{
cout << "Not an integer\n";
}
else
{
cout << "Is an integer\n";
cout << intVersion;
}
}while(intVersion == 0);

But even when I enter 1 it says its not an integer. How do I do this?
0
Question by:jandhb
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 2
• 2

LVL 15

Expert Comment

ID: 12429941
It would help to convert it AFTER reading it, instead of before.
0

LVL 1

Author Comment

ID: 12429943
please show me what you mean.
0

LVL 1

Author Comment

ID: 12429987
i dont understand what your saying. can you show me?
0

LVL 15

Expert Comment

ID: 12430174
string s;

do
{
cout << "Enter the Fibonacci number to compute: " << endl;
cin >> s;
int intVersion = atoi(s.c_str());
if (intVersion == 0)

The call to atoi should occur after s has acquired a value from the input stream.
0

LVL 39

Expert Comment

ID: 12430685
Actually, atoi() isn't best choice to check numeric input as it accepts all string input and would give a non-zero return for "-123XT" or "7AA123". Better use strtol:

string s;
char* pstop;

do
{
cout << "Enter the Fibonacci number to compute: " << endl;
cin >> s;
int intVersion = strtol(s.c_str(), &pstop, 10);
if (*pstop != '\0')   // not a number (stopped before end)

Regards, Alex

0

LVL 1

Author Comment

ID: 12432560
Alex,

do
{
cout << "Enter the Fibonacci number to compute: " << endl;
cin >> s;
int intVersion = strtol(s.c_str(), &pstop, 10);
if (*pstop != '\0')   // not a number (stopped before end)
{
cout << "Not an integer\n";
}
else
{
cin >> intVersion;
}
}while(*pstop != '\0');
0

LVL 39

Accepted Solution

itsmeandnobodyelse earned 80 total points
ID: 12433316
>> Is this what your saying....

you would have to enter the (valid) number twice

int intVersion = 0; // define outside of loop or the value is lost after loop
char* pstop;
// begin an infinite loop
while (true)
{
cout << "Enter the Fibonacci number to compute: " << endl;
cin >> s;
intVersion = strtol(s.c_str(), &pstop, 10);
if (*pstop == '\0')
break;     // break loop if valid number
cout << "Not an integer\n";
}

Normally, i avoid using same condition twice as while condition and if condition. An alternative to that is using an input function

bool getNumber(int& intVersion)
{
char* pstop;
string s;
cout << "Enter the Fibonacci number to compute: " << endl;
cin >> s;
intVersion = strtol(s.c_str(), &pstop, 10);
return (*pstop == '\0'); // true if good number
}

int main()
{
int intVersion = 0;
while (!getInput(intVersion))
{
cout << "Not an integer\n";
}
return 0;
}

Looks great?

Regards, Alex

0

LVL 1

Author Comment

ID: 12441212
Alex, again thank you for your help here.

If you can I would like for you to take a look at my post here on Fibonacci number() as I need some help.

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21187247.html
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more â€¦
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.htâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month11 days, 22 hours left to enroll