我目前正在編寫一個分配程序,它需要使用函數來使用戶能夠輸入3個可變元素。我很難將這些變量返回到我的主函數中,我曾經看到過其他類似的問題,並且試圖使用指針,但無法使其工作。我嘗試低於:如何使用指針從函數返回多個值C
#include <stdio.h>
#include <stdlib.h>
//Function Header for positive values function
double get_positive_value(double* topSpeed, double* year, double*
horsepower);
int main(void){
int reRunProgram = 0;
while (reRunProgram==0)
{
//variable declarations
double tS;
double yR;
double hP;
int menuOption;
int menuOption2;
//menu
printf("1.Create Bugatti\n");
printf("2.Display Bugatti\n");
printf("3.Exit\n");
//user choice
scanf("%d", &menuOption);
//Create car
if (menuOption == 1) {
//run the get positive values function
get_positive_value (&tS, &yR, &hP);
printf("top speed is %lf\n", tS);
}
//Display car (but no car created)
else if (menuOption == 2){
printf("error no car created\n");
}
//Exit
else if (menuOption ==3){
exit(EXIT_FAILURE);
}
}
return 0;
}
double get_positive_value(double* topSpeed, double* year, double*
horsepower)
{
do {
printf("Please enter the top speed of the bugatti in km/h\n");
scanf("%lf", &topSpeed);
} while(*topSpeed<=0);
do{
printf("Please enter the year of the bugatti, in four digit form (e.g. 1999)\n");
scanf("%lf", &year);
} while(*year<=0);
do{
printf("Please enter the horsepower of the bugatti\n");
scanf("%lf", &horsepower);
} while(*horsepower<=0);
}
C或C++?你把這個問題標記爲C++,但是你在標題中寫了「in C」,這是什麼? – Rakete1111
道歉,這是一個錯誤輸入標籤,它是C(我編輯是正確的) –
你的代碼甚至沒有編譯。 –