I am studying Object-Oriented Programming Using C++ (Fourth Edition) by Joyce Farrell and I'm currently on Chapter 7 (focuses on using classes), exercise 3 by myself (wish I could find an irc channel or some kind of study group, but I hate debugging...)
There aren't any syntax errors, but my program oddly enough stops after around the code line 81. I can enter the data in, but the console will just close out like it's finished. I even typed apple.dataEntry() twice just to see if that function will actually repeat and it does. Any clue why it's doing that?
#include "stdafx.h"
#include<iostream>
#include<string>
usingnamespace std;
class GroceryItem{
private:
int stockNum;
double price;
int quantityInStock;
double totalValue;
int askStockNum();
double askPrice();
int askQuantity();
void total();
public:
int getStockNum();
double getPrice();
int getQuantity();
double getTotal();
void dataEntry();
};
int GroceryItem::askStockNum(){
int x;
cout<<"How much stock is there? ";
cin>>x;
while(x < 1000 || x > 9000){
cout<<"That number is not valid. Please enter a value between 1000 and 9000.";
cin>>x;
}
return x;
}
double GroceryItem::askPrice(){
double x;
cout<<"How much does this item cost? ";
cin>>x;
while(x <= 0){
cout<<"That number is not valid. Please enter a value greater than 0. ";
cin>>x;
}
return x;
}
int GroceryItem::askQuantity(){
int x;
cout<<"How much is in stock? ";
cin>>x;
while(x <= 0){
cout<<"That number is not valid. Please enter a value greater than 0. ";
cin>>x;
}
return x;
}
double GroceryItem::getTotal(){
return totalValue;
}
void GroceryItem::total(){
double x;
totalValue = price * quantityInStock;
}
int GroceryItem::getStockNum(){return stockNum;}
double GroceryItem::getPrice(){return price;}
int GroceryItem::getQuantity(){return quantityInStock;}
void GroceryItem::dataEntry(){
askStockNum();
askPrice();
askQuantity();
}
int _tmain(int argc, _TCHAR* argv[]){
GroceryItem apple;
void displayValues(GroceryItem);
apple.dataEntry();
apple.dataEntry();
displayValues(apple);
cout << "Press ENTER to continue...";
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
return 0;}
void displayValues(GroceryItem x){
cout<<"Number in stock: "<<x.getStockNum()<<endl<<"Price: $"<<x.getPrice()<<endl<<
"Quantity in stock: "<<x.getQuantity()<<endl<<"Total: $"<<x.getTotal();
}
You say "the console will just close out like it's finished." Isn't that because it IS finished? After it gets the data, it displays it but then immediately ends, without giving you time to look at it. Your cin.ignore statement there seems to not stop the program from ending. Try other things, like cin.get() or just cin >>.