该怎么清除cin内剩余的输入内容
#include<iostream>#include<string>
using namespace std;
const int strsize = 100;
struct bop {
string fullname;
char title[strsize];
string bopname;
int preference; //0表示fullname,1表示title,2表示bopname
};
int main() {
bop a[5] = {
{ "张三","金蝉子","唐僧",1 },
{ "李四","石猴","孙悟空",2 },
{ "王五","小白龙","敖烈",1 },
{ "赵六","天蓬元帅","猪八戒", 0},
{ "孙七","卷帘大将","沙和尚",1 } };
char Xuanxiang;
cout << "Benevolent Order of Programmers Report\n"
"a. display by name b. display by title\n"
"c. display by bopname d. display by preference\n"
"q. quit\n";
cout << "Enter your choice:";
do {
cin >> Xuanxiang;
switch (Xuanxiang) {
case 'a': for (int i = 0; i < 5; i++)cout << a[i].fullname << endl;cout << "Next choice:"; break;
case 'b': for (int i = 0; i < 5; i++)cout << a[i].title << endl; cout << "Next choice:"; break;
case 'c': for (int i = 0; i < 5; i++)cout << a[i].bopname << endl; cout << "Next choice:"; break;
case 'd': for (int i = 0; i < 5; i++) {
if (a[i].preference == 1)cout << a[i].title << endl;
else if (a[i].preference == 0)cout << a[i].fullname << endl;
else if (a[i].preference == 2)cout << a[i].bopname << endl;
} cout << "Next choice:"; break;
case 'q':cout << "Bye!"; break;
}
} while ((Xuanxiang == 'a') || (Xuanxiang == 'b') || (Xuanxiang == 'c') || (Xuanxiang == 'd'));
system("pause");
return 0;
}
如上代码,a,b,c,d均能触发do while语句,但是如果输入abbb或者cdb之类“abcd”组成的字符串时怎么能让cin只读取第一位有效输入并把之后的输入舍弃,使下一次的cin能正常输入