Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c++ - How to stop my code from reading next character?

I just want to read sign character from user and after this ask whether he/she wants to repeat.

it is something like this:

cout << "Please now enter the sign (+-*/): ";
        cin >> *sign;
        cout <<endl; 

and after some time:

cout << "Do you want to try again? (y - yes, anything else - no): ";
        cin >> *yn;

problem is that if user enters something like "+t" in the first time then 't' is automatically assigned to the second yn character.

So how I can prevent this?

question from:https://stackoverflow.com/questions/65952010/how-to-stop-my-code-from-reading-next-character

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have two options:

  1. Read only one character from std::cin (example)
  2. Process only the first character character, i.e., sign[0]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...