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.
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:
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?
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. :)