#include <stdio.h>
#include <stdlib.h>
int main()
{
int _age;//declare variable
getAge();//calls function
welcomeMessage();//calls function
return 0;
}
int getAge(int _age)//takes input from user and assigns a value to age
{
printf("What is your age?\n");
scanf("%d", &_age);
}
int welcomeMessage(int _age)//prints how old you are
{
printf("Welcome, you are %d years old.\n", _age);
}
我不断得到这个年龄的随机数,我需要知道我为scanf添加了什么
答案 0 :(得分:2)
您的int getAge(int _age)
功能应如下所示:
void getAge(int* _age)
{
printf("What is your age?\n");
scanf("%d", _age);
}
在您的main()
功能中,您需要按以下方式致电int welcomeMessage(int _age)
:
int main()
{
int _age; // declare variable
getAge(&_age); // calls function
welcomeMessage(_age); // calls function
return 0;
}
实际上,更正确的代码版本应如下所示:
#include <stdio.h>
#include <stdlib.h>
void getAge(int*);
void welcomeMessage(int);
int main()
{
int _age;
getAge(&_age);
welcomeMessage(_age);
return 0;
}
void getAge(int* _age)
{
printf("What is your age?\n");
scanf("%d", _age);
}
void welcomeMessage(int _age)
{
printf("Welcome, you are %d years old.\n", _age)
}
答案 1 :(得分:0)
考虑重写getAge()
。请务必检查scanf()
。
int getAge(void) {
int age;
printf("What is your age?\n");
if (1 == scanf("%d", &age)) {
return age;
}
printf("Trouble with input\n");
return 0;
}