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.
//Pass-8 Test Code
//Creating and using Functions
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace 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
constdouble FUEL_POD = 125.50;
constdouble PROTON_AMMO = 17.2;
constdouble JET_PACK = 99.00;
constdouble 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;
}
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.
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.
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!