标题:该怎么清除cin内剩余的输入内容
取消只看楼主
gzy444
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2020-1-16
结帖率:100%
已结贴  问题点数:10 回复次数:2 
该怎么清除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能正常输入
搜索更多相关主题的帖子: cin 输入 cout case int 
2020-03-01 17:53
gzy444
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2020-1-16
得分:0 
回复 4楼 叶纤
吱吱吱吱!
2020-03-02 14:02
gzy444
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2020-1-16
得分:0 
回复 2楼 return_0
看楼下版主发的那个
2020-03-02 14:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-499490-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.229840 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved