Programming Project Help

This is a basic end of course assignment and me and my team are at a loss we have pulled together this program and debugged, but we are still not getting exactly what we need, the program requirements as follow:

Write a program to keep track of a hardware store inventory. The store sells various items. For each item in the store the following info is kept: item ID, item name, number of pieces ordered, number of pieces in the store, numbere of pieces sold, manufacturer's price, and store price. At the end of each week the manager wants to see the following style report:

Friendly Hardware Store
itemID ItemName pOrdered pInStore pSold manufPrice sellingPrice
4444 Circular Saw 150 150 40 45.00 125.00
3333 Cooking Range 50 50 20 450.00 850.00
.
.
.

Total Inventory: $####.##
Total Number of items in store: _______

The total inventory is the total selling value of all of the items currently in the store. The total number of items is the sum of the number of pieces of all the items in the store.

Program must be menu driven, giving user various choices, such as checking if an item is in store, selling an item, and printing a current report. After inputting the data, sort it according to the item's name. After an item is sold, update the counts.

Initially, the number of pieces (of an item) in the store is the same as the number of pieces ordered, and the number of pieces of an item sold is zero. Input to the program is a file consisting of data in the following form:

itemID
itemName
pOrdered, manufPrice, sellingPrice

Use seven parallel vectors to store the information. The program must contain at least the following functions: one to input data into the vectors, one to display the menu, one to sell an item, and one to print the report for the manager.

The following is the text within our inventory file:

itemID
4444
3333
itemName
Circular Saw
Cooking Range
pOrdered, manufPrice, sellingPrice
150, 45.00, 125.00
50, 450.00, 850.00

The following is our current code (Part 1):

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
#include<fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

using namespace std;

const int invSize = 2;

//Function Prototypes//

void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice);

void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice);

void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice);

void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice);

void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice);

int main()
{

	//Function Main//


	//Variables//

	vector<int> itemQuantity;
	vector<string> itemID;
	vector<string> itemName;
	vector<int> pOrdered;
	vector<int> pInStore;
	vector<int> pSold;
	vector<double> manufPrice;
	vector<double> sellingPrice;

	ifstream infile;
	//Inventory Text file created by, //
	infile.open("c:\\inventory.txt");

	//Display error if file is not open//

	if (!infile)
	{
		cout << "Error in opening inventory file " << endl;
		return 1;
	}

	getInventory(infile, itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);

	displayMenu(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);

	infile.close();

	return 0;
}
//getInventory function//

void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice)

{
	unsigned int i;
	string line;

	for (i = 0; i < itemID.size(); i++)
	{
		infile >> itemID[i]
			>> itemName[i]
			>> pOrdered[i]
			>> manufPrice[i]
			>> sellingPrice[i];
		pInStore[i] = pOrdered[i];
		pSold[0] = 0;
	}

	

}

