0

Source outline: User selects option either to 1. Make Bugatti; 2. Create Bugatti; or 3. Exit program. After each option is complete, the user should be returned back to the menu to select another option.

(Note: the user cannot display the car until it is created, hence the if statement in case 2)

The problem: User's inputs for createCar() function are not being returned back into main() (specifically to case 2 - Display Bugatti) and is displaying some large, odd values, instead of the user's inputs. I know it has something to do with the values not being stored into memory/called back to main().

Also, the while statements in createCar() function are completely being disregarded when I use parameters for some reason.

I would appreciate answers in code to make things easier to resolve personally if possible, thanks!

#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);
    }
}
Toby
  • 9,696
  • 16
  • 68
  • 132
Bob Smith
  • 11
  • 3
  • 5
    Please enable compiler warnings. They basically answer your question. Also [`fflush(stdin)` is undefined behavior](http://stackoverflow.com/q/2979209/3425536). – Emil Laine Apr 19 '17 at 14:52
  • 3
    Given `int *speed`, what is `&speed`? And `fflush(stdin);`? – Andrew Henle Apr 19 '17 at 14:54
  • 3
    You've got pointer problems, as Andrew Henle is pointing out. Instead of passing pointers to `scanf` in `createCar` you are passing pointers-to-pointers. Your `while`'s are being disregarded b/c you are comparing pointer values to 0 instead of values (i.e. use `*year`), also in `case 2` do not compare `topSpeed == NULL` because `topSpeed` is not a pointer. Hint: Read up on pointers. But this is a nice exercise. – jiveturkey Apr 19 '17 at 15:04

1 Answers1

0

You have to dereference the age and year pointer to get/set its value.

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

You have to remove the '&' at the scanf() in createVar, because speed, year and bhp are already pointers to int.

Enabling compiler warnings and resolving them would avoid you troubles!

thomas
  • 330
  • 3
  • 10