3 Replies - 287 Views - Last Post: 13 May 2013 - 03:29 PM Rate Topic: -----

#1 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 07-October 12

C++ User Menu

Posted 13 May 2013 - 02:50 PM

Hello, so I'm starting to create a user menu but I'm having trouble getting even the first option to work. When the user enters 'I' I want to be able to show the entire inventory again. But for some reason, when it goes to showInventory function, it completely skips both of my for loops. Does anyone know what the problem is?


#include "StdAfx.h"
#include <iostream>
#include "MemTracker.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
#pragma warning(disable:4996)
#pragma warning(disable:4700)
#define ONEK 1024
#define FILE_NAME "inventory1.csv"


class Utility{
public:

	char makeChar(char* pchar){return *pchar;}
	int get_number(char* pnumber){return atoi(pnumber);}
	int makeInteger(char* pnumber){return atoi(pnumber);}
	double makeReal(char* pnumber){
		double dnumber;
		sscanf(pnumber,"%lf",&dnumber);
		return dnumber;
	}
	char* makeString(char* pstring){
		char* ptr = new char[strlen(pstring)+1]; //char* ptr = (char*)malloc(strlen(pstring)+1);
		strcpy(ptr,pstring);
		return ptr;
	}
};//Utility
class Container
{
private:
	int count;
	char** lines;
	int nlines;
	Utility utility;
public:
	Container(int);
	~Container();
	int Container::getCount(){return count;}
	int Container::getLineCount(){return nlines;}
	char* Container::getLine(int count){return lines[count];}
	int Container::addLine(char* line){
		lines[count++] = utility.makeString(line);
		return nlines; 
	}

bool Container::areLinesEmpty(){
	if (lines) return false;
		else return true;
	}
char ** Container::returnLines() {return lines;}
};//Container
Container::Container(int n){
	count = 0;
	nlines = n;
	lines = new char*[nlines];
}
Container::~Container(){
	for (int x = 0; x < nlines; x++)
	delete [] lines[x];
	if (lines)
	delete [] lines;
	lines = NULL;
}

class FileHandler{
private:
	ifstream infile;
	char* line;
	Container * container;
public:
	FileHandler(ifstream& infile);
	FileHandler(char*){
		infile.open(FILE_NAME);
		int count = countLines(infile);
		container = new Container(count);
	}
	~FileHandler();
	int countLines(ifstream& infile);
	bool isOpen(ifstream& infile);
	int processFile(ifstream& myfile);
	void deleteFile();
	Container* returnContainer();
};//FileHandler

FileHandler::~FileHandler(){
	infile.close();
	int x = 0;
	if (container->areLinesEmpty()){
		for(x = 0; x < container->getLineCount(); x++)
			delete [] (*(container->returnLines()+x));
			delete [] (*(container->returnLines()));
	}
	if (container)
		delete container;
		container = NULL;
}
int FileHandler::countLines(ifstream& myfile){
	int count = 0;
	if (myfile){
		char buffer[256];
		while (!myfile.eof()){
		if (myfile.getline(buffer,256)) count++;
		}
	}
	return count;
}
bool FileHandler::isOpen(ifstream& myfile){
	if (myfile) return true;
	else return false;
}
int FileHandler::processFile(ifstream& myfile){
	if (isOpen(myfile)){
		char buffer[256];
		while (!myfile.eof()){ 
			if (myfile.getline(buffer,256))
				container->addLine(buffer);
		}
	}
	else{
	cout <<"Unable to open file" << FILE_NAME << "\n";
	return 0;
	}
	myfile.close();
	return container->getCount();
}
void FileHandler::deleteFile(){
	int x = 0;
	if (container->areLinesEmpty()){
		for(x = 0; x < container->getLineCount(); x++)
			delete(*(container->returnLines()+x));
			delete(*(container->returnLines()));
	}
	delete(container);
}
Container* FileHandler::returnContainer(){
return container;
}

