Can't get correct output.

So, I'm writing a program for C++ class assignment. The program works except for one little thing. I can't figure out why it will not show the total cost.

Code is as follows:

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// This is the inventory.h file.
// It contains the Inventory class declaration.

#ifndef INVENTORY_H
#define INVENTORY_H

class Inventory
{
private:
	int itemNumber;
	int quantity;
	double cost;
	double totalCost;
public:
	// Default constructor
	Inventory()
		{ itemNumber = quantity = cost = totalCost = 0; }
	
	// Overloaded constructor
	Inventory(int, int, double);	// Defined in Inventory.cpp

	// Mutators (i.e., "set" functions) defined in Inventory.cpp
	void setItemNumber(int);
	void setQuantity(int);
	void setCost(double);

	// setTotalCost calculates the total cost
	// and stores the result in the totalCost member
	void setTotalCost()
		{ totalCost = cost * quantity; }

	// Accessors (i.e., "get" functions)
	int getItemNumber()
		{ return itemNumber; }
	int getQuantity()
		{ return quantity; }
	double getCost()
		{ return cost; }
	double getTotalCost()
		{ return totalCost; }

	// Input validation functions
	bool validInt(int);
	bool validFloat(double);
};

#endif



// This is the inventory.cpp file.
// It contains the Inventory class function definitions. 

#include <iostream>
#include "Inventory.h"
using namespace std;

//************************************************************
// Overloaded constructor
// Accepts arguments to be stored in each member variable.
//************************************************************
Inventory::Inventory(int in, int q, double c)
{ 
	setItemNumber(in);
	setQuantity(q);
	setCost(c);
	setTotalCost();
}

//************************************************************
// setItemNumber accepts an argument to be stored in item number.
//************************************************************
void Inventory::setItemNumber(int in)
{
	while (!validInt(in))
	{
		cout << "Item Number must be positive. Please re-enter: ";
		cin  >> in;
	}
	itemNumber = in; 
}

//************************************************************
// setQuantity accepts an argument to be stored in quantity.
//************************************************************
void Inventory::setQuantity(int q)
{
	while (!validInt(q))
	{
		cout << "Quantity must be positive. Please re-enter: ";
		cin  >> q;
	}
	quantity = q; 
}

//************************************************************
// setCost accepts an argument to be stored in cost.
//************************************************************
void Inventory::setCost(double c)
{
	while (!validInt(c))
	{
		cout << "Cost must be positive. Please re-enter: ";
		cin  >> c;
	}
	cost = c; 
}

//************************************************************
// The validInt member tests its integer argument to see 
// if it is negative. If the argument is negative, the function 
// returns false. Otherwise, the function returns true.
//************************************************************
bool Inventory::validInt(int value)
{
	if (value < 0)    // the value is negative so it is NOT valid
		return false;
	else              // the integer value is valid
		return true;  
}

//************************************************************
// The validFloat member tests its floating-point argument to see
// if it is negative. If the argument is negative, the function 
// returns false. Otherwise, the function returns true.
//************************************************************
bool Inventory::validFloat(double value)
{
	if (value < 0)    // the value is negative so it is NOT valid
		return false;
	else              // the floating-point value is valid
		return true;
}



//  This is the main.cpp file.

// This program demonstrates the Inventory class
#include<iostream>
#include<iomanip>
#include "Inventory.h"
using namespace std;

void setItemNumber(Inventory &);
void setQuantity(Inventory &);
void setCost(Inventory &);
void setTotalCost(Inventory &);
int getItemNumber();

int main()
{
	Inventory item;
	setTotalCost(item);
	setItemNumber(item);
	setQuantity(item);
	setCost(item);
	cout << fixed << showpoint << setprecision(2) << endl;
	cout << "Item Number   : " << item.getItemNumber() << endl;
	cout << "Quantity      : " << item.getQuantity() << endl;
	cout << "Cost          : $" << item.getCost() << endl;
	cout << "Your Total Cost is $" << item.getTotalCost() << endl;
	
	item.setTotalCost();

	return 0;
}
void setItemNumber(Inventory &item)
{
	int itemNumber;

	// Get data from user
	cout << "Enter the item number \n";
	cout << "Item Number: ";
	cin  >> itemNumber;
	cin.ignore();
	item.setItemNumber(itemNumber);

}

void setQuantity(Inventory &item)
{
	int quantity;

	cout << "Enter the Quantity: ";
	cout << "Quantity: ";
	cin  >> quantity;
	cin.ignore();
	item.setQuantity(quantity);

}

void setCost(Inventory &item)
{
	double cost;

	cout << "Enter the Cost: ";
	cout << "Cost: ";
	cin  >> cost;
	cin.ignore();
	item.setCost(cost);
}

void setTotalCost(Inventory &item)
{
	double totalCost;

	item.setTotalCost();
	item.getItemNumber();
}


I don't know what I'm doing wrong. I thought it should work. Any help would be greatly appreciated.

Last edited on
You need to call item.setTotalCost() BEFORE you call item.getTotalCost(). Just move it up after setCost( item );
That worked perfectly. It was so simple that I completely overlooked it. Thank you very much.
Topic archived. No new replies allowed.