八角味的甘草糖 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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?