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