//待解决
#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();
}
不明白,怎么做输入呢?本人菜鸟,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要在新的一行输入