class Book{
private:
	char* title;
	char* author;
	char* isbn;
	char format; //P = paperback, H = hardback
	int itemNumber; //inventory unique identifier
	int year; //year published
	int nPages;
	double price;
	int CommaCheck;
	Utility * utility;
public:
	Book();
	~Book();
	void initBook(char* buff, int lineNumber);
	void showBook();
	//void checkBook(int lineNumber);
};//Book
Book::Book(){
title = NULL;
author = NULL;
isbn = NULL;
}
Book::~Book(){
	if (title)
		delete [] title;
	if (author)
		delete [] author;
	if (isbn)
		delete [] isbn;
	title = NULL;
	author = NULL;
	isbn = NULL;
}

void Book::initBook(char* buff, int lineNumber){
	itemNumber = utility->get_number(strtok(buff, ","));
	if( (itemNumber/1000)==4){
		isbn = utility->makeString(strtok(NULL, ","));
		title = utility->makeString(strtok(NULL, ","));
		author = utility->makeString(strtok(NULL, ","));
		year = utility->makeInteger(strtok(NULL, ","));
		format = utility->makeChar(strtok(NULL, ","));
		nPages = utility->makeInteger(strtok(NULL,","));
		price = utility->makeReal(strtok(NULL,","));
			if(strtok(NULL,",") != NULL)
				CommaCheck = 1;
	}
}
void Book::showBook(){
// note: not all members of book are displayed
	if((itemNumber/1000)==4){
		cout.precision(2);
		cout <<"Item " << itemNumber << ": \"" << title << "\" by " << author << ". $" << fixed << price << endl;
	}
}
/*
void Book::checkBook(int lineNumber){
	if((itemNumber/1000)==4){
		if((int)(log10((float)itemNumber) + 1) != 4)
			cout << "line(" << lineNumber << "): item# must be a 3-digit number " << endl;
		if(strlen(isbn) != 10)
			cout << "line(" << lineNumber << "): isbn must be a 10-digit number " << endl;
		if((int)(log10((float)year)+1) != 4)
			cout << "line(" << lineNumber << "): invalid format for year " << endl;
		if(CommaCheck == 1)
			cout <<"line(" << lineNumber << "): wrong number of commas. " << endl;
	}
}
*/
class DVD{
private:
	int itemNumber;
	int year;
	double price;
	char* star1;
	char* star2;
	char* title;
	char* upc;
	int CommaCheck;
	Utility* utility;
public:
	DVD();
	~DVD();
	void initDVD(char* buff, int lineNumber){
	itemNumber = utility->get_number(strtok(buff, ","));
	if((itemNumber/1000)==7){
		upc = utility->makeString(strtok(NULL, ","));
		title = utility->makeString(strtok(NULL, ","));
		year = utility->makeInteger(strtok(NULL, ","));
		star1 = utility->makeString(strtok(NULL, ","));
		star2 = utility->makeString(strtok(NULL, ","));
		price = utility->makeReal(strtok(NULL,","));
			if(strtok(NULL,",") != NULL)
				CommaCheck = 1;
	}
}
	void showDVD(){
		if(itemNumber/1000 == 7){
		cout.precision(2);
		cout <<"Item " << itemNumber << ": \"" << title << "\" (" << year << ") Starring " << star1 << ". $" << price << endl;
		}
	}

};//DVD

DVD::DVD(){
	title = NULL;
	upc = NULL;
	star1 = NULL;
	star2 = NULL;
}
DVD::~DVD(){
	if (title)
		delete [] title;
	if(upc)
		delete [] upc;
	if(star1)
		delete[] star1;
	if(star2)
		delete[] star2;
	title = NULL;
	upc = NULL;
	star1 = NULL;
	star2 = NULL;
}


class Inventory{
private:
	Book* books; //an array of books
	DVD* dvds; //an array of dvds
	int nbooks; //num of books in inventory	
	int numB;
	int numD;
	
public:
	Inventory();
	Inventory(int);
	~Inventory();
	int fillInventory(Container* cp);
	void showInventory();
	
	//void checkInventory();
};//Inventory
Inventory::Inventory(){
	books = NULL;
	dvds = NULL;
	nbooks = 0;
	numB = 0;
	numD = 0;
}
Inventory::Inventory(int n){
	nbooks = n;
	books = new Book[n]; //(struct Book*) malloc(sizeof(struct Book)*inventory->nbooks);
	dvds = new DVD[n];
	
}
Inventory::~Inventory(){
	if (books)
		delete [] books;
		books = NULL;
	if(dvds)
		delete [] dvds;
		dvds = NULL;
}

