大熊猫和考拉哪个珍贵:谁能找出这段C程序的错误

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/08 14:58:18
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
FILE *fp;
char str[10];
if((fp=fopen("statistics.txt","w+"))==NULL)
{
printf("cannot open file \n\n");
exit(1);
}
do{
printf("please select function:input(1),statistics(2)\n"); //请输入信息
gets(str);
if(*str!='\n')
{
strcat(str,"\n");
fputs(str,fp); //把信息读进文件
}
}while(*str!='\n');
printf("\n\ndesplaying contents of file statistics\n\n");
rewind(fp);
while(!feof(fp)); //feof检查文件是否结束
{
fgets (str,101,fp); //获得字符串,它是要求让你输入的字符最大数为100个
printf("%s\n",str);
}
fclose(fp);
exit(1);
}
void tj()
{
char c;
int letters=0,space=0,digit=0,others=0; //变量分别为字母,空格,数字,其它等个数
printf("please input some characters\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("char=%d\nspace=%d\ndigit=%d\nothers=%d",letters,space,digit,others);
}

if(*str!='\n')改为
if(*str!='\0')
while(*str!='\n');改为
while(*str!='\0');

错误信息呢????

while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;