Daniel Griffith
Daniel Griffith

Reputation: 27

Passing different values between multiple functions in C?

I'm having trouble passing differing values between multiple functions. That is, i'm trying to have two functions take user inputs, and then have both of those inputs passed to a third function that calculates those inputs added together. Afterwards, it passes that total on to another function that calculates the true total. Finally, after that, the last function displays the final number. I can't seem to figure out how to make multiple functions pass though.

# include <stdio.h>
# include <stdlib.h>

int carLength(int userInput) //Length
{
int length = 0;
do
{
    printf("\nPlease input the length of the area being carpeted:");
    scanf("%d", &userInput);
} while (length = 0);
length = userInput; //Essentially using this to store user input so i can 
use it later.
return length;
}

int carWidth(int userInput) //Width
{
int width = 0;
do
{
    printf("\nPlease input the width of the area being carpeted:");
    scanf("%d", &userInput);
} while (width = 0);
width = userInput; //Same as width
return width;
}

int carCalculate(int cost) //cost
{
int width = userInput;
int cost = (width * length) * 20;
return cost;
}

float cardiscount(float discount) //discount
{
float discount = cost - (cost * .1);
return discount;
}

float displaydisc(float discount) //discount
{
printf("\nThe total cost is: %f\n", discount);
return discount;
}

int main()
{
int length = 0;
int width = 0;
int cost = 0;
int discount = 0;
do {
    length = carLength;
} while (length == 0);
    do {
        width = carWidth;
    } while (carWidth == 0);
    do {
        cost = carCalculate;
    } while (cost == 0);
    do {
        discount = cardiscount;
    } while (discount == 0);
    do {
        displaydisc;
    } while (discount > 0);
    printf("\n Thank you for using this program!");

system("pause");
}

Upvotes: 0

Views: 57

Answers (2)

Julius
Julius

Reputation: 116

There are a few problems,

  • parameters given to most car functions are useless
  • needed parameters are not provided (carCalculate)
  • the loop on = 0 having = (assignment instead of equality test)
  • the loop on a condition where the variable is not changed
  • calling functions in main() without the (param)

This code should work:

int carLength() // <== no need to provide length, it is returned
{
    int length;
    do
    {
        printf("\nPlease input the length of the area being carpeted:");
        scanf("%d", &length);
    } while (length == 0); // <== length is changed within the loop
    return length;
}

int carWidth() //Width
{
    int width = 0;
    do
    {
        printf("\nPlease input the width of the area being carpeted:");
        scanf("%d", &width);
    } while (width == 0);
    return width;
}

int carCalculate(int width, int length) // <== need to provide values
{
    int cost = (width * length) * 20;
    return cost;
}

float cardiscount(float cost) //discount
{
    float discount = cost - (cost * .1);
    return discount;
}

void displaydisc(float discount) //discount
{
    printf("\nThe total cost is: %f\n", discount);
}

int main()
{
    int length; // <== no need to set to 0, as their values
    int width;  //     are going to be initialized later
    int cost;
    int discount;

    length = carLength();

    width = carWidth();

    cost = carCalculate(width, length);

    discount = cardiscount((float)cost);

    displaydisc(discount);

    printf("\n Thank you for using this program!");

    system("pause");
}

You could also have asked the input functions to fill a value which pointer is given as argument, for instance

int carLength(int *length) {
    do {
        printf("\nPlease input the length of the area being carpeted:");
        scanf("%d", length);
    } while (*length == 0);
}

int called in main

carLength( &length );

Upvotes: 2

BoldAsLove
BoldAsLove

Reputation: 689

Your not making any function calls. Function calls need to have open/close parentheses after the function name.

For example this is not a function call:

functionA;

... and this is a function call:

functionA();

Upvotes: 1

Related Questions