Page 1 of 3

C++ isdigit command

Posted: Wed Feb 25, 2009 9:18 pm
by herby490
I'm trying to make a program in which the user must enter a number and if the user enters a letter by accident the program gets screwed up. I'm trying to use the isdigit function to make sure that its a number and that fragment of my program looks like this

Code: Select all

if(!(isdigit(a)))
goto here;
Whenever i try to use the program and enter a letter my program still gets screwed up but if i take the not out and enter a number it works fine. Oh and a is a double if that plays any part.

Re: C++ isdigit command

Posted: Wed Feb 25, 2009 9:32 pm
by MarauderIIC
isdigit checks single digits only, not whole numbers. If the user inputs a digit, it stores it in the double. If the user inputs something that can't be stored in a double, like a letter, cin fails silently and you have to cin.clear and cin.ignore('\n', some large number) [or maybe it's some large #, \n] so that you clear the failure and then ignore the invalid input.

You can check for failure using if (!cin).

Edit: Sorry for the brief reply, sort of tired. Hopefully that's enough to get you going with google or forum search.

Re: C++ isdigit command

Posted: Wed Feb 25, 2009 9:36 pm
by herby490
How would i use the (!cin)

Re: C++ isdigit command

Posted: Wed Feb 25, 2009 11:14 pm
by MarauderIIC

Code: Select all

double decimalnum;
bool gotValidInput = false;
while (!gotValidInput) { //loop until we get some valid input
    cout << "enter a #: ";
    cin >> decimalnum; //if a non-number (this includes decimal #s since we're using a double) is entered, cin will go to a fail state
    //in other words if the input we got doesn't match the variable type, cin goes into a fail state.
    //so if we had an int type and a double was entered, it would fail. i think. or maybe it would truncate the decimal off. i dunno

    if (!cin) { //cin has entered a fail state. when cin is in a fail state all cin statements will get skipped
        cout << "that's not a number" << endl; //tell the user they suck
        cin.clear(); //get rid of the fail state flag
        cin.ignore(900, '\n'); // 900 is the # of invalid characters to ignore, i just picked some number out of the air
    } else {
        gotValidInput = true; //cin's not in a fail state, so we must have gotten valid input
    }
}

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 11:59 am
by Frosty
a Goto statement should Never be used

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 12:07 pm
by avansc
Frosty wrote:a Goto statement should Never be used
see this is what angers me. you learnt that from one of your subpar professors or some shitty online text.

i agree that goto should not be used to get out of a programatical error, by gotos are perfectly fine to use.
if you program in assembly you use them all the time.

GOTO's are fine, there is nothing wrong with them, the problem is when shitty programers use them, thats when shit happens.

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 12:12 pm
by Frosty
avansc wrote:
Frosty wrote:a Goto statement should Never be used
see this is what angers me. you learnt that from one of your subpar professors or some shitty online text.

i agree that goto should not be used to get out of a programatical error, by gotos are perfectly fine to use.
if you program in assembly you use them all the time.
Agreed that goto statements is fine when used in good context
But unless you're extremely keen., you'll misuse it and that will lead to some nasty bugs
and breaks in the flow of your program.

PS - damn my subpar professors :)

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 12:14 pm
by M_D_K
avansc wrote:
Frosty wrote:a Goto statement should Never be used
see this is what angers me. you learnt that from one of your subpar professors or some shitty online text.

i agree that goto should not be used to get out of a programatical error, by gotos are perfectly fine to use.
if you program in assembly you use them all the time.

GOTO's are fine, there is nothing wrong with them, the problem is when shitty programers use them, thats when shit happens.
Damn straight!

I use the goto. In fact that was what I used before I figured out the syntax of VB for statements. My teacher said "you shouldn't really use goto..." well it worked.
I should mention that this was after I had blocked out my previous experience with VB.

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 12:16 pm
by avansc
the main reason that gotos are "banned" is not because of bugs, although thaey can cause some ugly ones.
gotos are banned because of a much more sinister and lazy reason, gotos lead to spaghetti code,
and if the original coder gets fired, quits or dies, the other coders are left trying to fix something they really cant because they dont know why tyhe program works like it does.
and inevetably have to recode the entire program.

note, proper use of gotos can significantly decrease the size of a program.

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 1:12 pm
by eatcomics
Frosty wrote:a Goto statement should Never be used
That's what I keep telling ryan lol :lol:

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 5:22 pm
by herby490
Thanks for the advice about the goto command. when is the proper time to use it

Re: C++ isdigit command

Posted: Thu Feb 26, 2009 5:44 pm
by herby490
And now about the code. I typed everything almost the as I was told but when i type in a letter my program enters the "I dont know what to do" state and just repeats the cout commands in the while loop heres my code

Code: Select all

  
    double a;
    bool input_a;
   
    input_a= false;
	while(!input_a)
	{
		cout << "Enter an A value: ";
		cin >> a;
		
		if(!cin)
		{
			cout <<"Please enter a real number" << endl;
			cin.ignore(900,'\n');
			cin.clear();
		}
		else
		{
			input_a = true;
		}
	}
and sorry i made two post i tested it after i made my first post

Re: C++ isdigit command

Posted: Fri Feb 27, 2009 12:20 pm
by MarauderIIC
Gotos are one of the cleanest ways to exit nested loops. Why did we even start talking about gotos?

Hmm herby, this is what happens when I program straight into the forum. Try swapping the order of ignore and clear, that's the way I'm seeing it from google hits.

ie, instead of

Code: Select all

cin.ignore(...)
cin.clear()
try

Code: Select all

cin.clear()
cin.ignore(...)
Do that first. If it still doesn't work, try changing (!cin) to (cin.fail())

Re: C++ isdigit command

Posted: Fri Feb 27, 2009 1:36 pm
by Ginto8
MarauderIIC wrote:Gotos are one of the cleanest ways to exit nested loops.
:| wouldn't break be best? Unless "nested" loops are different from normal ones...

Re: C++ isdigit command

Posted: Fri Feb 27, 2009 1:51 pm
by MarauderIIC
Try getting out of

Code: Select all

for (int i = 0;i < something;++i) {
    cout << "initial processing start" << endl;
    if (errorInitial) { display only this error; get out of all loops; }
    for (int j = 0; j < something;++j) {
        cout << "mid processing start" << endl;
        if (errorMid1) { display only this error message; get out of all loops; }
        for (int k = 0;k < something;++k) {
            cout << "mid processing 2 start" << endl;
            if (errorMid2) { display only this error message; get out of all loops; }
        }
        cout << "mid processing 3 start" << endl;
        if (errorMid3) { display only this error message; get out of all loops; }
    }
    cout << "post processing start" << endl;
    if (errorPost) { display only this error; get out of all loops; }
}
You could use break, but you'd have to check for every single relevant error flag before every single processing is done -- added to the ifs that are already there -- since break; will only break its local loop, OR you could put

Code: Select all

goto nestedParser_escape;
in place of "get out of all loops" and then do

Code: Select all

}//end of outermost loop
nestedParser_escape:
//go on to next task
So in general, yes, don't use a goto. But it does have its specific uses. Don't use a goto if there's something else that can do the job as well or better (better includes clarity). "Never" is never true in programming. Ha that's a neat unintentional pun.