C++ isdigit command
Moderator: Coders of Rage
Re: C++ isdigit command
Switching the two works Thanks
Re: C++ isdigit command
Ok i just realized that if i type a number then a letter the cin still fails how can i stop this
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: C++ isdigit command
Use atoi() and getline(). I'm pretty sure this would work (just an example, and I didn't see the original code):herby490 wrote:Ok i just realized that if i type a number then a letter the cin still fails how can i stop this
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;
}
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: C++ isdigit command
I would rather not have to convert it to a number is then any other way to do it?
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: C++ isdigit command
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.
sorry if you really don't want to do that.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: C++ isdigit command
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
Here you go:
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.
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
}
}
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: C++ isdigit command
You know, I didn't even know that sync() existed. Thanks.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Kros
- Chaos Rift Regular
- Posts: 136
- Joined: Tue Feb 24, 2009 4:01 pm
- Current Project: N/A
- Favorite Gaming Platforms: PC, Playstation/2/3
- Programming Language of Choice: C++
- Location: Oregon,USA
- Contact:
Re: C++ isdigit command
Wow. Me either. What the heck?MarauderIIC wrote:You know, I didn't even know that sync() existed. Thanks.
Handy.
YouTube ChannelIsaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: C++ isdigit command
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.
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.
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
}
Last edited by Maevik on Sat Mar 07, 2009 3:15 pm, edited 2 times in total.
My love is like a Haddoken, it's downright fierce!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: C++ isdigit command
Maevik, thanks but already posted :) http://elysianshadows.com/phpBB3/viewto ... 402#p35402
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Maevik
- Chaos Rift Junior
- Posts: 230
- Joined: Mon Mar 02, 2009 3:22 pm
- Current Project: www.keedepictions.com/Pewpew/
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: Long Beach, CA
Re: C++ isdigit command
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() ?MarauderIIC wrote:Maevik, thanks but already posted http://elysianshadows.com/phpBB3/viewto ... 402#p35402
My love is like a Haddoken, it's downright fierce!
Re: C++ isdigit command
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?
but it wont work. How can i break the loop if the user entered a valid number?
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: C++ isdigit command
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)
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)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: C++ isdigit command
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
Note i did not use the else and the else if statement in the same program I just put it there for convenience.
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;
}