0

I am currently writing a program for a assigment which requires the use of a function to enable the user to input 3 vairables. I am having difficulty returning these variables to my main function, I have seen other similar questions previously asked and have attempted to use pointers but am unable to get it working. My attempt is below:

#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);
}
5
  • 1
    C or C++? You tagged this question as C++, but you wrote "in C" in the title, which is it? Commented Apr 16, 2017 at 10:02
  • Apologies that was an error entering the tag, it is C (which I have edited to be correct) Commented Apr 16, 2017 at 10:12
  • Your code doesn't even compile. Commented Apr 16, 2017 at 10:18
  • Take the compiler's warning serious. Fix the code until no more warnings are issued. Do not do this by blindly casting away warnings. Commented Apr 16, 2017 at 11:41
  • What's the idea behind the semicolon here: } else;? Commented Apr 16, 2017 at 11:44

3 Answers 3

2

You can't return multiple values from a function unless you wrap them in a struct. As far as pointers are concerned you can modify the values that you passed into the function from main. I think you're doing it wrong here :

scanf("%lf", &topSpeed);

Since topSpeed is a pointer to a double and you only need to pass the address of the variable you passed from main (not the address of pointer variable). You should instead do:

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

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

You declared the variables tS, yR & hP inside the main function and passed them by reference to the get_positive_value() function.

So the address of the variables are being passed. Not the variables themselves.

In get_positive_value(), you are attempting to place some values into the 3 variables using scanf() where you should've given the address of the variables but gave the address of address instead. &topSpeed in get_positive_value() is like &(&tS) in main().

Since you have passed them by reference, in get_positive_value(), you have the address of tS, yR, hP in topSpeed, year, horsepower respectively.

topSpeed itself is the address of tS. Not &topSpeed.

You should change
scanf("%lf", &topSpeed);
to
scanf("%lf", topSpeed);
(likewise for the other 2 variables)

Because topSpeed is having the address of the variable tS in main(). So if you say &topSpeed you are trying to access the 'address of address of tS'.

Comments

0

When you do *someptr you are asking for the value, at the memory address this pointer is pointing at.

When you do a scanf and you use &x for a variable, you do it because you want to store the value at that memory address. So when you do a scanf with a pointer, you don't use * because you pass a value instead of an address, to store the value at.

You don't use & either, because you pass the memory address of the pointer instead of the one you actually want to modify. This is your main error. Lastly​, you could return those values all at once using a struct, but pointers are more elegant.

Hope I helped you and I was clear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.