Need help with Petrol Station Program

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
int main();

			class petrolPump					//Petrol Pump class
			{
				int x, y;

			public:

				void unleaded()
				{
					int ptrl = 137.7;
					cout << "Unleaded Price  = 137.7 per litre\n";
					cout << ("\n") << endl;
					cout << "How many litres do you want? : ";
					cin >> x;
					y = ptrl*x;
					cout << ("\n") << endl;
					cout << "---------------------'STANDBY'---------------------" << endl;
					Sleep(3000);
					cout << "----------------------'READY'----------------------" << endl;
					Sleep(3000);
					cout << "------------------------'3'------------------------" << endl;
					Sleep(1000);
					cout << "------------------------'2'------------------------" << endl;
					Sleep(1000);
					cout << "------------------------'1'------------------------" << endl;
					Sleep(1000);
					cout << "-------------'REFILLING / PLEASE WAIT'-------------" << endl;
					Sleep(5000);
					cout << "-----------------'REFILL COMPLETE'-----------------" << endl;
					Sleep(500);
					cout << ("\n") << endl;
					cout << ("**********************************************************") << endl;
					cout << ("Payment													") << endl;
					cout << ("**********************************************************") << endl;
					cout << ("\n") << endl;
					cout << "-Total Cost = " << y;
					cout << ("\n") << endl;
				}

				void diesel()
				{
					int die = 141.7;
					cout << "Diesel Price = 141.7 per litre\n";
					cout << ("\n") << endl;
					cout << "How many litres do you want? : ";
					cin >> x;
					y = die*x;
					cout << ("\n") << endl;
					cout << "---------------------'STANDBY'---------------------" << endl;
					Sleep(3000);
					cout << "----------------------'READY'----------------------" << endl;
					Sleep(3000);
					cout << "------------------------'3'------------------------" << endl;
					Sleep(1000);
					cout << "------------------------'2'------------------------" << endl;
					Sleep(1000);
					cout << "------------------------'1'------------------------" << endl;
					Sleep(1000);
					cout << "-------------'REFILLING / PLEASE WAIT'-------------" << endl;
					Sleep(5000);
					cout << "-----------------'REFILL COMPLETE'-----------------" << endl;
					Sleep(500);
					cout << ("\n") << endl;
					cout << ("**********************************************************") << endl;
					cout << ("Payment													") << endl;
					cout << ("**********************************************************") << endl;
					cout << ("\n") << endl;
					cout << "-Total Cost = " << y;
					cout << ("\n") << endl;
				}
			};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (selection)
						cout << "Payment Transaction Complete!" << endl;
					cout << ("\n") << endl;
					cout << "-------------------------------------------" << endl;
					cout << "CUSTOMER RECEIPT" << endl;								//CUSTOMER RECEIPT
					cout << "-------------------------------------------" << endl;
					cout << ("\n") << endl;

					int wmain(void);
					{
						SYSTEMTIME time; //system time
						while (true)
						{
							GetLocalTime(&time);
							wprintf(L"Local Time : %02d:%02d:%02d\n", time.wHour, time.wMinute, time.wSecond); //current system time
							wprintf(L"Date: %02d:%02d:%02d\n", time.wMonth, time.wDay, time.wYear);		      //current system date
							cout << ("\n") << endl;
							cout << ("Fuel Nzl Type  = ") << i << endl;		//fuel type selected
							cout << ("Amount Dispensed = ") << endl;		//amount of fuel used
							cout << ("Total Cost = ") << endl;		        //total cost of fuel
							cout << ("\n") << endl;
							cout << "-------------------------------------------" << endl;
							cout << "Thank You for using Petrol Service Station!" << endl;
							cout << "-------------------------------------------" << endl;
Last edited on
Hi,
I really think if you directly posted your code on the forum, you would definitely get much more (helpful) respones from others.
Thanks for the advice, first time on the forums.
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.

Willir wrote:
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
Last edited on
I have tried calling the functions to the customer receipt but I get errors.

What is the best way to return the results and call the functions?
I think you'll find this helpful:

http://www.cplusplus.com/doc/tutorial/functions/

It should explain how to define functions, how to declare them, and how to pass data to and from functions.
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.
1
2
  cout << Liters Dispensed: " << pump.get_liters_dispensed() << endl;
  cout << Total Amount: " << pump.get_total_cost() << endl;

Last edited on
Thanks for the advice I will take it into consideration.
Also, I've just noticed this at the top of your first code snippet:

int main();

What's that for?
Here is my full code if you would like to inspect it.

Not finished commenting.

http://codepad.org/JafsJo29
Last edited on
So, at several points in your code, you have the following:

int main();

Sometimes, but not always, followed by opening a new block.

What's that supposed to be for?

I assumed I had to call the main function each time I was declaring new functions etc....

I suppose declaring int main(); once is enough?
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.

Last edited on
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.
Topic archived. No new replies allowed.