Page 2 of 3
Re: C++ isdigit command
Posted: Sat Feb 28, 2009 3:06 pm
by herby490
Switching the two works Thanks
Re: C++ isdigit command
Posted: Wed Mar 04, 2009 3:34 pm
by herby490
Ok i just realized that if i type a number then a letter the cin still fails how can i stop this
Re: C++ isdigit command
Posted: Wed Mar 04, 2009 6:36 pm
by Ginto8
herby490 wrote:Ok i just realized that if i type a number then a letter the cin still fails how can i stop this
Use atoi() and getline(). I'm pretty sure this would work (just an example, and I didn't see the original code):
Code: Select all
#include <iostream>
#include <string>
using namespace std;
int main()
{
string temp = "";
int x = 0;
cout << "Enter input starting with an integer value: ";
getline(cin, temp);
x = atoi(temp);
cout << "\nYou entered " << x << ".\n";
getchar();
return 0;
}
That should be what you're looking for.
Re: C++ isdigit command
Posted: Thu Mar 05, 2009 6:59 pm
by herby490
I would rather not have to convert it to a number is then any other way to do it?
Re: C++ isdigit command
Posted: Fri Mar 06, 2009 2:03 pm
by Ginto8
Not really.
that's the only way to make it work if a user inputs a number than some set of characters.
sorry if you really don't want to do that.
Re: C++ isdigit command
Posted: Fri Mar 06, 2009 5:49 pm
by herby490
Its hard to believe that is the only way to do it. You would think that this would be a problem that people want to address.
Re: C++ isdigit command
Posted: Sat Mar 07, 2009 3:04 am
by Scoody
Here you go:
Code: Select all
int intval = 0;
while (intval != -1) {
cout << "Number please: ";
cin >> intval;
if (cin.fail()) {
cout << "Something bad happened ..." << endl;
cin.clear(); // clear the failstate
cin.sync(); // clear cin's buffer of nastiness
}
}
This loop will run until intval is equal to -1. You just add your own check in the loop-head that suits your needs.
If the input is something like this: 123 abc lol test haha 321
It will go through every chunk that's separated by a space, and since "abc" isn't valid the buffer will be cleared and "lol test haha 321" gets lost.
But if you only want to read one number at a time you can add cin.sync(); after cin >> intval; and the user will be forced to enter a new number until it meets your criteria.
Re: C++ isdigit command
Posted: Sat Mar 07, 2009 1:02 pm
by MarauderIIC
You know, I didn't even know that
sync() existed. Thanks.
Re: C++ isdigit command
Posted: Sat Mar 07, 2009 2:23 pm
by Kros
MarauderIIC wrote:You know, I didn't even know that
sync() existed. Thanks.
Wow. Me either. What the heck?
Handy.
Re: C++ isdigit command
Posted: Sat Mar 07, 2009 3:10 pm
by Maevik
Another good way to make sure you're getting a number is to use if( cin.fail() ) with a flag loop and a "Try again retard" message that prompts them again.
Code: Select all
int main()
{
double a = double();
bool again = true;
while ( again )
{
cout << "For the quadratic equation, please enter to value for a: ";
cin >> a;
again = false;
if (cin.fail())
{
cout << "Invalid Data, the value for a must be numerical. idiot" << endl;
cin.ignore(1000, '\n');
cin.clear();
again = true;
}
}
//carry on with your program
}
it's a lot of code, but it's effective and it gives you a chance to berrate your user. The cin.fail() function checks to see if the data in the input stream caused a fail state, if it does it tells them they're doin' it wrong, ignore's the next 1000 characters in the stream, or until it gets to a newline (which would normally be the end of the stream anyway ) clears the input stream, and prompts the loop to go back and ask the question again.
Re: C++ isdigit command
Posted: Sat Mar 07, 2009 3:12 pm
by MarauderIIC
Re: C++ isdigit command
Posted: Sat Mar 07, 2009 3:14 pm
by Maevik
Ahh, ok. I didn't realize !cin worked the same way. If you use this method, do you still need cin.clear() and cin.ignore() ?
Re: C++ isdigit command
Posted: Sun Mar 08, 2009 7:55 pm
by herby490
That works if I type something that is a letter or has a letter but now the only number that it will accept is negative 1. I have tried if(isdigit(a)) break;
but it wont work. How can i break the loop if the user entered a valid number?
Re: C++ isdigit command
Posted: Sun Mar 08, 2009 11:21 pm
by MarauderIIC
Did you get the right condition? "while (intval != -1) {"
Essentially it checks to see if intval got assigned to. You could also put a break in an else-condition on Scoody's code.
Maevik, !cin is shorthand for cin.fail() (or !cin.good(), perhaps)
Re: C++ isdigit command
Posted: Tue Mar 10, 2009 7:14 pm
by herby490
Ok I still can't get it to work. I have tried to make it so it would break the loop if the person entered a valid input. Heres the code
Code: Select all
a=0;
while(a!=-1)
{
cout << "Enter an A value: ";
cin >> a;
if(!cin)
{
cout <<"Congratulations you failed the cin statment.\nPlease enter a real number" << endl;
cin.clear();
cin.sync();
}
else if(a!=-1)//Tried those once
break;
else //tried this another time
break;
}
Note i did not use the else and the else if statement in the same program I just put it there for convenience.