dimanche 27 août 2017

Unexpected results (int and if)

This is part of a program that's a simple dice simulator.

int x,y;
cin>>y;
    if(y<=0){cout<<"Invalid input";}
    else{
        for(x=0;x<y;x++){ 
            cout<<rand()%6<<endl;
        }
    }

I added the if(y<=0) to check if the input was a negative number but it also displays the message even if the input is an alphabet,special character or decimal.

I get that it outputs the statement for decimal because it ignores the decimal part of the input.

But why does this happen for special characters and alphabets?

Aucun commentaire:

Enregistrer un commentaire