954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

scanf acting weird at invalid input

I've searched the forums a bit but I couldn't really find anything relative to this matter.

I'm relatively new to C++, and I always used cout and cin. But I find the syntax style a bit off, so I was searching for alternatives and found scanf, sscanf and well printf.

Now I think I understand how scanf works, but what I don't understand is why it continues to execute the program when It couldn't find the type it was searching for in the first place.

Here's the code.

int main(){
        int  players;
        int  sblind;
        int  tchips;
        char uname[16];

        do{
            printf( "How many players are going to enter the game? (min = %d, max = %d)\nPlayers: ", MIN_PLAYERS, MAX_PLAYERS );
            scanf( "%d[int]", &players );
        } while( players < MIN_PLAYERS || players > MAX_PLAYERS );

        do{
            printf( "What will be the value of the small blind? (min = %d)\nSmall Blind: ", MIN_BLIND );
            scanf( "%d", &sblind );
        } while( sblind < MIN_BLIND );

        do{
            printf( "How many chips does each player begin with? (min = %d)\nChips: ", MIN_BLIND * MIN_CHIPSMP );
            scanf( "%d", &tchips );
        } while( tchips < MIN_BLIND * MIN_CHIPSMP );


        CTable table (players);

        printf( "\n" );

        for( int i = 1; i <= players; i++ ){
            do{
                printf( "What is the name of player %d? (max length = %d)\nUsername: ", i, MAX_UNAMELEN );
            } while( scanf( "%s", &table.players[i].uname ) > MAX_UNAMELEN );
        }

        return 0;
    }


Now it works when I just enter the value scanf expects, but how can I prevent invalid input?

Xorifelse
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

By not using scanf() . You need to read the entire input as a string and validate each character. If it validates properly, convert the input to an integer.

Look at fgets()

Actually, using C++ you want getline()

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

By not using scanf() . You need to read the entire input as a string and validate each character. If it validates properly, convert the input to an integer.

Look at fgets()

Actually, using C++ you want getline()

I've searched up fgets() , and on most of the websites I read that this is an un-save function which can return faulty values.

Also getline() is a member of iostream, which I planned "not" to use because of the weird syntax.

There aren't any other ways to do this? It's just weird for me since I always worked with language which where type-safe.

Xorifelse
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
I've searched up fgets() , and on most of the websites I read that this is an un-save function which can return faulty values.

Either you searched wrong websites, or you misread them. Most likely they were talking about gets() (without a leading f) which is unsafe to the extent ofnever ever use it. On the other hand, fgets() is perfectly safe and sound.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 
I've searched up fgets() , and on most of the websites I read that this is an un-save function which can return faulty values.


Whatnezachem said...

Also getline() is a member of iostream, which I planned "not" to use because of the weird syntax.


What's weird about it?There aren't any other ways to do this? It's just weird for me since I always worked with language which where type-safe.
Yeah, but thesyntax is even weirder -- and harder...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: