Wrong answer in Output. Not calculating.

Whenever this program runs, the repeated statements gives off-the-wall numbers. Thanks for any help. There are no errors and it compiles correctly.

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
#ifndef INVENTORY_H
#define INVENTORY_H

class Inventory
{
	private:
		int itemNumber;
		int quantity;
		double cost;
		double totalCost;

	public:
		Inventory()
		{
			itemNumber = 0;
			quantity = 0;
			cost = 0;
			totalCost = 0;
		}

		Inventory(int itemNumber, int quantity, double cost)
		{
			itemNumber = getItemNumber();
			quantity = getQuantity();
			cost = getCost();
			setTotalCost(quantity, cost);
		}

		void setItemNumber(int)
		{
			itemNumber = itemNumber;
		}

		void setQuantity(int)
		{
			quantity = quantity;
		}

		void setCost(double)
		{
			cost = cost;
		}

		void setTotalCost(int, double)
		{
			totalCost = quantity * cost;
		}

		int getItemNumber()
		{
			return itemNumber;
		}

		int getQuantity()
		{
			return quantity;
		}

		double getCost()
		{
			return cost;
		}

		double getTotalCost()
		{
			return totalCost;
		}
};

#endif 


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
#include <iostream>
#include <iomanip>
#include "Inventory.h"
using namespace std;

char again;

int main()
{
	do
	{
		int itemNumber;
		int quantity;
		double cost;
		double totalCost;

		cout << "Inventory Class" << endl;
		cout << "---------------" << endl << endl;

		cout << "Item Number: ";
		cin >> itemNumber;
		if (itemNumber < 0)
		{
			cout << "Item Number invalid." << endl; 
			cout << "Item Number: ";
			cin >> itemNumber;
		}

		cout << "Quantity: ";
		cin >> quantity;
		if (quantity < 0)
		{
			cout << "Quantity invalid." << endl;
			cout << "Quantity: ";
			cin >> quantity;
		}

		cout << "Cost: ";
		cin >> cost;
		if (cost < 0)
		{
			cout << "Cost Invalid." << endl;
			cout << "Cost: ";
			cin >> cost;
		}

		cout << endl << "---------------" << endl;

		Inventory inventory1(itemNumber, quantity, cost);

		itemNumber = inventory1.getItemNumber();
		cout << "ItemNumber: " << itemNumber << endl;

		quantity = inventory1.getQuantity();
		cout << "Quantity: " << quantity << endl;

		cost = inventory1.getCost();
		cout << "Cost: " << cost << endl;

		totalCost = inventory1.getTotalCost();
		cout << "Total Cost: " << fixed << setprecision(2) << totalCost << endl << endl;

		cout << "Do you want to run this program again? Y/N: ";
		cin >> again;
	} while (again == 'y' || again == 'Y');
		return 0;
}
Last edited on
1
2
3
4
5
6
7
		Inventory(int itemNumber, int quantity, double cost)
		{
			itemNumber = getItemNumber();
			quantity = getQuantity();
			cost = getCost();
			setTotalCost(quantity, cost);
		}

`itemNumber' refers to the parameter. You are setting it to the result of `getItemNumber();'
¿does that make sense to you?
I understand what you are saying. But does that mean I would enter something like...

itemNumber = iNumber;
quantity = quan;
cost = c;
?
and change it in the rest of my code?
What are you trying to do with that function? Is it supposed to obtain the values of itemNumber, quantity and cost and pass them back to the calling code?

Edit: Oh, wait, it's a constructor. It's supposed to be setting the values of the data members of the Inventory class, right?

Well, you've got a problem, because you've chosen your parameter names to be identical to the names of the data members. This means that they will mask the data member names, so that whenever the compiler sees the word "itemNumber" within the constructor, it will think you mean the parameter, and not the data member. So this won't do what you want it to do.

This is one of the reasons why most tutorial sources will recommend you adopt a special naming convention for you data members, e.g. prefixing them with "m_", so that you don't accidentally mask them with a parameter name or local variable.

Did your compiler not warn you about this when you compiled it?
Last edited on
Just fixed it. Thanks for the help guys.

No, my compiler didn't warn me about it. Silly me, I figured since no error or warning was given, I was good.

Huh... I'm surprised by that. Name masking is one of those things I would expect any good compiler to warn you about.
I guess this is why many people make their parameters const, so at least assigning to them would give an error.
I guess so, though it seems a bit overkill to me.

FWIW, I've just tested with g++ 4.5.1, and it doesn't utter a peep. I find that pretty shocking :O

Edit: Nor does Visual C++ 2010.
Last edited on
Topic archived. No new replies allowed.