5 Replies - 3086 Views - Last Post: 25 September 2012 - 09:17 PM Rate Topic: -----

#1 msdiddyj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 12

Reading .txt into parallel array and writing to new file

Posted 25 September 2012 - 07:09 PM

Okay so I have another homework assignment that I just can't seem to figure out where I'm going wrong. The assignment is as follows:

Write a C++ program to perform grocery check-out procedure for a simple store with max 100 products utilizing a class.

When the program starts, it should read the product information file (inventory.txt - create it using the data below) - it contains product information (PLU code, product name, product sales type, price per pound or price per unit & current inventory level) - one product in each line.

(file attached)

Then, program should repeatedly invoke customer check-out functionality until the store associate (user) decides to quit. As part of checkout functionality, prompt for PLU code, validate it, then the user to input weight for each product if it is sold by weight, or # of units if sold by unit. Compute the price of the item and keep up the subtotal. Once all purchased products are rung for a customer, output the total purchase amount. If the total purchase exceeds $50, apply 5% discount to the total. Keep the checkout functionality in a separate method.

We need to keep track of inventories automatically as well. So, keep updating the inventory data along with checkout operations. When the store closes every day, product information file should be saved in output.txt in the same format as input inventory file.


Okay, my issue is that no matter what it will only read the first line, so no matter what PLU code I put in there, only the information for 4101 comes up. Another thing is that the new inventory file does not update the inventory. I have three files of code:

product.h
#ifndef product_h
#define product_h
#include <string>
#include <iostream>
using namespace std;

class Product
{
private:
	int plu;
	string productName;
	int type;
	double price;
	double inventory;
public:
	Product();	
	Product(int, string, int, double, double);
	int getPLU();
	string getProductName();
	int getType();
	double getPrice();
	double getInventory();
	double updateInventory(double);
				
};
#endif


product.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "product.h"
using namespace std;

Product::Product()
{
	plu = 0;
	productName = "none yet";
	type = 0;
	price = 0;
	inventory = 0;
}
Product::Product(int pl, string pn, int t, double pr, double i)
{
	plu = pl;
	productName = pn;
	type = t;
	price = pr;
	inventory = i;
}
int Product::getPLU()
{	return plu;}

string Product::getProductName()
{	return productName;}

int Product::getType()
{	return type;}

double Product::getPrice()
{	return price;}

double Product::getInventory()
{	return inventory;}

double Product::updateInventory(double quantity)
	{
		if (quantity > inventory)
			cout << "This item is not in stock." << endl;
		else inventory -= quantity;
		return inventory;
	}


proj2.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"
using namespace std;

Product inv[100];	//declare vector of 100 objects
int count = 100;	//maximum products is 100

int menu(void)	//display user options 
{
	int option;
	cout << "Enter (1) if you are ready to check out." << endl;
	cout << "Otherwise, enter (0) when done" << endl;
	
	cin >> option;
	return option;
};
void updateFile()	//output results to a new file, rather than overwriting original file
{
	ofstream newInvFile("newinventory.txt");
		
	for (int p = 0; p < count; p++)
	{
		newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
	}

}
void checkout(void)
{
	double total = 0;
	double quantity;
	int input;
	int PLU = 0;

	do
	{
		PLU = 0;
		cout << "Enter PLU code or (0) to exit" << endl;
		cin >> input;
		if (input = 0)
			break;
		for (int p = 0; p < count; p++)		
		{
			if (inv[p].getPLU() == input)
			{
				PLU = p;
				break;
			}
		}
		if (PLU == -1)
		{
			cout << "Please enter a valid PLU" << endl;
			continue;
		}

		if (inv[PLU].getType() == 1)	// determine whether product is weighted or sold by units
			{cout << "Weight: ";}
		else 
			{cout << "Quantity: ";}

		cin >> quantity;
		total += quantity * inv[PLU].getPrice();
		inv[PLU].updateInventory(quantity);
	} 
	while (input != 0);

	cout << "Total: $" << total << endl;

	if (total > 50)	//apply discount if total is over $50
		{
			total = total * 0.95;
			cout << "Your purchase of over $50 qualifies you for a 5% discount. Total: $" << total << endl;
		}
	
}
int main()
{
	int plu;
	string productName;
	int type;
	double price;
	double inventory;	
	count = 0;

	ifstream originalInvFile ("inventory.txt");	//read from original inventory file
	
	if (originalInvFile.is_open())
	{
		while (!originalInvFile.eof())	//
		{
			originalInvFile >> plu >> productName >> type >> price >> inventory;

			Product temp = Product(plu, productName, type, price, inventory);	//overload and store in temporary variable
		    inv[count] = temp;
			count++;
		}
		originalInvFile.close();
	}
	int option;

	do
	{
		cout << "WELCOME TO THE CHECKOUT LINE" << endl;

		option = menu();
		if (option == 1)
			checkout();
		else 
			updateFile();
	} 
	while(option != 0);

	system ("pause");
}


I've been looking at my code for days and hours and i MUST be missing something. I am sooo tired and everything is just jumbled together :( thank you guys in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading .txt into parallel array and writing to new file

#2 msdiddyj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 12

Re: Reading .txt into parallel array and writing to new file

Posted 25 September 2012 - 07:15 PM

I thought I attached the file but I don't see it on the post. It's this:

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
4167 DELICIOUS_RED_SM 1 0.89 35.4
4124 EMPIRE 1 1.14 145.2
4129 FUJI_REG 1 1.05 154.5
4131 FUJI_X-LGE 1 1.25 164.1
4135 GALA_LGE 1 1.35 187.7
4133 GALA_REG 1 1.45 145.2
4139 GRANNY_SMITH_REG 1 1.39 198.2
4017 GRANNY_SMITH_LGE 1 1.49 176.5
3115 PEACHES 1 2.09 145.5
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67
94011 ORGANIC_BANANAS 1 0.99 56.3
Was This Post Helpful? 0
  • +
  • -

#3 msdiddyj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 12

Re: Reading .txt into parallel array and writing to new file

Posted 25 September 2012 - 08:20 PM

Oh, I just realized that it may have been confusing that I put parallel arrays. The assignment calls for classes, which I have done. No one has any ideas? :(
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • Joined: 19-February 09

Re: Reading .txt into parallel array and writing to new file

Posted 25 September 2012 - 08:38 PM

Hi, in the checkout function you have assigned instead of compared.

043	        if (input = 0)




When you have written and closed newinventory.txt, you can remove() inventory.txt and rename() the new file.
Was This Post Helpful? 1
  • +
  • -

#5 msdiddyj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-September 12

Re: Reading .txt into parallel array and writing to new file

Posted 25 September 2012 - 08:51 PM

OMFG I COULD KISS YOU RIGHT NOW, IT WORKS! oh, we did a new file because our professor didn't want us to overwrite the original file. we have to create a test plan and stuff and it's easier to compare the changed inventory that way.

but THANK YOU OMMMGGGGGGGGGGGGGGGGG. who knew it was that simple. man, i feel like a dummy.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • Joined: 19-February 09

Re: Reading .txt into parallel array and writing to new file

Posted 25 September 2012 - 09:17 PM

View Postmsdiddyj, on 26 September 2012 - 06:51 AM, said:

OMFG I COULD KISS YOU RIGHT NOW, IT WORKS!



:winkiss: :blush:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1