京城皮肤病医院治痘痘:看我这代码哪里错了

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/03 07:55:05
在TC可以通过.但是到了VC6.0显示有一处错误

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
main()
{
int a=0,b=0,c=0;
randomize();
a=rand()%1000+1;
b=rand()%1000+1;
c=a+b;
printf("%d+%d=",a,b);
scanf("%d",&c);

if(a+b==c)
printf("That right");

else
printf("wrong\nc=%d\n",a+b);

}
把在VC++6.0可以执行的代码发出来
我给分了

randomize()函数没有定义
vc6的标准库里没这个函数。
如果想随机初始化,要改成:srand( (unsigned)time( NULL ) );

代码如下(有一个warning,因此在main前加个void比较好):

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void main()
{
int a=0,b=0,c=0;
srand( (unsigned)time( NULL ) );
a=rand()%1000+1;
b=rand()%1000+1;
c=a+b;
printf("%d+%d=",a,b);
scanf("%d",&c);

if(a+b==c)
printf("That right");

else
printf("wrong\nc=%d\n",a+b);

}

int a=0,b=0,c=0
这里错了

错误在于:需要在main()函数里最后写上 return 0;
或在main()前边加上void

我也同意上一位的意见!
你的主函数必须加void或者加上返回值

//我喜欢这样写:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>

void main()
{
int a,b,c,d;
srand( timeGetTime( NULL ));
a=rand()%1000+1;
b=rand()%1000+1;
c=a+b;
std::cout<< a <<"+"<< b << "=" << c << std::endl;
std::cin >> d ;
c == d ? std::cout << "that right" << std::endl : std::cout << "wrong" << std::endl << "c=" << c << std::endl;

return 0;
}