• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Need Help With Some C Coding

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

kraven

Member
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:
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 :rain:

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.
:comp:
 
I would review passing values as opposed to passing pointers to values. There are basically two ways to get a value back from a function - one is to have the function return a single value, and the other is to pass variable pointers and have the function "fill them in". It looks like in a few cases, you're effectively doing this:

Code:
int x;
x = 0;
fillInMyVariable(x);
printf("%d", x);

void fillInMyVariable(int x)
{
    x = 5;
}

That's not going to work, because the code calling the fillInMyVariable function can't "see" the reassignment of "x". It would print "0" in the above case. You'd have to do this:

Code:
int x;
x = 0;
x = fillInMyVariable();

int fillInMyVariable()
{
    return 5;
}

Or you'd have to do this:

Code:
int x;
x = 0;
fillInMyVariable(&x);

void fillInMyVariable(int* x)
{
    *x = 5;
}

Does that make sense?
 
A little bit.
I'll have to play around with that method.
I haven't been using indirect assignment with pointers and references to pointers.
 
Also, for function 2 and 3 in my function declaration, should those have void return values because i don't actually return something from them, i'd be setting the values of the variables using references and pointers?
 
If you're not comfortable with pointers and dereferencing (or you haven't learned it yet in class) the simpler way is to just use return values. In that case, you should re-work your code so that it's possible to put everything together using functions that all return a single value. In my opinion that's the nicer way to do it.

If you're not returning anything from a function then the function should be "void", yes. And generally if you're setting things using pointers, then it's better to pick one way of getting values instead of trying to use both.

In the example I gave above, I wouldn't use the second one - that is, I wouldn't use a pointer just to set a single value. In that case it's more appropriate to return the value, so I'd use the first example.

One issue with getting used to pointers is that you'll want to use them for EVERYTHING, which lends itself to long, complex functions that 'return', via pointers, three or four values. That's a bad habit to get into stylistically. In general, functions should do a single, focused operation that produces a single result. I would apply that thinking to your getCostAndCapacity function. Just having the "and" in the name tells me it's doing a little too much. If it were me, I would break those operations into two separate functions that each return one value.

Edit: never mind, I guess your assignment requires you to have functions that take multiple values and set them using pointers. That's fine if that's what you're supposed to do.
 
aarrgh! almost have it.

i was able to compile it all but ended up with a segmentation fault at run time.

there's a tutor coming in in 1 hr. i hope they know C.

i'm sure using pointers and references has it's place but i much prefer passing arguments and returning values.

C is a merciless biotch. i think i actually enjoy coding in VB a lot more.
 
C is a tough language to start out with. I started with Java, and found it very helpful for learning C later. Java isn't as tough on you with regard to segfaults and such - it provides much more informative errors.

If you'd like, you can post your code again and I'll see if I can find what's segfaulting. My guess is that you're trying to dereference (*) something that you shouldn't.

For example, this code should segfault:

int x;
x = 0;
*x = 10;
 
I started with VB then learned Java, moved on to some C++ and now I'm doing C.

To clarify, I have had very little experience with references and pointers before this.

Lucky me, the tutor that's going to show up doesn't know C. Buuuuut by teacher is here so I think I'm saved.

On an aside, who uses C anymore? I have never seen a company look for a C programmer.
 
It's still used pretty heavily for building services. There's less demand for lots of general-purpose C developers, but a developer with a lot of experience coding high-performance platforms in C is extremely valuable. It's also a good language for learning how computers actually run code, since it's very close to the system.

I believe C is a very good language to have at least some experience in.
 
Here's the code that I ended up with:

Main:
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 = 0.0;
float fillUpCost = 0.0;
int capacity = 0;

getCostAndCapacity(&price, &capacity);

fillCost(&price, &capacity, &fillUpCost);

printf("You entered: $%.2f for the price per gallon.\n", price);
printf("And %d for the tank capacity.\n", capacity);
printf("Based off these numbers, the cost to fill your tank is: $%.2f.\n", fillUpCost);

return 0;

}//main

Function Definitions:
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

void fillCost(float *price, int *capacity, float *fillUpCost)
{
        *fillUpCost = tankFill(*price, *capacity);

}//fillCost

void 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:\n");
                printf("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:\n");
                printf("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

Library:
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);
void fillCost(float *price, int *capacity, float *fillUpCost);
void getCostAndCapacity(float *price, int *capacity);

#endif

It all compiles and runs as expected.

Now for part 2....
 
Back