#include <stdio.h>
#include <stdlib.h>
int getAge(void);
int main(void)
{
int age = 0;
getAge();
printf("The age is %4d\n",age);
system("PAUSE");
return 0;
}
//The function gets the age until it is valid, and returns the vaild age
int getAge(void)
{
const int MAX_AGE = 120;
const int MIN_AGE = 0;
int age = 0;
int invalidAge = 0;
printf("please enter your age (0-120): ");
do
{
scanf("%d", &age);
invalidAge = ( age > MAX_AGE || age < MIN_AGE );
if( invalidAge )
{
printf("Invalid age! Please enter your age (0-120): ");
}
} while ( invalidAge );
printf("Finally!!!\n");
return age;
}
您好,此代码存在运行时问题。谁能告诉我要改变什么? 没有编译错误。
答案 0 :(得分:2)
这里的问题是,在你的代码中,
getAge();
您忘记将getAge();
函数的返回值收集到age
变量中。因此,getAge()
函数的返回值始终被忽略,输出为0。
您需要将代码更改为
age = getAge();
将要存储的函数的返回值存储到age
。
答案 1 :(得分:1)
请在&#34;年龄&#34;中获取返回值变量如下。
int main(void)
{
int age = 0;
age= getAge(); //catch the return value
printf("The age is %4d\n",age);
system("PAUSE");
return 0;
}