while(cin>>a)怎么才能按回车键结束循环

不思jo 2015-09-04 12:11:14
#include<iostream>
#include<string>
using namespace std;
int main()
{
int arr[100];
int temp;
int count=0;
while(cin>>temp)
{
arr[count]=temp;
count++;
}
}
我要输入一个数组,但是数组不知道确切大小,比如我输入1 2 3 4然后按回车键却不能结束,怎样才能按回车键结束呢??
...全文
3715 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
www_adintr_com 2015-09-04
  • 打赏
  • 举报
回复 1
用 Ctrl+Z 来结束
二班的码农 2015-09-04
  • 打赏
  • 举报
回复
用getchar,不过得将整形改成char型 (ch = getchar()) != '\n'
  • 打赏
  • 举报
回复
#include<iostream>
#include<string> 
using namespace std;
int main()
{
int arr[100];
int temp;
int count=0;
char c;
while(((cin>>temp).get(c)))
{
	
	arr[count]=temp;
	if(c=='\n')
		break;
    count++;
	if(count>=100)
		break;
}
for(int i=0;i<=count;i++)
	cout<<arr[i];
system("pause");
return 0;

}
已测试完毕
mujiok2003 2015-09-04
  • 打赏
  • 举报
回复
http://ideone.com/FaA3m2
lm_whales 2015-09-04
  • 打赏
  • 举报
回复
用 getline 函数,可以保证回车结束输入
paschen 版主 2015-09-04
  • 打赏
  • 举报
回复
用getchar,cin按回车结束不了,同时cin不支持输入空行

65,193

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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