I'm working through an old C++ textbook I found and I'm trying to understand classes. The file I am working on is called Grocery.cpp. I defined a class called GroceryItem and then defined a public function called dataEntry(). From within the dataEntry function, I'm calling four private functions to get a stock number, price and a quantity. I can successfully retrieve the user input but somehow the functions are not storing the input because my displayGroceryItem function doesn't do what it should which is to display what the user has entered (a stock number, a price and a quantity). The output does prompt a user for the input correctly but when the displayGroceryItem function is called, the fields are all display zero.
I've searched these forums and cplusplus.com but have come up short. Google keeps directing me to Chegg.com but it supposedly shows the solution which won't help me understand the concept at all.
I want to do this on my own and it's driving me nuts. I'm looking for tips or advice or even an "I can see the problem and it's at line #someLineNumber" but not finished code. Can anyone see how I goofed this up? Am I not calling the function correctly or did I not write the function correctly? I'm at a loss.
Any hints or advice is much appreciated!
The whole code follows:
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class GroceryItem
{
private:
int stockNum;
double price;
int quantityInStock;
double totalValue;
// Three private functions to prompt for user input
int getStockNum();
double getPrice();
int getQuantityInStock();
double calculateTotal(); // Fourth private function to calculate total value (price times quantity in stock)
public:
void dataEntry(int, double, int);
void setStockNum(int);
void setPrice(double);
void setQuantity(int);
void displayGroceryItem(int, double, int, double); // Displays a GroceryItem’s values.
};
int GroceryItem::getStockNum()
{
int itemNum = 0;
stockNum = itemNum;
int LOW = 1000;
int HIGH = 9999;
int stockNum = itemNum;
cout << "Enter an item number >> ";
cin >> itemNum;
while(itemNum < LOW || itemNum > HIGH)
{
cout << "Invalid item number. Enter a valid item number between 1000 and 9999 >> ";
cin >> itemNum;
}
return stockNum;
}
double GroceryItem::getPrice()
{
double itemPrice = 0;
price = itemPrice;
cout << "Enter the item price >> ";
cin >> itemPrice;
if(itemPrice < 0)
{
cout << "Invalid price. Enter a positive number >> ";
cin >> itemPrice;
}
return price;
};
int GroceryItem::getQuantityInStock()
{
int itemQuantity = 0;
quantityInStock = itemQuantity;
cout << "Enter a quantity >> ";
cin >> itemQuantity;
if(itemQuantity < 0)
{
cout << "Invalid quantity. Enter a positive number >> ";
cin >> itemQuantity;
}
return quantityInStock;
};
double GroceryItem::calculateTotal()
{
int itemQuantity = 0;
double itemPrice = 0, totalValue;
quantityInStock = itemQuantity;
price = itemPrice;
totalValue = itemQuantity * itemPrice;
return totalValue;
}
void GroceryItem::setStockNum(int itemNum)
{
stockNum = itemNum;
}
void GroceryItem::setPrice(double itemPrice)
{
price = itemPrice;
displayGroceryItem(stockNum, price, quantityInStock, totalValue);
}
void GroceryItem::setQuantity(int itemQuantity)
{
quantityInStock = itemQuantity;
}
void GroceryItem::displayGroceryItem(int itemNum, double itemPrice, int itemQuantity, double grandTotal)
{
this->stockNum = itemNum;
this->totalValue = grandTotal;
this->quantityInStock = itemQuantity;
this->price = itemPrice;
totalValue = quantityInStock * price;
cout << "The stock number is #" << itemNum << endl <<
"The price of the item is $" << itemPrice << endl <<
"The quantity in stock is " << itemQuantity << endl <<
"Total value is: $" << totalValue << endl;
}
void GroceryItem::dataEntry(int, double, int)
{
GroceryItem::getStockNum();
GroceryItem::getPrice();
GroceryItem::getQuantityInStock();
};
int main()
{
GroceryItem stock;
int stockNum, quantityInStock, itemNum, quantity;
double price, totalValue, itemPrice, grandTotal;
stock.dataEntry(stockNum, price, quantityInStock); // call the dataEntry function
stock.displayGroceryItem(stockNum, price, quantityInStock, totalValue); // display what the user entered from the dataEntry function
return 0;
}