C++ Integer Input

Started by
14 comments, last by _goat 17 years, 7 months ago
What is the best and safest way to get integer input from a user? (For a Console App) Currently, I'm just using cin, but that is giving me a lot of trouble when I try to cin to a (int) variable, and the user inputs a non-integer character. Someone suggested I include <sstream> and use this method:

string mystring;
int myint;
getline (cin,mystring);
stringstream(mystring) >> myint;
Is that good enough or is there something better I can do to assure that the input I receive is taken as an integer?
--------------------------------------Amaze your friends! Astound your family! Kennify your text!
Advertisement
cin should work perfectly fine. The other method will work no better.

You simply need to do error checking after you have received input.

use the isalpha(input) function to check what the user has input. If this returns true, then the input contains characters. If its false, then it is a valid integer, and you can proceed with your program.

You can then hold your user input in a do while function:

char myInput[MAX_LENGTH];bool validInput = false;do{cout << "Enter input:  ";cin >> myInput;validInput = isInputInt(myInput);} while (!validInput);


have your function return false if isalpha returns true. Run it through a loop checking each element of the char array for isalpha. After it passes inspection, and its a valid integer, convert it to an integer.
That is no good, isalpha() returns false even if I input punctuation marks, which aren't integers.
--------------------------------------Amaze your friends! Astound your family! Kennify your text!
The CIN function will return a true or false value as to whether or not it succeeded. If it succeeds, it will fill in the inputted value into the address of the variable.

This example makes sure the input is a valid integer.

#include <iostream>
using namespace std;

void main()
{
int j = 0;
cout << "Enter a number: ";
if(!(cin >> j))
cout << "You must enter a number!" << endl << endl;
else
cout << "You entered " << j << endl << endl;
}
"I'd rather know one thing, no matter how ordinary, than discourse endlessly on great issues." -- Galileo
Quote:Original post by F1N1TY
The CIN function will return a true or false value as to whether or not it succeeded. If it succeeds, it will fill in the inputted value into the address of the variable.


To be more precise, cin is not a function, it is an object (a global object, BTW) - an instance of a class which overload operator bool in order for you to be able to compile and run that code.

@Mr Safety
char myInput[MAX_LENGTH];cin >> myInput;
(/quote]
Hum. No. This code is subject to the classical buffer overflow attack. Use std::string instead [smile]

Regards,
The code I posted will determine if it can place the inputted value into the variable (i.e, input can't be larger than maximum int range value).


Quote:Original post by Emmanuel Deloget
To be more precise, cin is not a function, it is an object (a global object, BTW) - an instance of a class which overload operator bool in order for you to be able to compile and run that code.


Yeah, sorry about that...


"I'd rather know one thing, no matter how ordinary, than discourse endlessly on great issues." -- Galileo
How would I go about using std::string for the itneger input?

Should I just use getline() to get the input into the string and then use type casting to convert it to an integer? What will happen if the input is purely text and it is casted to an integer?
--------------------------------------Amaze your friends! Astound your family! Kennify your text!
Quote:Original post by Verminox
How would I go about using std::string for the itneger input?

Should I just use getline() to get the input into the string and then use type casting to convert it to an integer? What will happen if the input is purely text and it is casted to an integer?


You can't cast a std::string to an int. You'd have to use a stringstream or a function like std::atoi to convert it.

Using a std::string would just prevent the potential buffer overflow inherient in Mr Safety's example code.

As F1N1TY pointed out, using cin >> an_int; will return false if it can't convert the input so there shouldn't be any need to make it more complex than that.

However, if you cin to a std::string, that will accept any text and you could then use a variant of Mr Safety's approach to ensure the string contains only numbers before using a stringstream to convert it into an int variable.
Quote:Original post by Verminox
That is no good, isalpha() returns false even if I input punctuation marks, which aren't integers.


This is the
Actually, you should use isdigit(), not isalpha(). From the MSDN:
Quote:isalpha returns a non-zero value if c is within the ranges A – Z or a – z.
.

Regards,
Thinking about it, if you are going to cin to a string then use a custom function to check it is a valid number, just checking to see if everything is a digit is not going to work.

For example, "-1,234" would be a valid integer input that I assume atoi or stringstream would cope with, as well as leading or trailing whitespace.

This topic is closed to new replies.

Advertisement