0
源纲要:用户选择选项要么1.使布加迪; 2.创建布加迪;或者3.退出程序。每个选项完成后,用户应该返回菜单选择另一个选项。参数传递不正确返回主
(注意:用户不能在壳体显示汽车被创建,直到它,因此if
声明2)
的问题:没有被返回到main()
用户为createCar()
功能输入(具体地,涉及情况2 - 显示布加迪),并显示一些大的奇数值,而不是用户的输入。我知道这与值没有被存储到内存/回调到main()
有关。
另外,createCar()
函数中的while
语句在我由于某种原因而使用参数时完全被忽略。
我将不胜感激代码中的答案,使事情更容易解决,如果可能的话,谢谢!
#include <stdio.h>
#include <math.h>
#define now 2017
//Function headers
void printMenu(void);
void createCar(int *speed, int *year, int *bhp, int *age);
int main(void)
{
//Variables
int userInput;
int topSpeed, yearMade, horsepower, carAge;
/***Loop program to return to menu after option is completed***/
for(;;)
{
//Print menu and get input from user
printMenu();
scanf("%i", &userInput), fflush(stdin);
//Validate input
while(userInput < 1 || userInput > 3)
{
printf("\nWrong input, please retry...\n");
scanf("%i", &userInput), fflush(stdin);
}
//Make decisions after user's choice
switch(userInput)
{
//Option 1: Create car then return to menu
case 1:
createCar(&topSpeed, &yearMade, &horsepower, &carAge);
continue;
//Option 2: Read car details (if created) then return to menu
case 2:
if(topSpeed == NULL)
{
printf("\nYou must first create a car, please retry...\n\n");
continue;
}
printf("\n----Bugatti Veyron----\n");
printf("Top Speed: %i km/h\nYear made: %i\nAge: %i years old\nHorsepower: %i bhp\n", &topSpeed, &yearMade, &horsepower, &carAge);
printf("----------------------\n");
continue;
//Option 3: Kill program
case 3:
exit(1);
}
}
return 0;
}
//Function: Display menu
void printMenu(void)
{
printf("-----------------------------------------\n");
printf("[Bob's Custom Car Creation Complex v1.0]\n");
printf("1. Create Bugatti\n2. Display Bugatti\n3. Exit\n");
printf("-----------------------------------------\n");
}
//Function: Make a car + validate inputs
void createCar(int *speed, int *year, int *bhp, int *age)
{
//Prompt user for top speed + validate input
printf("Enter the top speed of your Bugatti:");
scanf("%i", &speed), fflush(stdin);
while(speed <=0)
{
printf("You cannot have a top speed of nothing silly :-D\nPlease retry...\n");
scanf("%i", &speed), fflush(stdin);
}
//Prompt user for year mate + validate input
printf("What year is your Bugatti produced?:");
scanf("%i", &year), fflush(stdin);
while(year <=0)
{
printf("You cannot own a Bugatti that is from the future laddy!!\nPlease retry...\n");
scanf("%i", &year), fflush(stdin);
}
//Calculate age of car
age = now - year;
//Prompt user for horsepower + validate input
printf("How much horsepower does your Bugatti have?:");
scanf("%i", &bhp), fflush(stdin);
while(bhp <=0)
{
printf("A Bugatti with no engine... doesn't sound too promising :-O\nPlease retry...\n");
scanf("%i", &bhp), fflush(stdin);
}
}
请启用编译器警告。他们基本上回答你的问题。另外['fflush(stdin)'是未定义的行为](http://stackoverflow.com/q/2979209/3425536)。 – emlai
给定'int * speed','&speed'是什么?和'fflush(stdin);'? –
Andrew Henle指出,你有指针问题。不是在'createCar'中传递指向'scanf'的指针,而是传递指针指针。你的'while'正被忽略b/c你正在比较指针值为0而不是值(即使用'* year'),'case 2'也不比较'topSpeed == NULL',因为'topSpeed'不是一个指针。提示:阅读指针。但这是一个很好的练习。 – jiveturkey