Program not working when building

I created three files, Checkout.cpp, grocery.cpp and grocery.h
I try to build it and it creates an exe file but when I run it I dont get the intentory list to tell what I sell. Can someone see what Im doing wrong? thanks

Also, I want to convert this to be able to run on my nova account in UNIX. I know I can put in .cpp files but how can I build everything so it will be able to run without an exe file that C++ creates?

checkout.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "grocery.cpp"


using namespace std;

const int STOCKROOM = 30;		// Max. items in inventory
const char *INVENT = "Invent.in";	// inventory file
const char *RECEIPTS = "Receipts.out";	// file of receipts

int main ( ) 
{
	Stockroom stockroom(30);

	ifstream invent(INVENT);	// Read inventory

	ofstream receipts(RECEIPTS);
	stockroom.WriteStock(receipts);	// List inventory on receipt
	stockroom.WriteStock(cout);	// and onto screen

	int customer = 1;
	string more;
	do {
		double bought(0.00), tax(0.00);
		receipts << endl << "Customer " << customer << endl;
		cout << "Enter purchases for customer " << customer++ << endl
			<< "Enter product code 0 to end purchases." << endl << endl;

		cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
		cout.precision(2);
		receipts << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
		receipts.precision(2);

		int prod, quantity;
		do {
			//cout << "Enter product code and quantity: ";
			cin >> prod;
			if (prod > 0) {
				Grocery *product = stockroom.GroceryClerk(prod);
				cin >> quantity;

				if (product == NULL) {	// product not found?
		receipts << "*** item " << prod << " not in inventory ***" << endl;
		cout << "*** item " << prod << " not in inventory ***" << endl;

				} else if ((quantity < 1) || (10 < quantity)) {
		receipts << "*** " << quantity << " of item " << prod << 
			" are not for sale ***" << endl;
	cout << "*** " << quantity << " of item " << prod << 
			" are not for sale ***" << endl;

				} else {		// sale
					bought += product->Sale(quantity);
					tax += product->Tax(quantity);
					string desc = product->Description();
					int descblanks = 12 - desc.length() + 1;
					for (int d = 0; d < descblanks; d++)
						desc += ' ';
		receipts << desc << setw(2) << quantity << " @ " << setw(4) <<
			product->Price() << setw(6) << product->Sale(quantity) 
			<< (product->Tax(quantity) > 0.00 ? " TX" : " ") << endl;
		
		cout << desc << setw(2) << quantity << " @ " << setw(4) <<
			product->Price() << setw(6) << product->Sale(quantity) 
			<< (product->Tax(quantity) > 0.00 ? " TX" : " ") << endl;
		
				}
			}
		} while (prod != 0);

		if (bought > 0.00) {		// bought anything?
			receipts << endl << "              Subtotal " 
				<< setw(5) << bought << endl << 
				"                   Tax " 
				<< setw(5) << tax << endl << endl << 
				"                 Total " 
				<< setw(5) << (bought + tax) << endl << endl;
			
			cout << endl << "              Subtotal " 
				<< setw(5) << bought << endl << 
				"                   Tax " 
				<< setw(5) << tax << endl << endl << 
				"                 Total " 
				<< setw(5) << (bought + tax) << endl << endl;
			
		}

		cout << "Is there another customer? (Y/N) ";
		cin >> more;
	} while (more == "Y");
	if (more == "N")
		cout << "Thanks for shopping at Bryan's Market." << endl;

return 0;
}


grocery.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>
#include <string>
#include "grocery.h"


using namespace std;


// Input Grocery from in.
void Grocery::ReadGrocery ( istream& in )
{
	in >> product >> description >> price >> taxable;
	char eol; in >> eol; in.unget();	// Position after eol.
}

// Output Grocery to out.
void Grocery::WriteGrocery ( ostream& out )
{
	out << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
	out.precision(2);			// 2 decimals
	string desc = description;
	int descblanks = 12 - desc.length() + 1;
	for (int d = 0; d < descblanks; d++) desc += ' ';
	out << setw(5) << product << " " << desc << setw(4) << price 
		<< " " << taxable;
}


// A Stockroom for shelves items
Stockroom::Stockroom (int shelves)
{
	pantry = new Grocery[shelves];
	stockshelves = shelves;
	groceries = 0;
}

// BuyStock: Include Grocery in Stockroom
// Returns true if food added to stock; false otherwise
bool Stockroom::BuyStock (Grocery food)
{
	if ((groceries+1) >= stockshelves) return(false);
	for (int g = 0; g < groceries; g++)
		if (food.Product() < pantry[g].Product()) {
			for (int g1 = groceries++; g1 >= g; g1--)
				pantry[g1+1] = pantry[g1];
			pantry[g] = food;
			return(true);
		} else if (food.Product() == pantry[g].Product()) return(true);
	pantry[groceries++] = food;
	return(true);
}

// GroceryClerk: Search Stockroom for prod
// Returns: Grocery* except NULL when prod not in inventory
Grocery* Stockroom::GroceryClerk (int prod)
{
	int low = 0;
	int high = groceries-1;
	while (low <= high) {
		int mid = (low + high) / 2;
		if (pantry[mid].Product() == prod) return(&pantry[mid]);
		if (pantry[mid].Product() < prod) low = mid + 1;
		else high = mid - 1;
	}
	return(NULL);
}

// Output all items in Stockroom to out.
void Stockroom::WriteStock ( ostream& out )
{
	for (int g = 0; g < groceries; g++) {
		pantry[g].WriteGrocery(out);
		out << endl; }
	out << endl;
}


grocery.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define GROCERYH

using namespace std;

const double TAXRATE = 0.075;

// Item in grocery inventory
class Grocery
{
public:
	// Constructors
	Grocery ( ) : product(-1), description(""), price(0.0), taxable(' ') { }
	//Grocery ( ) { }
	Grocery ( int prod, string desc, double cost, char tax ) : 
	product(prod), description(desc), price(cost), taxable(tax) { }
	//Grocery ( int prod, string desc, double cost, char tax )
	//{ product = prod; description = desc; price = cost; taxable = tax; }

	// Input/output
	void ReadGrocery ( istream& in );
	void WriteGrocery ( ostream& out );

	// Access functions
	inline int Product ( ) { return(product); }
	inline string Description ( ) { return(description); }
	inline double Price ( ) { return(price); }
	inline char Taxable ( ) { return(taxable); }

	// Calculations
	inline double Sale ( int quantity ) { return(price * quantity); }
	inline double Tax ( int quantity ) {
		return(taxable != 'T' ? 0.00 : Sale(quantity) * TAXRATE); }

private:
	int product;			// product code
	string description;		// description
	double price;			// unit price
	char taxable;			// T = Taxable, N = Non-taxable
};

// Grocery stockroom (all inventory)
class Stockroom
{
public:
	// Constructor
	Stockroom (int shelves);

	// Output
	void WriteStock ( ostream& out );

	// Enter food into stock
	bool BuyStock (Grocery food);

	// Search for prod.
	Grocery* GroceryClerk (int prod);

private:
	Grocery *pantry;		// groceries in stock
	int groceries,			// # groceries in stock
            stockshelves;		// max. groceries in stock

};
//#endif  