void displayMenu(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice)
{
	char menuAnswer;
	cout << "Type 'C' to check if an item is in stock." << endl;
	cout << "Type 'S' to sell an item to a customer." << endl;
	cout << "Type 'R' to print an inventory report." << endl;
	cin >> menuAnswer;

	if (menuAnswer == 'C' || menuAnswer == 'c')
		itemSearch(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
	if (menuAnswer == 'S' || menuAnswer == 's')
		sellItem(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
	if (menuAnswer == 'R' || menuAnswer == 'r')
		printReport(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
}


I am using visual studio 2015 and when I run the program it brings me to the menu with the 3 choices Check, Sell, or Print, but when I choose check or sell and input the item number/name the program causes a break, if I choose Print it prints all 0s as if it didn't pull any information from the .txt file. I have tried numerous formats within the text file and I've looked over the code all day and all night for the past two days and I just can't seem to figure out why it's breaking. Any and all help would be greatly appreciated.
Last edited on
Second part of our code:

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
110
111
void sellItem(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice)
{
	int itemNumber;
	int index;
	int number;
	int amtBought;
	double cash;
	char verifyItem;
	int i;

	for (i = 0; i < 5; i++)
	{
		cout << "What itemID is being purchased? ";
		cin >> itemNumber;

		i = itemNumber;

		if (i != -1)
		{
			cout << "You are selling " << itemName[i] << ", which costs $" << sellingPrice[i]
				<< ".  Is this correct? (Y/N)" << endl;
			cout << endl;
			cin >> verifyItem;

			if (verifyItem == 'N' || verifyItem == 'n')
			{
				cout << "What item number is being purchased? " << endl;
				cin >> itemNumber;
			}

			cout << "How many items are being purchased? " << endl;
			cin >> amtBought;

			if (amtBought < 1)

				cout << "None of this item is being purchased!" << endl;

			else
			{
				if (pInStore[i] < amtBought)
					cout << "There are not enough of this item to sell!  Up to " << pInStore[i] << " can be sold." << endl;

				else
				{
					cout << "The total price of this transaction is " << amtBought * sellingPrice[i] << endl;
					cout << "How much money did the customer give? " << endl;
					cin >> cash;

					if (cash < (amtBought * sellingPrice[i]))
					{
						cout << "The customer did not pay enough." << endl;
						continue;
					}
					else
					{
						pInStore[i] = pInStore[i] - amtBought;
						cout << "Please give the customer $" << cash - (amtBought * sellingPrice[i]) << " in change." << endl;
						cout << "Have a nice day!" << endl;
					}
				}
			}
		}
	}
}
//itemSearch function//
void itemSearch(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice)
{
	int number;

	cout << "Which item would you like to check?" << endl;
	cout << endl;
	cin >> number;

	for (number = 0; number < 5; number++)
		if (pInStore[number] > 0)
			cout << "There are " << pInStore[number] << "of that item available." << endl;
}
//printReport function//
void printReport(vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice)
{
	unsigned int i;
	int totalItems = 0;
	double totalInventory = 0;

	cout << "                         Friendly Hardware Store" << endl << endl;
	cout << "itemID ItemName     pOrdered pInStore pSold manufPrice sellingPrice" << endl;
	cout << fixed << showpoint;
	cout << setprecision(2);

	for (i = 0; i < itemID.size(); i++)
	{
		cout << left;
		cout << setw(7) << itemID.at(i);
		cout << setw(15) << itemName.at(i);
		cout << right;
		cout << setw(8) << pOrdered.at(i);
		cout << setw(9) << pInStore.at(i);
		cout << setw(6) << pSold.at(i);
		cout << setw(11) << manufPrice.at(i);
		cout << setw(13) << sellingPrice.at(i) << endl;

		totalInventory += pInStore.at(i) * sellingPrice.at(i);
		totalItems += pInStore.at(i);
	}

	cout << endl;
	cout << "Total Inventory: $" << totalInventory << endl;
	cout << "Total number of items in the store: " << totalItems << endl;
	system("pause");
	return;
}
Use seven parallel vectors to store the information.

This is making the program much harder, you really should be using a structure. Using more than two parallel vectors is really a very very bad practice, especially when you're going to try to sort all of these vectors and need to keep all of the entries in sink.

One of your primary problems is being caused by this function signature:
void getInventory(ifstream& infile, vector<string> itemID, vector<string> itemName, vector<int> pOrdered, vector<int> pInStore, vector<int> pSold, vector<double> manufPrice, vector<double> sellingPrice);

You don't seem to realize that passing values by value means that any changes made in the function will be lost when the function returns. You need to pass the variables by reference not by value if you want the variables to have values in the calling function.

Next your "display menu" function really should only be displaying the menu, not calling any functions. You should just return a value from the that function and let the calling function call the proper menu item.

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
...

int main()
{
 ...
   char answer = displayMenu();
   answer = toupper(answer);
   while(answer != 'C' || answer != 'S' || answer != 'R')
   {
      cout << "Please try again!\n";
      answer = displayMenu();
   }

   switch(answer)
   {
      case 'C':
         itemSearch(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
         break;
      case 'S':
...
      

char displayMenu()
{
   	char menuAnswer;
	cout << "Type 'C' to check if an item is in stock." << endl;
	cout << "Type 'S' to sell an item to a customer." << endl;
	cout << "Type 'R' to print an inventory report." << endl;
      
	cin >> menuAnswer;

        return menuAnswer;
}


By the way you should be passing your vectors into your functions by reference/const reference to avoid unnecessary copying of the vectors. For example your display function should look like:

1
2
3
4
5
6
void displayMenu(const vector<string>& itemID, const vector<string>& itemName,
                 const vector<int>& pOrdered, const vector<int>& pInStore, 
                 const vector<int>& pSold, const vector<double>& manufPrice,
                 const vector<double>& sellingPrice)
{
...


And note how I formatted the function to eliminate the very very long line.


Last edited on
Would the const function become problematic if the user is trying to sell an item? That means the variable would change no? I tweaked the code to the const, there were some errors and I worked around them because in some situations it couldn't call a const function such as

1
2
3
4
5
6
7
infile >> itemID[i]
			>> itemName[i]
			>> pOrdered[i]
			>> manufPrice[i]
			>> sellingPrice[i];
		pInStore[i] = pOrdered[i];
		pSold[0] = 0;

came in with multiple errors when the vectors were set as constants. I worked around them and the program runs as it did, same breaks and nothing past the menu. I feel as if it may be a problem with the text file, based on the program I'm not sure if my txt file information is formatted for the program to read it, it makes it through the infile.open, but no information is populated when a report is printed. In theory everything in the txt documented I provided above should show up on the printReport function.
Further I would concur the 7 vector parallels is an issue, this seems like it could be much simpler.
Topic archived. No new replies allowed.