int Inventory::fillInventory(Container* container){
	int current = 0;
	Inventory(container->getLineCount());
	numB = 0;
	numD = 0;
	if (container->getLineCount() != NULL){
		for (current = 0; current < container->getLineCount(); current++){
			char* temp = container->getLine(current);
			char* temp2 = new char[1000];
			strcpy(temp2,temp);
			int num = atoi(temp2);
				if(num < 5000){
					books[numB].initBook(container->getLine(current),current);
					numB++;
					delete [] temp2;
				}
				else{
					dvds[numD].initDVD(container->getLine(current),current);
					numD++;
					delete [] temp2;
				}
		}
	}
	return nbooks+numD;
}
void inline Inventory::showInventory(){
	int x = 0;
	int y = 0;
	cout <<"Books in inventory: " << endl;
	for (x = 0; x < nbooks; x++)
		books[x].showBook();
	cout <<"DVDs in inventory: " <<endl;
	for (y = 0; y < numD; y++)
		dvds[y].showDVD();

}
class Cart{
private:
	char items[10];
	int amountB;
	int amountD;
	Inventory inventory;
public:
	Cart();
	~Cart();
	void printMenu(){
		char choice;
			cout << "Press (I) to see inventory. " << endl;
			cout << "Press (A) to add an item to cart. " << endl;
			cout << "Press (C) to see cart." << endl;
			cout << "Press (E) to exit." << endl;
			cin >> choice;
			toupper(choice);
			processChoice(choice);
	}
	void processChoice(char userChoice){
		switch(userChoice){
		case 'I':
			inventory.showInventory();
		case 'A':
			fillCart();
		case 'C':
			showCart();
		case 'E':
			break;
		default:
			cout << "Choice does not match any menu option. Please select again" << endl;
	}
	}
	int fillCart();
	void showCart(){
		int x;
		for( x = 0; x < items[10]; x++)
			cout << items << endl;
	}
}; //Cart
Cart::Cart(){

	amountB = 0;
	amountD = 0;
}
Cart::~Cart(){

}
int inline Cart::fillCart(){
	int x = 0;

	return x;
}
/*
void Inventory::checkInventory(){
	int z = 0;
	cout << "\n\nErrors encountered during processing: " << endl;
	for(z = 0; z < nbooks; z++)
		books[z].checkBook(z);
}
*/

int main(){
	FileHandler fhandle(FILE_NAME);
	ifstream myfile(FILE_NAME);
	cout <<"Processing File from: "<< FILE_NAME << endl;
	if (fhandle.processFile(myfile)){
		cout << "Filling Inventory... " << endl;
		int nbooks = fhandle.returnContainer()->getLineCount();
		Inventory inventory(nbooks);
		inventory.fillInventory(fhandle.returnContainer());
		inventory.showInventory();
		Cart cart;
		cart.printMenu();
		//inventory.checkInventory();
		cout <<"Cleaning up...." << endl;
	}
	return 0;
}


-Sorry for posting so much code. I'm not sure what parts people might want. But the problem that I'm concerned with is the last 2 classes at the bottom of my program.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ User Menu

#2 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 07-October 12

Re: C++ User Menu

Posted 13 May 2013 - 03:02 PM

Okay, after playing around with my code some more, I get the problem. The inventory destructor is called before my menu even prints and so, everything is set to 0. But how can I get around this? Is there a way to delay the destructor call until after my program is done running?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: C++ User Menu

Posted 13 May 2013 - 03:22 PM

Uh...the Inventory variable in the Cart class -- a member variable -- has nothing whatsoever to do with the Inventory variable in your main.
Was This Post Helpful? 0
  • +
  • -

#4 hotshotennis  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 98
  • Joined: 07-October 12

Re: C++ User Menu

Posted 13 May 2013 - 03:29 PM

View PostJackOfAllTrades, on 13 May 2013 - 03:22 PM, said:

Uh...the Inventory variable in the Cart class -- a member variable -- has nothing whatsoever to do with the Inventory variable in your main.


I feel like I need the one in my cart class to access showInventory function and the one in main to access all the other inventory functions. Is there a different way to do it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1