新手请教,请求改正程序错误[02]!

jyh070207 2010-09-16 09:58:05
新手请教,请求改正程序错误[02]!

初次接触Visual C++ 6.0,以下是考试例题,但编译出错,请帮忙修改!

//题目
编写一个函数fun(str),它的功能是:统计由实参传来的一个字符串中的字母,数字,空格和其它字符号个数,
部分源程序给出如下,请勿改动主函数main和其它函数中的任何内容,
仅在函数fun的花括号中填入你编写的若干语句.

程序版本:Visual C++ 6.0

程序代码如下:
#include "stdafx.h"
int alph,digit,space,others;
void main()
{
char text[80];
printf("\nEnter a string:");
gets(text);
printf("it is:");
puts(text);
alph=digit=space=others=0;
fun(text);
printf("\n alph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others);
}

fun(str)
char str[];
{int i,temp;
for(i=0;str[i]!='\0';i++)
{temp=str[i];
if(isalpha(str[i])) alph++;
else if(isdigit(temp)) digit++;
else if(isspace(temp)) space++;
else others++;}
return alph,digit,space,others;
}


以下是编译出错信息!
-------------------Configuration: chapter7 - Win32 Debug--------------------
Compiling...
chapter7.cpp
E:\xuexi\vc++\example\chapter7\chapter7.cpp(14) : error C2065: 'fun' : undeclared identifier
E:\xuexi\vc++\example\chapter7\chapter7.cpp(18) : error C2065: 'str' : undeclared identifier
E:\xuexi\vc++\example\chapter7\chapter7.cpp(19) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
E:\xuexi\vc++\example\chapter7\chapter7.cpp(19) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.

chapter7.exe - 1 error(s), 0 warning(s)
...全文
87 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2010-09-16
  • 打赏
  • 举报
回复
基础错误啊。。。
gameslq 2010-09-16
  • 打赏
  • 举报
回复
1.fun函数要有返回值修改下定义
int fun(str) char str[] 你这是定义函数的老语法,改成新语法:

int fun(char str[]) 或者这样 int fun(char *str);
2.在main函数之前 对 fun函数声明下
int fun(char str[]);
jyh070207 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dingshaofengbinbin 的回复:]
你return的真有意思。。。。函数返回值都没有!!!
[/Quote]

dingshaofengbinbin:你好!
按你所说的修改,编译也不成功,我不是用vc++的,只是这次考试有几个相关的题目,所以想先调试出来.
原题如下:
编写一个函数fun(str),它的功能是:统计由实参传来的一个字符串中的字母,数字,空格和其它字符号个数,部分源程序给出如下,请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句.
#include<stdio.h>
Int alph, digit, space, others;
Main()
{ char text[80];
Printf(“\n 请输入一个字符串:\n”);
gets(text);
printf(“字符串是:”);
puts(text);
alph = digig = space = others = 0;
fun(text)
printf(“\n %d个字母,%d个数字,%d个空格,%d个其它字符. \n”,alph, digit,space,others);
}

Fun(str)
Char str[]
{…………..
}

按你所说修改后,出错信息如下:
--------------------Configuration: chapter7 - Win32 Debug--------------------
Compiling...
chapter7.cpp
E:\xuexi\vc++\example\chapter7\chapter7.cpp(7) : error C2065: 'str' : undeclared identifier
E:\xuexi\vc++\example\chapter7\chapter7.cpp(9) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
E:\xuexi\vc++\example\chapter7\chapter7.cpp(28) : error C2065: 'fun' : undeclared identifier
执行 cl.exe 时出错.

chapter7.exe - 1 error(s), 0 warning(s)

dingshaofengbinbin 2010-09-16
  • 打赏
  • 举报
回复
你return的真有意思。。。。函数返回值都没有!!!
dingshaofengbinbin 2010-09-16
  • 打赏
  • 举报
回复
fun(str)
char str[];
{int i,temp;
for(i=0;str[i]!='\0';i++)
{temp=str[i];
if(isalpha(str[i])) alph++;
else if(isdigit(temp)) digit++;
else if(isspace(temp)) space++;
else others++;}
return alph,digit,space,others;
}
main函数前面先声明这个函数,还有fun(str)
char str[];去掉

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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