Beginner C++ Student

I get the must return a value error for the following code and can't figure out how exactly to call or reference the class so that I can access and call the members in the main(). Thank you all in advance for any solutions or advice.

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

class Inventory {
private:
	int itemNumber,
		quantity;
	double cost;
	double TotalCost;
public:
	Inventory()
	{
		itemNumber = 0; quantity = 0;
		cost = 0;
	}
	int setItemNumber()
	{
		cin >> itemNumber;
	}
	int setQuantity()
	{
		cin >> quantity;
	}
	double setCost()
	{
		cin >> cost;
	}
	int getItemNumber()
	{
		return setItemNumber();
	}
	int getQuantity()
	{
		return setQuantity();
	}
	double getCost()
	{
		return setCost();
	}
	double getTotalCost()
	{
		TotalCost = (quantity * cost);
	}
};

int main()
{
	Inventory inv;
	cout << "Enter item number: ";
	inv.setItemNumber();
	cout << endl;
	cout << "Enter the quantity: ";
	inv.setQuantity();
	cout << endl;
	cout << "Enter the cost: ";
	inv.setCost();
	cout << endl;
	cout << "The total cost is $" << inv.getTotalCost() << endl;

	system("pause");
	return 0;
}
Last edited on
Lets pick one:
1
2
3
4
5
6
7
8
9
10
double Inventory::getTotalCost()
{
  TotalCost = (quantity * cost);
}

int main()
{
  Inventory inv;
  std::cout << inv.getTotalCost();
}

On line 1 you do promise that calling getTotalCost() will give you a double value.
On line 9 you call the function (properly) and clearly expect to get a value.

What value does the function return?
Look carefully at the body of the function (on lines 2-4).
What value do you return?
@keskiverto
I think I understand the mistake I am making now. I didn't return a value at all, correct?
I was able able to achieve a working program with the following changes to the class:
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
int setItemNumber()
	{
		cin >> itemNumber;
		return itemNumber;
	}
	int setQuantity()
	{
		cin >> quantity;
		return quantity;
	}
	double setCost()
	{
		cin >> cost;
		return cost;
	}
	int getItemNumber()
	{
		return itemNumber;
	}
	int getQuantity()
	{
		return quantity;
	}
	double getCost()
	{
		return cost;
	}
	double getTotalCost()
	{
		TotalCost = (quantity * cost);
		return TotalCost;
	}
Is this the best way to rewrite this or am I still slightly off?
Last edited on
Please use code tags. Enter [code] and [/code] around the code that you paste into your posts. You can also use the "<>" Format button to generate these tags for you (although this button never works when creating a new thread).

You probably resolved your set...() functions the wrong way. When you set a value into a data member, that's probably all you want to do. So, you might want your set...() functions to have a return type of void and then not return anything.

Of course, maybe you do want to set the value and immediately use it where the function was called. In that case, your code is correct, but the function name is misleading.

Get used to paying attention to what you name your functions. The names should give you a good idea of what the function does. When I read set...(), I assume that a value is being set and that is all--no need for a return value. If you want to set the value and return it, another name, like setAndGet...() might be easier to identify (although maybe a bit clumsier to use). In my mind, having 2 simple functions set...() and get...() is easier to understand, and calling them back-to-back is better than creating another function to do both.
Ahh ok I will keep the code formatting in posts in mind thank you. New here and still getting used to all the rules and behaviors. Thank you very much that is what I was doing wrong I meant to write the set() functions with void not int and double. I was able to remove the redundant return commands in the setters and run the program successfully. Thank you for your help both @keskiverto and @doug4. :)
Last edited on
Topic archived. No new replies allowed.