我目前正在编写一个分配程序,它需要使用函数来使用户能够输入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(我编辑是正确的) –
你的代码甚至没有编译。 –