如何读取到的空格、TAB键和换行符

ck_chuyun 2002-08-23 03:50:54
#include <iostream>
using namespace std;

void main()
{
char ch;
int space = 0, tab = 0, lf = 0;

cin >> ch;

switch (ch)
{
case ' ':
++space;
break;
case '\t':
++tab;
break;
case '\n':
++lf;
break;
}

cout << space << endl;
cout << tab << endl;
cout << lf << endl;
}
这个程序怎样改才能记录被读取到的空格、TAB键和换行符的个数????
...全文
256 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
charlslu 2002-08-23
  • 打赏
  • 举报
回复
我非常赞同yousp的做法。
ck_chuyun 2002-08-23
  • 打赏
  • 举报
回复
SORRY,全通过了,不过还有点不完善
ck_chuyun 2002-08-23
  • 打赏
  • 举报
回复
出错了

E:\源代码\FIRST\first.cpp(1) : error C2143: syntax error : missing ';' before '<'
E:\源代码\FIRST\first.cpp(1) : error C2501: 'include' : missing storage-class or type specifiers
E:\源代码\FIRST\first.cpp(1) : error C2143: syntax error : missing ';' before '<'
E:\源代码\FIRST\first.cpp(30) : error C2065: 'cout' : undeclared identifier
E:\源代码\FIRST\first.cpp(30) : error C2065: 'endl' : undeclared identifier
E:\源代码\FIRST\first.cpp(30) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
E:\源代码\FIRST\first.cpp(31) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
E:\源代码\FIRST\first.cpp(32) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
yousp 2002-08-23
  • 打赏
  • 举报
回复
兄台的程序只顺序执行了一次,再怎么样也只能读一个字符呀,呵呵
程序我把它改成下面的样子了,VC6编译通过
#include <iostream>
using namespace std;

void main()
{
char ch;
int space = 0, tab = 0, lf = 0;

//这里使用一个缓冲存放需要记录的内容
char * buffer = "1 2 3 4 5 1 \n";

while (ch != '\0')
{
switch (ch)
{
case ' ':
++space;
break;
case '\t':
++tab;
break;
case '\n':
++lf;
break;
}
//往下扫描
ch = * buffer++;
}

cout << space << endl;
cout << tab << endl;
cout << lf << endl;
}

如果使用cin,则buffer缓冲可以这样获得:
#include <iostream.h>
#include <strstrea.h>

void main()
{
char ch;
int space = 0, tab = 0, lf = 0;

//char * buffer = "1 2 3 4 5 1 \n";

const BUFFERSIZE = 200;
char buffer[BUFFERSIZE];
char * p = buffer;

cin.getline(p, BUFFERSIZE);
ch = * p;
while (ch != '\0')
{
switch (ch)
{
case ' ':
++space;
break;
case '\t':
++tab;
break;
case '\n':
++lf;
break;
}
ch = * p++;
}

cout << space << endl;
cout << tab << endl;
cout << lf << endl;
}
不过现在只能获取一行的数据,而且数据的个数也受缓冲区大小BUFFERSIZE的限制

还可以改进,自己做吧!

jyc_nj 2002-08-23
  • 打赏
  • 举报
回复
basic_ios::operator >>(char c) 这类都是返回付合格式要求的内容,所以,这里是不行的.
用cin.read(&ch,1)或特给char准备的cin.get()
operator<<
和write的区别亦是如此.
ck_chuyun 2002-08-23
  • 打赏
  • 举报
回复
都没用!!!!!!!!!
天外飞狐 2002-08-23
  • 打赏
  • 举报
回复
在switch外面包一个while(cin>>ch){}
然后象你这样就行了

应该自己多试验,不要什么都问
step_by_step 2002-08-23
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

void main()
{
char ch;
int space = 0, tab = 0, lf = 0;

cin >> ch;

switch (ch)
{
case ' ':
++space;
break;
case '\t':
++tab;
break;
case '\r'://加上这一行!!!!!!!!!!!!!!!!!
case '\n':
++lf;
break;
}

cout << space << endl;
cout << tab << endl;
cout << lf << endl;
}
zyzl 2002-08-23
  • 打赏
  • 举报
回复
不需要再改了吧,这样就可以了

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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