Lost in functions

Working on another programming assignment and cannot seem to find a foothold how to write a single function that calculates cost of 4 different items. I think that if i get a bit of help on this function I will be able to figure out the other function which is a VOID that outputs to display the cost of the cargo to display; formatted of course. I currently have three different book sources that help a bit, however the damn $239 textbook isn't much help. C++ Primer is too advanced and C++ for dummies isn't much better than the textbook. Looking for a bit of help where to begin. The current code compiles but I have to modify it using the two functions. After this class I have a new found respect for programmers.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
 //Pass-8 Test Code
//Creating and using Functions

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Function prototype; Must code the definition
	double calcCost(int cargoAmount, double pricePerUnit);

//Definition of Function
	double calcCost(int cargoAmount, double pricePerUnit) 
	{
		//Not sure what to do here

	}




//Void Function prototype to output description, Sales Amount and Cost in Column Format
	void printOutput();


//Definition of this void function

	void printOutput()
	{


	}

int main() {

//Constant Variables
const double FUEL_POD = 125.50;
const double PROTON_AMMO = 17.2;
const double JET_PACK = 99.00;
const double OXYGEN_TANK = 50.40;

cout<<"*****************************************\n";
cout<<"*****************************************\n";
cout<<"\n\n";
cout<<"Welcome to Dukes Space Travel Company.\n\n"<<endl;

//Integer variables to hold quantities of items bought

int spaceshipID = 0;
int nbrFuelPods;
int nbrCannonAmmo;  //will require math sold per 100 rounds using % operator 
int nbrJetpacks;
int nbrOxyTanks;

//Double variables to hold cost of items

double costFuelPods;
double costAmmo;
double costJetpack;
double costOxygen;
double totalCost;

//String variable to hold name of captain. May need to use getline(cin, captName) and ignore
//to get entire name of captain if user enters first and last name
string captName; 

//Do the following as long as ship id is not -999

cout<<"Please Enter 3-Digit Ship ID: ";
cin>>spaceshipID;

	while(spaceshipID != -999) {	
		cin.ignore();
		cout<<"Enter Captain's Name: "; //Prompt user to enter Captain Name
		getline(cin, captName); //Code needed to get entire name
		cout<<"Enter Number of Fuel Pods: ";
		cin>>nbrFuelPods;
		cout<<"Enter Quantity of Proton Ammo in Quantities of 100: ";
		cin>>nbrCannonAmmo;
		cout<<"Enter Number of Jetpacks: ";
		cin>>nbrJetpacks;
		cout<<"Enter Number of Oxygen Tanks: ";
		cin>>nbrOxyTanks;

		//Add a couple lines to improve output appearance
		cout<<endl;
		cout<<endl;
		cout<<left<<"The Dukes Space Travel Company Sales Statement"<<endl;
		cout<<"Spaceship ID Number "<<spaceshipID<<endl;
		cout<<"====================================================="<<endl;

		//I'm assuming this block will occur in a function??
		costFuelPods = nbrFuelPods * FUEL_POD;
		costAmmo = (nbrCannonAmmo / 100) * PROTON_AMMO; //Not sure this is correct if 150 rounds are ordered
		costJetpack = nbrJetpacks * JET_PACK; 
		costOxygen = nbrOxyTanks * OXYGEN_TANK;
		totalCost = (costFuelPods + costAmmo + costJetpack + costOxygen);

		//Will this block occur inside a function as well?
		cout<<setw(15)<<"Cargo "<< setw(20) <<"Sales Amount "<<setw(20)<<"Cost"<<endl;
		cout<<setw(15)<<"Fuel Pod "<< setw(20)<< nbrFuelPods << fixed << setprecision(2)<<"$"<<costFuelPods<<endl;
		cout<<setw(15)<<"Proton Ammo "<<setw(20)<<nbrCannonAmmo<<fixed<<setprecision(2)<<"$"<<costAmmo<<endl;
		cout<<setw(15)<<"JetPacks "<<setw(20)<<nbrJetpacks<<fixed<<setprecision(2)<<"$"<<costJetpack<<endl;
		cout<<setw(15)<<"Oxygen "<<setw(20)<<nbrOxyTanks<<fixed<<setprecision(2)<<"$"<<costOxygen<<endl;
		cout<<"=======================================================\n";
		cout<<"======================================================="<<endl;
		cout<<"\n\n";
		cout<<setw(35)<<"Total Due: "<<fixed<<setprecision(2)<<"$"<<totalCost<<endl; 
		cout<<endl;
		cout<<"Please Enter 3-Digit Ship ID: ";
		cin>>spaceshipID;
	
	} 
system("Pause");
return 0;

}

 
Last edited on
Your code is kinda long, I only read some part of it

C++ for dummies is quite sufficient I think

1
2
3
4
5
	double calcCost(int cargoAmount, double pricePerUnit) 
	{
		//Not sure what to do here
                 return cargoAmount * PricePerUnit;
	}

The code above is what I think you want ?
But I am not sure of what you want ...

I am not exactly sure by calculates cost of 4 different items.

so can you tell me on how will you call this function
that will probably make things clearer for me ...
how to write a single function that calculates cost of 4 different items.

Some ideas...
- Not always a good idea, have functions that perform one function (i.e. cost of one thing).
- Return a struct containing the individual costs.
1
2
3
4
5
6
7
8
9
10
11
12
struct costs {
int a,b ..
};
costs calc(int A,int B ...)
{
    costs ans;
    a = //do something with A
    b = //do something with B
    ...
    return ans;
}

- Just return the total costs. Best suited for your situation.
1
2
3
4
5
6
double calc(double a, double b ...)
{
    double total,costA , costB ...;
    // do something with them
    return total;
}
The instructions I was given were to use compute the cost of each item of cargo in a function. The function is to be called 4 times once for each cargo type.

I'm supposed to use this prototype:

[ double calc(int cargoAmount, double pricePerUnit); }]

This assignment is a modification of a previous one that I understood well enough to complete on my own. I assume the calculations I've already done have to be inside a function now. Some questions I have and areas I'm not understanding.

- The user enters name and quantity of each type of cargo within the while loop are the function calls also placed within the while loop?

- do I just copy the calculations already completed and put them inside the function and make the call 4 times?

I realize this is extremely easy for those proficient in coding, but being my second class (Java first-----and much easier) these assignments seem really complicated to me. I'd rather not ask for help, but being an online class and no physical human resources to ask questions and seek help makes it much more complicated. Not sure about anyone else here, but I'm extremely appreciative to learn and help.
Then it is easier , (if I understand the problem well)
1
2
3
4
5
6
7
8
9
double calc(int cargoAmount, double pricePerUnit)
{
     return cargoAmount * pricePerUnit;
}
...
costFuelPods = calc(nbrFuelPods , FUEL_POD);
costAmmo = calc(nbrCannonAmmo / 100 , PROTON_AMMO );
...
totalCost = costFuelPods + costAmmo;

Using a while loop ? that seems unnecessary.
Last edited on
I belive i understand some of it on calculating the cost of 4 items with the help of (akn). I would'nt be able to do it unless you have shown me.
Thank you for all the help! I have a much better understanding of the arguments in the function definition. The program compiles and runs correctly. My next issue is writing another function to print to display one category of cargo at a time. I am going to figure this one out on my own by reading, practicing and testing. Again, thanks for the huge help!
Topic archived. No new replies allowed.