C++大神们帮忙看下程序哪里出问题了(统计空格制表符和换行符的个数),输入结束时按ctrl+z没反应

//待解决
#include<iostream>#include<string>
using namespace std;
void main()
{
char s;
int space=0,table=0,enter=0;
cout<<"Please input a string:"<<endl;
cin>>s;
while(s!=EOF)
{
switch(s)
{
case 32:space++;break;
case 9:table++;break;
case 10:enter++;break;
default:break;
}
cin>>s;
}
cout<<"The numbers of the SPACE is:"<<space<<endl;
cout<<"The numbers of the TABLE is:"<<table<<endl;
cout<<"The numbers of the ENTER is:"<<enter<<endl;
getchar();
}

第1个回答  2013-04-16
定义的类型有问题 char 只是1个字符长度,你输入一行数据回车后其实它只接受到了第一个字符 就是f
然后ctrl+z 用断点跟踪值为-52 EOF的值是-1
第2个回答  2013-04-16
你的eof符就没有输入进去,要在新的一行输入ctrl+z追问

不明白,怎么做输入呢?本人菜鸟,ctrl z不就表示eof 吗?

追答

1.输入ctrl+z要在一行开始,也就是你打完回车之后
2.结束判断不能用你那个要用while(!cin.eof())
3如果你使用getchar()输入,那么你那样判断没问题。
2,3判断方式不同可能是因为cin在c++中的流封装形式的问题

追问

还是不行,能不能帮我在原来的代码上改改,把改后的代码拷给我看看,麻烦了,谢谢大神。

追答

#include
#include
using namespace std;
void main()
{
char s;
int space=0,table=0,enter=0;
cout<<"Please input a string:"<<endl;
s=getchar();
while(s!=EOF)
{
switch(s)
{
case 32:space++;break;
case 9:table++;break;
case 10:enter++;break;
default:break;
}
s=getchar();
}
cout<<"The numbers of the SPACE is:"<<space<<endl;
cout<<"The numbers of the TABLE is:"<<table<<endl;
cout<<"The numbers of the ENTER is:"<<enter<<endl;
}
cin不识别空格之类的符号,我之前没看你的代码内容,都没注意这个问题。改成getchar读数就好了,记得ctrl+z要在新的一行输入

本回答被提问者采纳
相似回答