cin.clear() does not clear standard input. It cleans up error bits such as eofbit , failbit and others, and sets the stream to a good state. Maybe you expected him to take away something in him? If the user typed
yes no
Just before that, and you
cin >> someStringVariable;
It will read to no , and the stream will contain
no
The clear call then clears any error bits that are active. Then,
cin>>addAntonymAnswer1;
Will read no , which was not eaten by the previous reading, and the action returns immediately, without waiting for a new entry. What you need to do is make clear , followed by ignoring, up to the next new line. You specify the number of characters that should be ignored as much as possible. This amount should be as high as possible:
cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Doing this will cause the stream to be empty and the next read will wait for the type to be entered.
Another problem arises if you have cin >> followed by getline : cin will leave any spaces (also new lines) after its read token, but getline will stop reading after it gets on such a new line. I see that you put clear after almost everything. Therefore, I want to show you when you need it and when not. You do not need this when you are doing a few cin >> . Suppose you have in your buffer: "foo \ nbar \ n". Then you do the following readings
cin >> a; // 1 cin >> b; // 2
After the first, your buffer will contain "\ nbar \ n". That is, a new line is still located. The second cin>> first skip all spaces and newlines so that it can handle the \n at the beginning of bar . Now you can also make several getline calls:
getline(cin, a); getline(cin, b);
Getline throws \n which it reads at the end of the line, but will not ignore newlines or spaces at the beginning. So, after the first getline, the buffer contains "bar \ n". The second line of getline will read "bar \ n" correctly. Now consider the case where you need clear / ignore:
cin >> a; getline(cin, b);
The first will leave the stream as "\ nbar \ n". Then getline will immediately see \n at the beginning and will assume that it is reading an empty line. Thus, it will continue immediately and wait for nothing, leaving the stream "bar \ n". So, if you have getline after cin>> , you must first run the clear / ignore sequence to clear the new line. But between getline or cin>> 's you shouldn't do this.