这样对吗?

starcat 2010-05-30 11:15:26
做一个习题
/*
Display a file a line at a time, waiting for the user to press
the "Enter" key after each line.
*/

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

int main() {
vector<string> lines;
ifstream in("filename.cpp");
string line;
while(getline(in, line))
lines.push_back(line);
char ch;
for(int i = 0; i < lines.size(); i++) {
cout << lines[i];
cin.get();
cout << endl;
}
}

不知符不符合题意
不想用C的方法getchar()来判断回车,求指点
...全文
116 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
starcat 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gjlzjb 的回复:]
楼主你用“cin.get()”说明是任意字符都可以,而题目要求是回车,所以我做了一些改动。只能用回车,你看是否合理。
[/Quote]谢谢
奇怪的是,我的写法输入tab,或者space都没反应
难道跟编译器有关?(BCC5.5)
gjlzjb 2010-05-31
  • 打赏
  • 举报
回复
楼主你用“cin.get()”说明是任意字符都可以,而题目要求是回车,所以我做了一些改动。只能用回车,你看是否合理。

/*
Display a file a line at a time, waiting for the user to press
the "Enter" key after each line.
*/

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

int main() {
vector<string> lines;
ifstream in("filename.cpp");
string line;
while(getline(in, line))
lines.push_back(line);
char ch;
for(int i = 0; i < lines.size(); i++) {
cout << lines[i];
//cin.get(); 原来你这样意思是任意字符都可以,题目要求是回车
while(cin.get(ch))//获取一个字符
{
if(ch=='\n') break;//判断是否是回车
}
//cout << endl;
}
}
liutengfeigo 2010-05-31
  • 打赏
  • 举报
回复

UP

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