Overloaded functions

Oct 31, 2013 at 10:52am
I'm a little confused on this assignment. I've done most of the program but am a little confused by the call to the second function. Thought I was doing well and felt good about getting as far as I did before the current confusion or lack of knowledge. The instructions are:

A value-returning function named calcTravelCost that accepts cost per day and number of days and returns the total cost (overloaded functions).

 The main function will request the fuel cost, number of days, waste disposal fee, and ask if there is a miscellaneous expense. If ‘Y’ it will ask for the amount and call the first calcTravelCost function. If ‘N’ then it will call the first calcTravelCost using a default value. This calcTravelCost will call the second calcTravelCost appropriately. Continue looping for more expenses until -999 is entered for fuel cost.

Here is what I have so far that compiles and runs correctly. Thanks in advance for looking/attempting to help.
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
//Joe Snuffy
//CSCI 1010 W1
//PASS 10
/*Space Travel Company program that will calculate
the total cost for travel using reference parameters,
overloaded functions and value returning functions.*/

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

using namespace std;

/*function prototype...the misc variable will be set to a default value
if the parameters are unchanged the default value will be used.*/ 

double calcTravelCost(double &fuelCost, double &foodCost, double &oxyCost, double &wasteCost, double miscCost);

double calcTravelCost(double &perDay, double &total, int &nbrOfDays);


//declare constant GLOBAL variables

const double OXYGEN_PERDAY = 25.00;
const double FOOD_PERDAY = 35.00;

//declare additional variables

double fuelCost = 0; //initialize
double fuelTotal = 0;
double foodCost = 0;
double oxyCost = 0;
double wasteCost = 0;
double miscCost = 0;

double totalCost = 0;
int nbrOfDays = 0;

int main()
{
//variable to hold user choice for Y or N
char userChoice = ' ';
//Prompt user for input
cout<<"Welcome to Joe Snuffy Space Travel Company"<<endl;
cout<<"Please enter fuel cost or -999 to end: ";
cin>>fuelCost;
while(fuelCost != -999)
{
	cout<<"Enter the number of days: ";
    cin>>nbrOfDays;
    cout<<"Enter the waste disposal cost: ";
    cin>>wasteCost;
    cout<<"Is there a miscellaneous cost?  (Y or N): ";
    cin>>userChoice;//character variable

	if(userChoice == 'Y' || userChoice == 'y')
	{ 
		cout<<"Please enter the miscellaneous expense cost: ";
		cin>>miscCost;
		//call to first function
		calcTravelCost(fuelCost, foodCost, oxyCost, wasteCost, miscCost);
		cout<<endl;
		cout<<endl;
		cout<<"The total space travel cost is: "<<endl;
		cout<<"Fuel Cost"<<setw(30)<<fixed<<setprecision(2)<<fuelCost<<endl;
		cout<<"Oxygen"<<setw(33)<<fixed<<setprecision(2)<<oxyCost<<endl;
		cout<<"Food"<<setw(35)<<fixed<<setprecision(2)<<foodCost<<endl;
		cout<<"Waste Disposal"<<setw(25)<<fixed<<setprecision(2)<<wasteCost<<endl;
		cout<<"Miscellaneous Cost"<<setw(21)<<fixed<<setprecision(2)<<miscCost<<endl;
		cout<<endl;
		cout<<endl;
		cout<<"Total"<<setw(34)<<fixed<<setprecision(2)<<totalCost<<endl;
		cout<<endl;
		cout<<endl;
	}

	else if(userChoice == 'N' || userChoice == 'n')
	{
		//Confused what would go here.

	}
		
		cout<<"Please enter fuel cost or -999 to end: ";
		cin>>fuelCost;
		
		
}
system("Pause");
return 0;
}//return

//overloaded function defintions
double calcTravelCost(double &fuelCost, double &foodCost, double &oxyCost, double &wasteCost, double miscCost)
{
	fuelTotal = fuelCost * nbrOfDays;
	foodCost = FOOD_PERDAY * nbrOfDays;
	oxyCost = OXYGEN_PERDAY * nbrOfDays;
	totalCost = fuelCost + foodCost + oxyCost + wasteCost + miscCost;
	return totalCost;
}

double calcTravelCost(double &perDay, double &total, int &nbrOfDays)
{

}
Oct 31, 2013 at 12:23pm
Exactly what you done for the first function you would do for the second. Just instead of passing 5 parameters you will now pass 3 instead. Function overloading allows you to name a function exactly the same as another provided u pass a different variables to it. In this case using 5 and three should work perfectly.
Oct 31, 2013 at 11:29pm
Seriously? This assignment seem way to easy for what I've been able to accomplish with minimal assistance here. This is the completed assignment. It compiles all scenarios correctly but I just don't trust the work was supposed to be completed by someone with my skills so soon. Here is more of the assignment if this helps. I really want to do my best and understand so to me it is important that I get it right.

Write the C++ code that will contain 2 functions (besides the main function):
 A value-returning function named calcTravelCost
o that accepts at least
 the fuel cost,
 number of days of space travel,
 waste disposal cost.
