Need help with a value-returning function

Hello everyone,
Been working on this program and have it all put together except of one problem.
I need to return a total of the overall amount of lumber ordered.
Example:
Enter item: P 10 2 4 8
10 2x4x8 Pine, cost: $47.47
Enter item 1 1 12 8
1 1x12x8 Maple, cost: $36.00
Enter item: T
Total cost: $83.47

I can't get it to return the Total cost. Hoping someone can tell me what I am doing wrong or show me what I need to fix. Thank you all
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
#include <iostream>
#include <fstream> 

using namespace std;



float bCalculation(char bType,float& bNumb,float& bWide,float& bHigh,float& bLong,float& tCost,float total);
void bTotal(char bType, float& total, float tCost);
int main()
{   
 char bType;
 float bNumb;
 float bWide = 0;
 float bHigh = 0;
 float bLong = 0;
 float tCost = 0;
 float total =0;
	do
	{
cout << "Enter Wood Type" << endl
<< "P = Pine," << " F = Fir," << " C = Ceder," << " M = Maple," 
<< " O = Oak," << " T = Total Price" << endl;
cin >> bType;
		
if(toupper(bType) == 'T')
{
bTotal(bType,total,tCost);		
}
		

cout << endl << "Enter Amount, Width (in), Height(in), Length(ft),seperated by space" << endl;
cin >> bNumb >> bWide >> bHigh >> bLong;
cout << endl;

while (bNumb <= 0 || bWide <= 0 || bHigh <= 0 || bLong <= 0)
{
cout <<"Incorrect Input, Plese enter number of Pieces, Width, Height,Length" << endl;
cin >> bType >> bNumb >> bWide >> bHigh >> bLong;
cout << endl;
}
		
bCalculation(bType,bNumb,bWide,bHigh,bLong,tCost,total);
				
switch(toupper(bType))
{
case 'P':cout << bNumb << " " << bWide << "x" << bHigh << "x" << bLong << " " << " Pine " << tCost << endl ;
break;
case 'F':cout << bNumb << " " << bWide << "x" << bHigh << "x" << bLong << " " << " Fir " << tCost << endl ;
break;
case 'C':cout << bNumb << " " << bWide << "x" << bHigh << "x" << bLong << " " << " Ceader " << tCost << endl ;
break;
case 'M' :cout << bNumb << " " << bWide << "x" << bHigh << "x" << bLong << " " << " Maple " << tCost << endl ;
break;
case 'O' :cout << bNumb << " " << bWide << "x" << bHigh << "x" << bLong << " " << " Oak " << tCost << endl ;
break;
case 'T':
default :cout << "Incorrect Input, Please use letter (P, F, C, M, O) for wood type,or T for totla." <<endl;
}
		
}while(bNumb,bWide, bHigh, bLong );
return 0;
}
void bTotal(char bType,float& total,float tCost)
{
	tCost= total;
	total = total + tCost;	
	cout << "total" << total;

}

float bCalculation(char bType,float& bNumb,float& bWide,float& bHigh,float& bLong,float& tCost,float total)
{

	float tMessure;
	float sCost;
	float bPrice =0;
	
	if(toupper(bType) == 'P')
		bPrice = 0.89;
	 if(toupper(bType) == 'F')
		bPrice = 1.09;
	 if(toupper(bType) == 'C')
		bPrice = 2.26;
	 if(toupper(bType) == 'M')
		bPrice = 4.50;
	 if(toupper(bType) == 'O')
		bPrice = 3.10;
	 
	tMessure = bWide * bHigh *(bLong * 12);
	cout << tMessure << endl;
	sCost = (tMessure / 144)* bPrice;
	cout << sCost << endl;
	tCost = sCost * bNumb;
	
return tCost;
}
You've got some logic errors to sort out as well but this should fix your total.

line 8
1
2
float bCalculation(char bType,float& bNumb,float& bWide,float& bHigh,float& bLong,float& tCost,float & total); 
//make total a reference since we want to change it 


1
2
3
4
5
6
7
void bTotal(char bType,float& total,float tCost)
{
	//tCost= total; // don't need now
	//total = total + tCost; //don't need now	
	cout << "total" << total;

}


line 72
1
2
3
4
5
6
7
8
9
10
float bCalculation(char bType,float& bNumb,float& bWide,float& bHigh,float& bLong,float& tCost,float& total)
// change to match prototype

//...

	tCost = sCost * bNumb;
	total += tCost; // add the cost for each item to total when we calculate it

return tCost;
}



Last edited on
Thank you so much vin
It works now with your help. I also have changed the program around a little. Instead of using a do-while loop I am using just a while loop.
I could not figure out how to add the total of each item, for the overall total. Thank you so much for your help again.
Topic archived. No new replies allowed.