参数未正确传递回主

时间:2017-04-19 14:51:35

标签: c

来源大纲:用户选择以下选项:1。制作布加迪; 2.创建布加迪;或3.退出程序。每个选项完成后,应将用户返回菜单以选择其他选项。

(注意:在创建汽车之前,用户无法显示汽车,因此情况2中的if声明

问题:用户对createCar()功能的输入不会返回main()(特别是情况2 - 显示布加迪)并显示一些大而奇的值,而不是用户的输入。我知道这与未存储到内存中的值/回调到main()有关。

此外,当我出于某种原因使用参数时,while函数中的createCar()语句完全被忽略。

我很感激代码中的答案,以便在可能的情况下让个人更容易解决,谢谢!

#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);
    }
}

1 个答案:

答案 0 :(得分:0)

您必须取消引用年龄和年份指针以获取/设置其值。

//Calculate age of car
*age = now - *year;

你必须删除&#39;&amp;&#39;位于scanf()的{​​{1}},因为createVarspeedyear已经指向了int。

启用编译器警告并解决它们可以避免麻烦!