o If there is a miscellaneous expense, it will accept it. If there is not a miscellaneous expense, have a default value of 0.
o This function will,
 by reference parameters, pass back
 the cost of the oxygen (all days)
 and the cost of the food (all days)
 and return the total cost (by return statement).
 A value-returning function named calcTravelCost that accepts cost per day and number of days and returns the total cost (overloaded functions).
 The main function will request the fuel cost, number of days, waste disposal fee, and ask if there is a miscellaneous expense. If ‘Y’ it will ask for the amount and call the first calcTravelCost function. If ‘N’ then it will call the first calcTravelCost using a default value. This calcTravelCost will call the second calcTravelCost appropriately. Continue looping for more expenses until -999 is entered for fuel cost.

And here is my program

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
//Joe Snuffy
//CSCI 1010 W1
//PASS 10
/*Space Travel Company program that will calculate
the total cost for travel using reference parameters,
overloaded functions and value returning functions.*/

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

using namespace std;

/*function prototype...the misc variable will be set to a default value
if the parameters are unchanged the default value will be used.*/ 

double calcTravelCost(double &fuelCost, double &foodCost, double &oxyCost, double &wasteCost, double miscCost=0);

double calcTravelCost(double &costPerDay, double &totalCost, int &nbrOfDays);
//declare constant GLOBAL variables
const double OXYGEN_PERDAY = 25.00;
const double FOOD_PERDAY = 35.00;
double fuelCost; 
double fuelTotal;
double foodCost;
double oxyCost;
double wasteCost;
double miscCost;
double totalCost;
double costPerDay;

int nbrOfDays;

//declare additional variables
int main()
{
//variable to hold user choice for Y or N
char userChoice = ' ';
//Prompt user for input
cout<<"Welcome to Joe Snuffy Space Travel Company"<<endl;
cout<<"Please enter fuel cost or -999 to end: ";
cin>>fuelCost;
while(fuelCost != -999)
{
	cout<<"Enter the number of days: ";
    cin>>nbrOfDays;
    cout<<"Enter the waste disposal cost: ";
    cin>>wasteCost;
    cout<<"Is there a miscellaneous cost?  (Y or N): ";
    cin>>userChoice;//character variable
	cout<<endl;

	if(userChoice == 'Y' || userChoice == 'y')
	{ 
		cout<<"Please enter the miscellaneous expense cost: ";
		cin>>miscCost;
		//call to first function
		calcTravelCost(fuelCost, foodCost, oxyCost, wasteCost, miscCost);
		cout<<endl;
		cout<<endl;
		cout<<"The total space travel cost is: "<<endl;
		cout<<"Fuel Cost"<<setw(30)<<fixed<<setprecision(2)<<"$"<<fuelCost<<endl;
		cout<<"Oxygen"<<setw(33)<<fixed<<setprecision(2)<<"$"<<oxyCost<<endl;
		cout<<"Food"<<setw(35)<<fixed<<setprecision(2)<<"$"<<foodCost<<endl;
		cout<<"Waste Disposal"<<setw(25)<<fixed<<setprecision(2)<<"$"<<wasteCost<<endl;
		cout<<"Miscellaneous Cost"<<setw(21)<<fixed<<setprecision(2)<<"$"<<miscCost<<endl;
		cout<<endl;
		cout<<endl;
		cout<<"Total"<<setw(34)<<fixed<<setprecision(2)<<"$"<<totalCost<<endl;
		cout<<endl;
		cout<<endl;
	}

	else if(userChoice == 'N' || userChoice == 'n')
	{
		calcTravelCost(costPerDay, totalCost, nbrOfDays);
		cout<<"The total space travel cost is: "<<endl;
		cout<<"Fuel Cost"<<setw(30)<<fixed<<setprecision(2)<<"$"<<fuelCost<<endl;
		cout<<"Oxygen"<<setw(33)<<fixed<<setprecision(2)<<"$"<<oxyCost<<endl;
		cout<<"Food"<<setw(35)<<fixed<<setprecision(2)<<"$"<<foodCost<<endl;
		cout<<"Waste Disposal"<<setw(25)<<fixed<<setprecision(2)<<"$"<<wasteCost<<endl;
		cout<<endl;
		cout<<endl;
		cout<<"Total"<<setw(34)<<fixed<<setprecision(2)<<"$"<<totalCost<<endl;
		cout<<endl;
		cout<<endl;
	}		
		cout<<"Please enter fuel cost or -999 to end: ";
		cin>>fuelCost;		
}
system("Pause");
return 0;
}//return

//overloaded function defintions
double calcTravelCost(double &fuelCost, double &foodCost, double &oxyCost, double &wasteCost, double miscCost)
{
	fuelTotal = fuelCost * nbrOfDays;
	foodCost = FOOD_PERDAY * nbrOfDays;
	oxyCost = OXYGEN_PERDAY * nbrOfDays;
	totalCost = fuelCost + foodCost + oxyCost + wasteCost + miscCost;
	return totalCost;
}

double calcTravelCost(double &perDay, double &total, int &nbrOfDays)
{
	fuelTotal = fuelCost * nbrOfDays;
	foodCost = FOOD_PERDAY * nbrOfDays;
	oxyCost = OXYGEN_PERDAY * nbrOfDays;
	costPerDay = fuelCost + foodCost + oxyCost + wasteCost;
	totalCost = costPerDay;
	return totalCost;
}
Topic archived. No new replies allowed.