习题3.5 实现从标准输入每次读入一行文本。然后改写程序,每次读入一个单词。

2022/1/4 22:33:25

本文主要是介绍习题3.5 实现从标准输入每次读入一行文本。然后改写程序,每次读入一个单词。,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.每次读入一个单词

#include<iostream>
#include<string>
using namespace std;
int main(){
    string word;
    int n=1;
    while(cin>>word){
        cout<<n<<": "<<word<<endl;
        n++;
    }
    cout<<endl;
    system("pause");
    return 0;
}

2.每次读入一行文本

#include<iostream>
#include<string>
using namespace std;
int main(){
    string line;
    int n=1;
    while(getline(cin,line)){
        cout<<n<<": "<<line<<endl;
        n++;
    }
    cout<<endl;
    system("pause");
    return 0;
}

 



这篇关于习题3.5 实现从标准输入每次读入一行文本。然后改写程序,每次读入一个单词。的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程