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?

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()

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.

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 of never ever use it. On the other hand, fgets() is perfectly safe and sound.

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.

What nezachem 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 the syntax is even weirder -- and harder...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.