-
Variable Type Problems
I know that this question has been answered before but I have searched all over and not found what I'm looking for. I'm writing a console application. We all know how much the computer hates it when you input a character when you should have inputted some type of number. I would like to know how to avoid this... Example
Code:
...
int I;
cin>>I;
if (A character was put in){
start over
}
else {
Its all good
}
I'm sure someone else has asked this before but I searched for a long time and didn't find what I was looking for... Thanks for your help!
-
There is a funciton called isalpha() that may be what you are looking for.
Code:
do
{
c=getch();
}while( ! isalpha(c) )
-
I still don't understand...
In your example, of what variable type is "c".
And, I'm not sure if this matters, but I'm using "cin" not getch();
-
Well, fgets() has always worked for me :)
Why not try this:
Code:
int iget(int max_buff = 100) {
char buff[max_buff ];
return atoi(fgets(buff, max_buff , stdin));
}
int main() {
int num;
do{
num = iget();
if(num != 0) //...user pressed enter, but inputted nothing...
break;
}while(1);
return 0;
}
-
So what you are saying is that there is no possible way to avoid this problem using std::cin? I don't get the other examples, I just finished learning the basics of the language.
-
Of course you can over-ride cin, but I haven't tried that yet...but look:
Code:
class my_cin {
public:
char buffer[10000];
int operator >> (int num) {
return num = atoi(fgets(buffer, 10000, stdin));
}
float operator >> (float num) {
return num = atof(fgets(buffer, 10000, stdin));
}
long operator >> (long num) {
return num = atol(fgets(buffer, 10000, stdin));
}
double operator >> (double num) {
return num = atod(fgets(buffer, 10000, stdin));
}
char operator >> (char c) {
return c = getch();
}
char * operator >> (char str[]) {
return fgets(str, 10000, stdin);
}
char * get( char str[], int max ) {
return fgets(str, max, stdin);
}
};
Now declare a global input object (like cin)...
my_cin inn;
int main() {
char c;
int i;
double d;
char string[100];
inn >> string;
inn >> d;
//...or even:
char ch = inn >> c; //...get c, and also assign value to ch.
return 0;
}
-
look into the istream methods called good(), bad(), fail(), and clear() and then you could try something like this;
Code:
int num;
int flag = 0;
while(flag == 0)
{
cout << "enter a num" << endl;
cin >> num;
if(!cin.good())
{
cin.clear();
cout << "you did not enter an integer, try again. " << endl;
}
else
flag = 1;
}
It should prevent entering a char when requesting an int but it may not detect entering a double instead of an int (it may just truncate the double at the decimal point, leaving the decimal point and fraction in the input buffer). It also won't prevent the user from entering 12345567789 which may also screw things up. But it is one of the things you can try.
-
Quote:
Originally posted by Sebastiani
Why not try this:
Code:
int iget(int max_buff = 100) {
char buff[max_buff ];
return atoi(fgets(buff, max_buff , stdin));
}
Because it won't work.
The size of arrays have to be constant
Use elad's method with a little modification: Add ignore(10000) after cin.clear().
But if you're looking for a complete function, try this:
It won't decrease functionality of cin like declaring a new class will.
(custom classes might override istream::operator>>)
Code:
//nice, safe reading function
template<typename T> bool get(T& t)
{
using std::cin;
cin>>t;
if (cin.eof())
cin.clear();
else if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (cin.bad())
cin.clear();
else
{
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return true;
}
return false;
}
-
Very good point Sang-Drax. Only poeple like me who hate the STL should be writing their own input classes :D
But very good code example...