Invent.in
1
2
3
4
5
6
7
8
32998	fruity-pebbles	2.99    T
33125	eggs		2.09	T
31002	pancakes	3.59	T
41505	syrup		1.49	T
52458	dryer-sheets	0.99	N
53220	twinkies	1.29	T
63889	bagels		1.49	T
Last edited on
1
2
3
4
5
6
7
8
9
10
int main ( ) 
{
	Stockroom stockroom(30);

                //The following line will only open the file.
               //It doesn't actually read and/or display anything from the file
             ifstream invent(INVENT);	// Read inventory - No it doesn't

	ofstream receipts(RECEIPTS);
<SNIP>
Also why in checkout.cpp do you include grocery.cpp?
Yes you can include *.cpp files in other cpp files, but it usually for a special reason, not standard practice.
If I dont include grocery.cpp in my checkout file than it gives all these errors so when I add it they all go away. I thought the Inventroy file was nt reading...do you know of a way to make it read from my .in file which contins my inventory file so it will show on the program to chose from?
If I dont include grocery.cpp in my checkout file than it gives all these errors so when I add it they all go away. That is because your prject is not setup properly - but we will come back to that.
This is what I have done.
checkout.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include "grocery.cpp"

using namespace std;

const char *RECEIPTS = "Receipts.out";	// file of receipts
const char *INVENT = "Invent.in";	// inventory file
const int STOCKROOM = 30;		// Max. items in inventory

int main ( ) 
{

    Stockroom stockroom(30);  //same as before
    stockroom.LoadGroceryList(INVENT); //we let stockroom load the groceries

	ofstream receipts(RECEIPTS);
	stockroom.WriteStock(receipts);	// List inventory on receipt
	stockroom.WriteStock(cout);	// and onto screen

	int customer = 1;
	string more;
	do {
		double bought(0.00), tax(0.00);
		receipts << endl << "Customer " << customer << endl;
		cout << "Enter purchases for customer " << customer++ << endl
			<< "Enter product code 0 to end purchases." << endl << endl;

		cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
		cout.precision(2);
		receipts << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
		receipts.precision(2);

		int prod, quantity;
		do {
			//cout << "Enter product code and quantity: ";
			cin >> prod;
			if (prod > 0) {
				Grocery *product = stockroom.GroceryClerk(prod);
				cin >> quantity;

				if (product == NULL) {	// product not found?
		receipts << "*** item " << prod << " not in inventory ***" << endl;
		cout << "*** item " << prod << " not in inventory ***" << endl;

				} else if ((quantity < 1) || (10 < quantity)) {
		receipts << "*** " << quantity << " of item " << prod << 
			" are not for sale ***" << endl;
	cout << "*** " << quantity << " of item " << prod << 
			" are not for sale ***" << endl;

				} else {		// sale
					bought += product->Sale(quantity);
					tax += product->Tax(quantity);
					string desc = product->Description();
					int descblanks = 12 - desc.length() + 1;
					for (int d = 0; d < descblanks; d++)
						desc += ' ';
		receipts << desc << setw(2) << quantity << " @ " << setw(4) <<
			product->Price() << setw(6) << product->Sale(quantity) 
			<< (product->Tax(quantity) > 0.00 ? " TX" : " ") << endl;
		
		cout << desc << setw(2) << quantity << " @ " << setw(4) <<
			product->Price() << setw(6) << product->Sale(quantity) 
			<< (product->Tax(quantity) > 0.00 ? " TX" : " ") << endl;
		
				}
			}
		} while (prod != 0);

		if (bought > 0.00) {		// bought anything?
			receipts << endl << "              Subtotal " 
				<< setw(5) << bought << endl << 
				"                   Tax " 
				<< setw(5) << tax << endl << endl << 
				"                 Total " 
				<< setw(5) << (bought + tax) << endl << endl;
			
			cout << endl << "              Subtotal " 
				<< setw(5) << bought << endl << 
				"                   Tax " 
				<< setw(5) << tax << endl << endl << 
				"                 Total " 
				<< setw(5) << (bought + tax) << endl << endl;
			
		}

		cout << "Is there another customer? (Y/N) ";
		cin >> more;
	} while (more == "Y");
	if (more == "N")
		cout << "Thanks for shopping at Bryan's Market." << endl;

return 0;
}


Grocery.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "grocery.h"

using namespace std;

// Input Grocery from in.
void Grocery::ReadGrocery ( istream& in )
{
	in >> product >> description >> price >> taxable;
	//char eol; in >> eol; in.unget();	// Position after eol.
}

// Output Grocery to out.
void Grocery::WriteGrocery ( ostream& out )
{
	out << setiosflags(ios::fixed) << setiosflags(ios::showpoint);
	out.precision(2);			// 2 decimals
	string desc = description;
	int descblanks = 12 - desc.length() + 1;
	for (int d = 0; d < descblanks; d++) desc += ' ';
	out << setw(5) << product << " " << desc << setw(4) << price 
		<< " " << taxable;
}


// A Stockroom for shelves items
Stockroom::Stockroom (int shelves)
{
	pantry = new Grocery[shelves];
	stockshelves = shelves;
	groceries = 0;
}

// BuyStock: Include Grocery in Stockroom
// Returns true if food added to stock; false otherwise
bool Stockroom::BuyStock (Grocery food)
{
	if ((groceries+1) >= stockshelves) return(false);
	for (int g = 0; g < groceries; g++)
		if (food.Product() < pantry[g].Product()) {
			for (int g1 = groceries++; g1 >= g; g1--)
				pantry[g1+1] = pantry[g1];
			pantry[g] = food;
			return(true);
		} else if (food.Product() == pantry[g].Product()) return(true);
	pantry[groceries++] = food;
	return(true);
}

// GroceryClerk: Search Stockroom for prod
// Returns: Grocery* except NULL when prod not in inventory
Grocery* Stockroom::GroceryClerk (int prod)
{
	int low = 0;
	int high = groceries-1;
	while (low <= high) {
		int mid = (low + high) / 2;
		if (pantry[mid].Product() == prod) return(&pantry[mid]);
		if (pantry[mid].Product() < prod) low = mid + 1;
		else high = mid - 1;
	}
	return(NULL);
}

// Output all items in Stockroom to out.
void Stockroom::WriteStock ( ostream& out )
{
	for (int g = 0; g < groceries; g++) {
		pantry[g].WriteGrocery(out);
		out << endl; }
	out << endl;
}


void Stockroom::LoadGroceryList(const char* FileName)
{

	ifstream invent(FileName);	// Read inventory
    if(invent.is_open())
    {
        groceries =0; //
        Grocery TempGrocery;
        //readin  stock until max stock value is reached or rech EOF
        for(int count=0; count < stockshelves ; count ++)
        {
            
            TempGrocery.ReadGrocery(invent);
            if ( !invent.eof() || !invent.fail() )
            {
                //good read - save grocery data
                pantry[count] = TempGrocery;
                groceries ++;
            }
            else
            {
                //end of file or read failed
                break;
            }             

        }

        invent.close();
    }


}


grocery.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef GROCERY_H
#define GROCERY_H

using namespace std;

const double TAXRATE = 0.075;


// Item in grocery inventory
class Grocery
{
public:
	// Constructors
	Grocery ( ) : product(-1), description(""), price(0.0), taxable(' ') { }
	//Grocery ( ) { }
	Grocery ( int prod, string desc, double cost, char tax ) : 
	product(prod), description(desc), price(cost), taxable(tax) { }
	//Grocery ( int prod, string desc, double cost, char tax )
	//{ product = prod; description = desc; price = cost; taxable = tax; }

	// Input/output
	void ReadGrocery ( istream& in );
	void WriteGrocery ( ostream& out );

	// Access functions
	inline int Product ( ) { return(product); }
	inline string Description ( ) { return(description); }
	inline double Price ( ) { return(price); }
	inline char Taxable ( ) { return(taxable); }

	// Calculations
	inline double Sale ( int quantity ) { return(price * quantity); }
	inline double Tax ( int quantity ) {
		return(taxable != 'T' ? 0.00 : Sale(quantity) * TAXRATE); }

private:
	int product;			// product code
	string description;		// description
	double price;			// unit price
	char taxable;			// T = Taxable, N = Non-taxable
};

// Grocery stockroom (all inventory)
class Stockroom
{
public:
	// Constructor
	Stockroom (int shelves);

	// Output
	void WriteStock ( ostream& out );

	// Enter food into stock
	bool BuyStock (Grocery food);

	// Search for prod.
	Grocery* GroceryClerk (int prod);

    //Load GroceryList from file
    void LoadGroceryList(const char* Filename); //NEW FUNCTION FOR STOCKROOM CLASS

private:
	Grocery *pantry;		// groceries in stock
	int groceries,			// # groceries in stock
    stockshelves;		// max. groceries in stock

};
#endif   

I have added a function to the stockroom class called void LoadGroceryList(const char* Filename);.
So now in checkout.cpp Lines 17 and 18 - we make the stockroom variable.
We then call this Stockroom member function with the name of the inventory file - and it loads the grocery data.

See grocery.h line 60 and grocery.cpp line 78 onwards
Thanks for helping me with my code...I knew I wasnt calling to list it but i couldnt figure out why....
When i compile it and build I run the exe file and it still says

Enter purchases for customer 1
Enter profcut code 0 to end purchases

It still wont load my Inventory file up...is my Invent.in the right file to use? or should I be using a cpp file? Maybe this is what Im doing wrong....should I add the Inventory file in with another one some how? thanks
To be honest, I only concentrated on loading the invent.in file.
So using the three files, I have posted should sort that out.
I haven't attempted to find and cure all the other errors (I was leaving that to you).
Line 22 stockroom.WriteStock(cout); // and onto screen - works - so you should see the inventory that was loaded displayed on the screen.

The rest of the code after that, (where you enter customer purchases - I haven't looked at).
Last edited on
Your program is working - to a degree.
here is what appeared on my screen when I tried to buy some bagels for
customer 1:

32998 fruity-pebbles2.99 T
33125 eggs 2.09 T
31002 pancakes 3.59 T
41505 syrup 1.49 T
52458 dryer-sheets 0.99 N
53220 twinkies 1.29 T
63889 bagels 1.49 T

Enter purchases for customer 1
Enter product code 0 to end purchases

63889
6
bagels 6 @ 1.49 8.94 TX
0

Subtotal 8.94
Tax 0.67

Total 9.61

Is there another customer? (Y/N)


here is what appeared in the receipts.out file when I finished

32998 fruity-pebbles2.99 T
33125 eggs 2.09 T
31002 pancakes 3.59 T
41505 syrup 1.49 T
52458 dryer-sheets 0.99 N
53220 twinkies 1.29 T
63889 bagels 1.49 T


Customer 1
bagels 6 @ 1.49 8.94 TX

Subtotal 8.94
Tax 0.67

Total 9.61



That is exactly what I should be seeing. However, when I build my three files I go to run the exe file under Debug and all I get on my screen is

Enter pruchases for customer 1
Enter Product code 0 to end purchases

how are you getting the inventroy to come up and I cant?
have you amended your files to include reading the invent.in file as I have shown (or copied and pasted the code I've given) and then re-compiled?

What setup are you using for your programming?
Last edited on
I went as far to copy your entire sections that you rewrote and I saved them all and compiled tham and rebuilt them... Im using Visuall C++ 6.0. Should I be using something different to recompile this?
I was just wondering - it was just that it seemed not to be loading up the invent.in file. Because it should load it and display it as per checkout.cpp lines 20 and 22.
That file should be in the same directory as the project - because I believe the VC++ sets the working directory to be the project directory when it runs the program.

Can you use mycomputer/Explorer to find where the invent.in file is located? Is it in the project directory?

This is the problem with forum based diagnostics - if I was there we would have sorted the problem by now.
Perfect!!!! I put the Invent.in file in my debug folder with the exe file and it loaded it all up like it should...everything worked great except when I said there was a new customer after I bought some things it just shut down.but oh well..it is finally doing what it should....thaks alot for helping me...
Glad it's starting to work for you now :-).
I'm aware of the problem you mentioned, it's to do with this check:

1
2
3
4
5
6
		cout << "Is there another customer? (Y/N) ";
		cin >> more;
	} while (more == "Y"); //PROBLEM HERE
	if (more == "N") //By the way this line is basically redundatnt.
		cout << "Thanks for shopping at Bryan's Market." << endl;


The problem being that more will only be Y if the user uses the CAPS or SHIFT key to enter the letter y - otherewise it will be y.
I suggest you change more to be a char rather than a string and you need to check for either Y or y
So you will have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      //other stuff as before

	int customer = 1; //First Cusromer

	char more; //more is now type char


        //other stuff  as before


		cout << "Is there another customer? (Y/N) ";
		cin >> more;
	} while (more == 'Y' || more == 'y'); //check for Y or y. Notice also the SINGLE quotes
	//The line if (more == "N") has now been removed
        cout << "Thanks for shopping at Bryan's Market." << endl;

      //other stuff as before 
Topic archived. No new replies allowed.