C++ isdigit command

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: C++ isdigit command

Post by herby490 »

Switching the two works Thanks
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: C++ isdigit command

Post by herby490 »

Ok i just realized that if i type a number then a letter the cin still fails how can i stop this
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: C++ isdigit command

Post 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. ;) :mrgreen:
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.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: C++ isdigit command

Post by herby490 »

I would rather not have to convert it to a number is then any other way to do it?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: C++ isdigit command

Post 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. :(
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.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: C++ isdigit command

Post 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.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: C++ isdigit command

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: C++ isdigit command

Post by MarauderIIC »

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.
User avatar
Kros
Chaos Rift Regular
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

Post by Kros »

MarauderIIC wrote:You know, I didn't even know that sync() existed. Thanks.
Wow. Me either. What the heck?

Handy.
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
User avatar
Maevik
Chaos Rift Junior
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

Post 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.
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!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: C++ isdigit command

Post by MarauderIIC »

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.
User avatar
Maevik
Chaos Rift Junior
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

Post by Maevik »

MarauderIIC wrote:Maevik, thanks but already posted :) http://elysianshadows.com/phpBB3/viewto ... 402#p35402
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() ?
My love is like a Haddoken, it's downright fierce!
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: C++ isdigit command

Post 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?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: C++ isdigit command

Post 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)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: C++ isdigit command

Post 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.
Post Reply