注册 登录
编程论坛 C++教室

求一个用while循环输入字符,然后输出的程序谢谢

newCpp 发布于 2009-08-31 17:01, 4054 次点击
求一个用while循环输入字符,然后输出的程序谢谢
我试了很多次都没有成功
将其发现一问题,为什么用while循环输入一个字符数组
输入的字符数,竟然会变得不受气限制了,这个是为什么
难道字符数组不存在下标我晕
我那个字符数组我只不过定义了10个元素而已,
他输入和输出竟然变得不受其约束了。
这个是为什么啊!!谢谢,谁能帮忙弄一下下啊!
非常地感谢
5 回复
#2
newCpp2009-08-31 17:13
#include<iostream>
using namespace std;
int main()
{
    char h;
int d;
while((h=getchar())!='\n')
{
    cout<<h<<endl;
}
}

可不可以不用这种,如果将char 后面的h定义成数组类型的该如何
让他输入回车后结束,
或者是让他在满足元素个数后,就输入不了
#3
wghost2009-11-04 11:00
你试着按两次回车,看看可以停止不!
为什么一定要这么做呢?
#include<iostream>
using namespace std;
const int n=10;
int main()
{
  char s[n];
  int i=0;
  while(a[i++]=getchar()&&i<n)
   { }
 cout<<a<<endl;
}
这么做试试!





#4
bhbh2009-11-04 11:16
  为什么这样输入呢?
#5
pywepe2009-11-04 17:07
用 cin.get()
#6
pywepe2009-11-04 17:13
#include<iostream>
using namespace std;
int main()
{
    char h;
int d;
while((h=getchar())!='\n')
{
    cout<<h<<endl;
}
}

这个程序没有问题呀

回车后开始从流读 读到\n就结束读


[ 本帖最后由 pywepe 于 2009-11-4 17:33 编辑 ]
1