The problem I am having is that I am finding it difficult to output Amount Dispensed & Total Cost in the customer receipt. Is there a way I can cout the output that I get when I cin the amount I want and the overall cost? Any help would be appreciated. Thanks in advance.
Need results of lines 16/37 & 48/69 in code block one to cout on lines 19 & 20 in code block 2 below!
If I've understood you right, you want to have your unleaded and diesel functions return the results of their calculations to the calling code. Then, have your code call those functions, and output the values returned.
I don't know what's going on in that second code snippet. From the way you've formatted it, line 9 looks like it's supposed to be the start of a function definition. However:
(a) Line 9 ends with a semicolon, which means it isn't; instead, the compiler will interpret it as a declaration.
(b) If you remove the semicolon, then you'll be trying to define a function inside another function, which is illegal.
Correct, I basically need the results to cout to the customer receipt.
If I remove the semicolon on line 9 in the second snippet it gives me errors, my code fully runs without errors at the moment. There is a lot more to the program than what I posted in the snippets buts its far too big to post.
Correct, I basically need the results to cout to the customer receipt.
MikeyBoy wrote:
you want to have your unleaded and diesel functions return the results of their calculations to the calling code. Then, have your code call those functions, and output the values returned.
Willir wrote:
If I remove the semicolon on line 9 in the second snippet it gives me errors
MikeyBoy wrote:
If you remove the semicolon, then you'll be trying to define a function inside another function, which is illegal.
Willir wrote:
my code fully runs without errors at the moment.
MikeyBoy wrote:
instead, the compiler will interpret it as a declaration
Line 5: x and y are very poor member variables names. You don;t even have a comment describing what they're used for. I had to dig through your code to figure out how you're using them. A program should be self-documenting. Member variables names such as liters_dispensed and total_cost would have been much more meaningful.
Lines 11,43: You're trying to assign a fractional value to an int. It's going to get truncated.
Your unleaded and diesel functions are identical except for the price and the name of the fuel. Consider having a function to print a receipt that takes the price and fuel name as arguments. It's also a good idea to separate your input and output functions.
2nd snippet lines 1-9 appear to be hanging code fragment and don't make sense as posted.
Need results of lines 16/37 & 48/69 in code block one to cout on lines 19 & 20 in code block 2 below!
You don't appear to instantiate your petrolPump class anywhere.
2nd snippet line 18: Not clear where i comes from (another poor variable name). Nor what the values of i mean.
1 2 3 4 5 6 7
// Line 10:
pertrolPump pump;
// After line 18:
if (i == 1)
pump.diesel ();
if (i == 2)
pump.unleaded();
Lines 19-20: If you want to retrieve the liters_dispensed and total_cost from the class, you're going to need getters for those variables in your class. Or print the values from a member function.
I assumed I had to call the main function each time I was declaring new functions etc....
Firstly, that's not calling a function, it's declaring it.
No, you don't need to declare it at all, anywhere in the code you've written. And you never, ever call the main function from within your code - that's illegal.
And in the places you've put those lines, you haven't declared or defined any new functions anyway, so I don't really understand what you were trying to achieve.
Hopefully, when you read that tutorial I linked you to (or just the relevant chapter in your textbook), that will help clear up your confusion about why and when you declare functions.
You sorted the issue out? I'm having a very similar issue with mine, tried everything to get it to take the total cost and the fuel but nothing seems to be working. Managed to get it to show what fuel it's taken but only as 1(petrol) or 2(diesel), not got the words for it. Stuck on it for a while now.