Hello All,

I am trying to read a short inventory list from a .txt file into a class of objects. To be plain and simple, I have no idea how to get the details in the file into objects to I can prompt for the pluCode and quantity to use them in calculations. Here's my code so far;

Header:

#include "stdafx.h"
#include <iostream>
//#include <ctime>
//#include <cstdlib>
#include <iomanip>
//#include <cmath>
#include <fstream>
//#include <string>
using namespace std;

//ShoppingCart class definition
class ShoppingCart
{
public:
	int setStore( int, string, int, double, double);	//setStore function
	void Shoppingcart( string );	//constructor
	int checkout(); //checkout function

private:
	int pluCode;	//product PLU code
	string prodName;	//product name
	int prodType;	//product type, weighted or units
	double unitPrice;	//product weighted/unit price
	double inStock;	//current total in stock

	double purchaseQty;	//purchase quantity
	double subtotal;	//purchase subtotal
	double total;	//purchase total
	double inventoryQty;	//updated inventory quantity
}	//e

.cpp

#include "stdafx.h"
#include <iostream>
//#include <ctime>
//#include <cstdlib>
#include <iomanip>
//#include <cmath>
#include <fstream>
#include <string>
#include "ShoppingCart.h"		//include definition of ShoppingCart class

using namespace std;

int ShoppingCart::setStore( int pluCode, string prodName, int prodType, double unitPrice, double inStock)	//constructor
{
	ShoppingCart myCart[22];

	string product;
	ifstream myfile ("inventory.txt");

	if (myfile.is_open())	//if myfile is successfully opened
	{
		cout << "Here is our inventory:\n" << endl;

		while (! myfile.eof() )
		{
			getline (myfile,product);
			cout << product << endl;
		}
		myfile.close();
	}//End if

	system("pause");
	return 0;
}

int ShoppingCart::checkout()
{
	cout << "Please enter the PLUcode for the produce you would like to buy :";
	cin >> pluCode;
}

...and here is an excerpt from the file content;

4101 BRAEBURN_REG 1 0.99 101.5
4021 DELICIOUS_GDN_REG 1 0.89 94.2
4020 DELICIOUS_GLDN_LG 1 1.09 84.2
4015 DELICIOUS_RED_REG 1 1.19 75.3
4016 DELICIOUS_RED_LG 1 1.29 45.6

I will greatly appreciate help in

void Shoppingcart( string );	//constructor

This isn't a constructor. If you think this is a constructor, you need to take a tutorial on classes, then come back to this.

int ShoppingCart::setStore( int pluCode, string prodName, int prodType, double unitPrice, double inStock)	//constructor

Neither is this. See comment above.

ShoppingCart myCart[22];

This is never used in the function, so it serves no purpose. Why have it?


All in all, if you are this lost, delete all of the code, put the assignment aside entirely, learn some class basics, then come back to it.

This article has been dead for over six months. Start a new discussion instead.