Dht~0 2018-12-31 14:17 采纳率: 100%
浏览 576
已采纳

如何用简短的代码写出string为空就跳出循环,请好心大佬指点。。

#include <iostream>
#include <string>
using namespace std;
void strcount(const string);
int main()
{
    string input;
    cout<<"enter a line:"<<endl;
    cin>>input;
    while(!input.empty())
    {
        while(cin.get()!='\n') continue;
        strcount(input);
        cout<<"enter next line (empty line to quit):"<<endl;
        cin>>input;
    }
    cout<<"bye"<<endl;
    return 0;
}
void strcount(const string str)
{
    static int total=0;
    int count=0;
    cout<<"\""<<str<<"\"contains ";
    count=str.length();
    total+=count;
    cout<<count<<"characters"<<endl;
    cout<<total<<"characters total"<<endl;
}

如何能用简短的语句达到输入为空就跳出循环呢...执行完一次以后我敲回车只是光标换行,如何能将string类的对象设置为空?我用C-free和DEV都试过了都是这个样子图片说明

  • 写回答

2条回答 默认 最新

  • Dht~0 2018-12-31 11:22
    关注

    经过本菜叽不懈研究,发现了如果用cin是怼不进去输入缓冲区队首的空格以及回车的,需要用getline才行所有问题解决了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