Call By reference, and returning wrong valu!

Write your question here.

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
  #include<iostream>
using namespace std;

void getInput(int& numOfDays, double& itemPrice);

double total(int& numOfDays, double& itemPrice);

void displayResults();

int main()
{
	int numOfDays;
	double itemPrice;

	getInput(numOfDays, itemPrice);

	total(numOfDays, itemPrice);

	displayResults();
}

void getInput(int& numOfDays, double& itemPrice)
{
	cout << "Please enter item price: $";
	cin >> itemPrice;

	while (itemPrice <= 0)
	{
		cout << "Invalid Input. Please enter a positive value: ";
		cin >> itemPrice;
	}

	cout << "\nHow many days until the item is sold?: ";
	cin >> numOfDays;

	while (numOfDays <= 0)
	{
		cout << "Invalid Input. Please enter a positive value: ";
		cin >> numOfDays;
	}

}


double total(int& numOfDays, double& itemPrice)
{
	double total;

	if (numOfDays <= 7)
	{
		total = itemPrice + (itemPrice * 0.05);
		return total;
	}

	else
	{
		total = itemPrice + (itemPrice * 0.10);
		return total;
	}

}

void displayResults()
{
	cout << "\nThe total is: " << total << "\n" << endl;
}


So this program is supposed to calculate the price of an item, and return it back as the variable total of type int. but for some reason it gives me a weird output on total. And am i using call by reference correct? sorry, i am just learning. Please help out!
> cout << "\nThe total is: " << total << "\n" << endl;
`total' is the name of a function, declared in line 6.
http://www.cplusplus.com/doc/tutorial/namespaces/ (scope)
Topic archived. No new replies allowed.