Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - If you enter a letter into an integer variable in C++ ...
If you enter a letter into an integer variable in C++ ...
I have studied this problem: basically solved the problem of inputting only numbers;

# include & lt string & gt

# include & ltiostream & gt

Use namespace std

void main()

{

String a;

CIN & gt; & gta;

for(; ; )

{

If (a & lt25 minutes. & ampa & gt"0")

{

if(atof(a . c _ str())= = atoi(a . c _ str()))

Break;

other

{

Cout & lt& lt is entered incorrectly, please re-enter.

CIN & gt; & gta;

}

}

other

{

Cout & lt& lt is entered incorrectly, please re-enter.

CIN & gt; & gta;

}

}

cout & lt& lta & lt& ltendl

int b = atof(a . c _ str());

cout & lt& ltb & lt& ltendl

System ("suspended");

}

You try!

Supplement: In fact, you can think about this problem from another angle, and replace your comnumber with char type, and then judge.

If 17 is input from the keyboard, it is not received by %d, but converted into "17" by %s, which is easy to judge; As for how to judge, I have an example above.