3 Replies - 305 Views - Last Post: 14 October 2011 - 10:25 PM Rate Topic: -----

#1 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

User Option Issue

Posted 14 October 2011 - 08:23 PM

My program is suppose to simulate a grocery store. The problem I am having is that no matter what I do, after the user inputs the code for a product and the weight they want to buy of it for the first time, the program makes the person buy the previous product instead of allowing the user to input the code for the the product they want to buy.

I am not sure why I have this problem.

Here is my code:

//Daniel Maclin
//Homework 3

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




int main(){
	
	ifstream inFile;
	int plu =0;
	string name;

	int sType; // the sales type
	double price; //the price per pound/weight
	double inventory;//the amount of the product the store contains

	int x = 0;//a counter for the number of products loaded into the Product class
	Product product[100];
	double balance=0;//the money amount of a product the user buys
	double totBalance=0;//the total balance of the user

	int quit = 0;//allows for the program to terminate
	int found = 0; //records the location of plu code
	double userOption;//the weight of a product the user wants to buy

	const double discount= 0.05;//the discount for the user
	double finalBalance = 0.0;//the final balance after the discount
	double discountAmt = 0;//the discount value
	bool checkOut = true;//allows the user to check out

	char choice = ' ';
	inFile.open("inventory.txt");
	if(inFile.fail()){
		cout << "Cannot open file";
		exit(1);
	}

	while(inFile.good()){
		inFile >> plu >> name >> sType >> price >> inventory;
		product[x] = Product(plu, name, sType, price, inventory);//this line
		x++;
	}

	while(quit != -1){
		cout << "Enter a PLU code or 0 to check out: ";
		cin >> plu;

		while(checkOut){

			if(plu == -1){
				quit = -1;
				break;
			}

			//Allows checkout
			if(plu == 0){
				checkOut = false;
			}

			for(int i=0; i < x; i++){
				if(plu == product[i].getPluCode()){
					found = i;
					cout << "Product matched" << endl;
				
				}
			}

			cout << product[found].getName() << "\n";
			cout << "Enter the weight for " << product[found].getName() << ": "<< endl;
			cin >> userOption;
		
			inventory =  product[found].getInventory() - userOption;

			//calculates the user's dollar amount
			balance = product[found].getPrice() * userOption;
			totBalance += balance;
			cout<<"Total Balance so far: "<< totBalance << endl;
			//is verification for the discount

			
		}

			if(totBalance > 50){
				discountAmt = totBalance * discount;
				finalBalance = totBalance - discountAmt;
				}
	
		cout << "Total: $ " << setprecision(2) << fixed << totBalance << 
			"\nDiscount: $ " << setprecision(2) << fixed << discountAmt << "\nAmount Due $ " 
			<< setprecision(2) << fixed << finalBalance << endl;

		cout << "Press y to check out the next customer (n to quit)"<<endl;
		if(choice == 'y' || choice == 'y'){
			checkOut = false;
		}
		else{
			cout<<"Thanks for shopping, have a nice day!" << endl;
			quit = -1;
		}
	}
		//after done with for loop, check to see if x is equal to -1, if not, product not found
		//if found, it gets the index
		//must create the calculations for the amount of a product the user wants.
		//Also must create the remainder for the amount of inventory left
		//Also, must create variable to keep user's amount spent
	
	system("PAUSE");
	return 0;
}





This post has been edited by Ipodhero178: 14 October 2011 - 08:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: User Option Issue

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,061
  • Joined: 02-June 10

Re: User Option Issue

Posted 14 October 2011 - 09:02 PM

You've already identified the problem as coming at line 47 where you are calling Product(parameters).
But not shown us that code. The problem I would guess is coming from that class/DLL but we don't see that code to help with it.
Was This Post Helpful? 0
  • +
  • -

#3 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: User Option Issue

Posted 14 October 2011 - 09:12 PM

View PostIpodhero178, on 14 October 2011 - 08:23 PM, said:

...the program makes the person buy the previous product instead of allowing the user to input the code for the the product they want to buy.


while(quit != -1){
	cout << "Enter a PLU code or 0 to check out: ";
	cin >> plu;
	while(checkOut){
		if(plu == -1){
         		quit = -1;
		break;
		}

		//Allows checkout
		if(plu == 0){
			checkOut = false;
		}

		for(int i=0; i < x; i++){
			if(plu == product[i].getPluCode()){
				found = i;
				cout << "Product matched" << endl;
				
			}
		}

		cout << product[found].getName() << "\n";
		cout << "Enter the weight for " << product[found].getName() << ": "<< endl;
		cin >> userOption;
		
		inventory =  product[found].getInventory() - userOption;

		//calculates the user's dollar amount
		balance = product[found].getPrice() * userOption;
		totBalance += balance;
		cout<<"Total Balance so far: "<< totBalance << endl;
		//is verification for the discount

			
	}

			if(totBalance > 50){
			discountAmt = totBalance * discount;
			finalBalance = totBalance - discountAmt;
			}

	cout << "Total: $ " << setprecision(2) << fixed << totBalance << 
		"\nDiscount: $ " << setprecision(2) << fixed << discountAmt << "\nAmount Due $ " 
		<< setprecision(2) << fixed << finalBalance << endl;

	cout << "Press y to check out the next customer (n to quit)"<<endl;
	if(choice == 'y' || choice == 'y'){
		checkOut = false;
	}
	else{
		cout<<"Thanks for shopping, have a nice day!" << endl;
		quit = -1;
	}
}
		//after done with for loop, check to see if x is equal to -1, if not, product not found
		//if found, it gets the index
		//must create the calculations for the amount of a product the user wants.
		//Also must create the remainder for the amount of inventory left
		//Also, must create variable to keep user's amount spent
	
	system("PAUSE");
	return 0;
}






I have the strangest feeling that it's an error in your math when calculating "product[found]".
if(plu == product[i].getPluCode())
{
    found = i;
}



Are you just forgetting that product[100] starts at zero and not one? That might account for the output being just slightly off.
Was This Post Helpful? 0
  • +
  • -

#4 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: User Option Issue

Posted 14 October 2011 - 10:25 PM

View PosttlhIn`toq, on 14 October 2011 - 10:02 PM, said:

You've already identified the problem as coming at line 47 where you are calling Product(parameters).
But not shown us that code. The problem I would guess is coming from that class/DLL but we don't see that code to help with it.

Well actually, that was from something else unrelated to this problem.

@Gungnir: I will look at it and post an update on it.

EDIT: That didn't work. I also start plu at zero, so there should not be a problem there.

To help, this is what the output looks like(my input will be bolded)


Enter a PLU code or 0 to check out: 4011
Product Matched
Bananas
Enter the weight for Bananas: 52.5
Total balance so far: 26.46
Product Matched
Bananas
Enter the weight for Bananas:

It should allow the user to do input another PLU code for another product until the user wants to check out.

This post has been edited by Ipodhero178: 14 October 2011 - 10:36 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1