八角味的甘草糖 2020-04-15 00:48 采纳率: 100%
浏览 173
已采纳

cin缓冲区清理,主体程序被强制结束循环

做练习题,每一道小题用函数写,并在主题程序中用循环结构调用。但是两个练习会要求输入字符 并多于需求。
cin缓冲区任然存在遗留的字符开头就是空字符,直接导致主程序结束,或者陷入死循环。

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int p5_8();
int p5_9();

int main()
{
    int num;
    cout << "code to use: ";
    cin >> num;
    while (num > 0)
    {
        if (num == 1)
            p5_8();
        else if (num == 2)
            p5_9();
        cout << "code to use: ";
        //cin.get();
        cin >> num;
    }
    return 0;
}

int p5_8()
{
    char word[100];
    int count = 0;
    cout << "Enter words (to stop, type the word done):" << endl;
    cin >> word;

    while (strcmp(word, "done") != 0)
    {
        if (bool(cin >> word) == true)
            count++;
    }

    cout << endl << "You entered a total of " << count << " words." << endl;
    //cin.sync();
    return 0;
}

int p5_9()
{

    string word;
    int count = 0;
    cout << "Enter words (to stop, type the word done):" << endl;
    cin >> word;

    while (word != "done")
    {
        if (bool(cin >> word) == true)
            count++;
    }

    cout << "You entered a total of " << count << " words." << endl;
    //cin.sync();
    return 0;
}

图片说明

尝试使用cin.sync()清理也没有效果。
请问如何解决问题,使主程序能够正常循环

  • 写回答

2条回答 默认 最新

  • 白色一大坨 2020-04-15 09:36
    关注

    你这个题目设计是用done这个词做结尾,如果不想这样,就得做修改,改规则,如果是输入一行以回车当结尾,这样做:

    #include <iostream>
    #include <cstring>
    #include <string>
    using namespace std;
    
    void p5_8();
    void p5_9();
    
    int main()
    {
        int num;
        cout << "code to use: ";
        cin >> num;
        while (num != 0)
        {
            if (num == 1)
                p5_8();
            else if (num == 2)
                p5_9();
            cout << "code to use: ";
            cin >> num;
        }
        return 0;
    }
    
    void p5_8()
    {
        char word[100];
        int count = 0;
        cout << "Enter words (to stop, type the word done):" << endl;
        cin >> word;
    
        while (strcmp(word, "done") != 0)
        {
            if (bool(cin >> word) == true)
                count++;
        }
    
        cout << endl << "You entered a total of " << count << " words." << endl;
        cin.clear();
        cin.sync();
    }
    
    void p5_9()
    {
    
        string word;
        int count = 0;
        cout << "Enter words (to stop, type the word done):" << endl;
        cin >> word;
    
        while (word != "done")
        {
            if (bool(cin >> word) == true)
                count++;
        }
    
        cout << "You entered a total of " << count << " words." << endl;
        cin.clear();
        cin.sync();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件