- Joined
- Nov 11, 2004
Yes, it's homework but I'm just stuck on it. I can't figure out how to get the values back. The program runs but the output is zeros.
Here's my main class:
My source code file for function definition:
And my header file:
This has to stay in the different files, would be easy if I could just put it in 1 file but nooooooooo
Here's the assignment:
File I. A source code file containing definitions for the following functions:
1. A function that takes as arguments a floating-point price-per-gallon and an integer tank capacity (in gallons) and returns the floating-point cost of filling that tank, assuming it is empty.
2. A function that takes three arguments that represent a price-per-gallon, tank capacity, and tank fill-up-cost. The function should set the value of the tank fill up cost argument using a call to function (1) with the passed in price per gallon and tank capacity values.
3. A function that takes two arguments that represetn a price per gallon and tak capacity. The function should set each of these variables to positive values input by the user, giving the user as many attempts as needed to input valid values.
File II. A header file containing the prototypes for all functions defined in File I.
File III. A source file containing a main function that does the following in the indicated order:
1. Get a price per gallon and tank capacity from the user by calling your function (I.3).
2. Calculate the tank fill up cost by calling your function (I.2).
3. Output all values (both user input and calculated).
My problem is getting the values passed around properly and then passed back into the main function.
I'm sure this is something simple I'm overlooking but I'm stumped.
Here's my main class:
Code:
/*fuelCostMain.c: calculates and outputs car fuel costs
Created: May 15, 2011
Author: Kraven
*/
#include <stdio.h>
#include "fuelCostFuncs.h"
int main (int argc, char* argv[])
{
float price, totalCost;
int capacity;
getCostAndCapacity(price, capacity);
totalCost = fillCost(price, capacity, totalCost);
printf("Here's the output: price = %f, capacity = %d, and fill cost = %f\n", price, capacity, totalCost);
return 0;
}//main
My source code file for function definition:
Code:
/*fuelCostFuncs.c: fuel cost library function definitions
Created: May 15, 2011
Author: Kraven
*/
#include <stdio.h>
#include "fuelCostFuncs.h"
float tankFill(float price, int capacity)
{
return (price * capacity);
}//tankFill
float fillCost(float price, int capacity, float fillUpCost)
{
fillUpCost = tankFill(price, capacity);
return fillUpCost;
}//fillCost
float getCostAndCapacity(float price, int capacity)
{
int flag = 0;
while (!flag)
{
printf("Input the cost per gallon.\n");
while (scanf("%f", &price) != 1)
{
scanf("%*s");
printf("Invalid input: please enter a number for the cost per gallon.\n");
}//while
if (price <= 0)
{
printf("Input error: enter a number greater than zero.\n");
} else {
flag = 1;
}
}//price while
flag = 0;
while (!flag)
{
printf("Input the tank capacity.\n");
while (scanf("%d", &capacity) != 1)
{
scanf("%*s");
printf("Invalid input: please enter a number for the tank capacity.\n");
}//while
if (capacity <= 0)
{
printf("Input error: enter a number greater than zero.\n");
} else {
flag = 1;
}
}//capacity while
}//getCostAndCapacity
And my header file:
Code:
/*fuelCostFuncs.h: header file for fill cost library
Created: May 15, 2011
Author: Kraven
*/
#ifndef FUELCOSTFUNCS_H
#define FUELCOSTFUNCS_H
#include <stdio.h>
//function prototypes
float tankFill(float price, int capacity);
float fillCost(float price, int capacity, float fillUpCost);
float getCostAndCapacity(float price, int capacity);
#endif
This has to stay in the different files, would be easy if I could just put it in 1 file but nooooooooo
Here's the assignment:
File I. A source code file containing definitions for the following functions:
1. A function that takes as arguments a floating-point price-per-gallon and an integer tank capacity (in gallons) and returns the floating-point cost of filling that tank, assuming it is empty.
2. A function that takes three arguments that represent a price-per-gallon, tank capacity, and tank fill-up-cost. The function should set the value of the tank fill up cost argument using a call to function (1) with the passed in price per gallon and tank capacity values.
3. A function that takes two arguments that represetn a price per gallon and tak capacity. The function should set each of these variables to positive values input by the user, giving the user as many attempts as needed to input valid values.
File II. A header file containing the prototypes for all functions defined in File I.
File III. A source file containing a main function that does the following in the indicated order:
1. Get a price per gallon and tank capacity from the user by calling your function (I.3).
2. Calculate the tank fill up cost by calling your function (I.2).
3. Output all values (both user input and calculated).
My problem is getting the values passed around properly and then passed back into the main function.
I'm sure this is something simple I'm overlooking but I'm stumped.