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.
// 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"
usingnamespace 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
returnfalse;
else // the integer value is valid
returntrue;
}
//************************************************************
// 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
returnfalse;
else // the floating-point value is valid
returntrue;
}
// This is the main.cpp file.
// This program demonstrates the Inventory class
#include<iostream>
#include<iomanip>
#include "Inventory.h"
usingnamespace 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.