Please help with Grocery Shopping example !

Hello,

Please help with this online grocery shopping program! The program is only reading and displaying the first grocery item, instead of all 10 grocery items. Also after the user responds that they would like to continue purchasing items, the program crashes. It looks like there is an issue with both loops, but I can't figure out what it is.

Header .h File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 #include<iostream>
#include<string>
using namespace std;

#ifndef GROCERYITEM_H
#define GROCERYITEM_H

class GroceryItem

{
public:
	GroceryItem();//default constructor
	void set_item_name(string);
	void set_item_price(double);
	void set_qty_on_hand(int);
	void set_qty_purchased(int);
	string get_item_name();
	double get_item_price();
	int get_qty_on_hand();
	int get_qty_purchased();
private:
	string item_name;
	double item_price;
	int qty_on_hand;
	int qty_purchased;
};

#endif


Function definition cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Member-functions for the GroceryItem class

#include<iostream>
#include "Grocery.h"
using namespace std;

GroceryItem::GroceryItem()
{
	item_name = " ";
	item_price = 0;
	qty_on_hand = 0;
	qty_purchased = 0;
}

void GroceryItem::set_item_name(string name)
{
	item_name = name;
}

void GroceryItem::set_item_price(double price)
{
	if( price > 0)
		item_price = price;

	if( price <= 0)
	{
		item_price = 0;
		cout << "Price cannot be negative. Price has been set to zero" << endl;
	}
}

void GroceryItem::set_qty_on_hand(int onhand)
{
	if( onhand > 0)

		qty_on_hand = onhand;

	if( onhand <=0 )
	{
		onhand = 0;
		cout << "Quantity on hand cannot be negative. Quantity on hand has been set to zero" << endl;
	}
}


Driver cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/Create and manipulate GroceryItem class objects

#include<iostream>
#include "Grocery.h"
using namespace std;

#include<fstream>

const int NUM_ITEMS = 10;

int main()
{
	GroceryItem grocery_items[NUM_ITEMS];

	ifstream inFile;
	inFile.open("Grocery.txt");

	int k;
	string tempy;
	double temp;
	for(k = 0; k < NUM_ITEMS; k++)
	{
		getline(inFile, tempy);
        grocery_items[k].set_item_name(tempy);

		inFile >> temp;
		grocery_items[k].set_item_price(temp);
		inFile >> temp;
		grocery_items[k].set_qty_on_hand(temp);
		
		
	}


	cout<<"The grocery items along with their respective price, and qty on hand are: "<<endl;

	for(k = 0; k < NUM_ITEMS; k++)
	{
		cout<<grocery_items[k].get_item_name()<<" "<<grocery_items[k].get_item_price()
			<<" "<<grocery_items[k].get_qty_on_hand()<<endl;
	}


Grocery.txt file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Raisan Bran
3.49
300
Milk
1.49
200
WhiteBread
2.49
50
Butter
2.49
100
Grape Jelly
1.09
50
Peanut Butter
2.49
45
Tomato Soup
.49
200
Cherry Yogurt
.69
250
Vanilla Yogurt
.69
200
Rye Bread
1.49
55



after the user responds that they would like to continue purchasing items, the program crashes.


I don't see that you posted this part of the code?


In the set functions where you check for negative values - if 0 is a valid price or quantity on hand, I might change the if conditions.

1
2
3
4
5
6
7
8
	if( price > 0) // >=0
		item_price = price;

	if( price <= 0) // <0
	{
		item_price = 0;
		cout << "Price cannot be negative. Price has been set to zero" << endl;
	}
Topic archived. No new replies allowed.