Need help with making my code more user friendly.

The code compiles well and with color, but I have a few more things I'd like to polish up on it.

1. Further input validation. (accept only numbers not letters, else loop)
2. Output to text file or print on local printer. (Printer would be secondary, or at least to a text file)
3. Use the same function in another module. (To keep the user from scrolling up)

Here is the code, with reamed out comments where I would like some direction or instruction.

#include<iostream>
#include<iomanip>
#include<sstream>
#include<string>
#include<fstream>
#include<Windows.h>

using namespace std;



class InvBin
{
private:
string description;
int qty;

public:
InvBin(string d = "empty", int q = 0)
{ description = d; qty = q;}

void setDescription(string d)
{description = d;}

string getDescription() const
{
string s = description;
return s;
}

void setQty(int q)
{qty =q;}

int getQty() const
{return qty;}
};

const int MAX_BINS = 30;

class BinManager
{
private:
InvBin bin[MAX_BINS];
int numBins;
string toString(int) const;

public:
BinManager()
{numBins = 0;}

BinManager(int, string[], int[]);

string getDescription(int binNum) const
{return bin[binNum-1].getDescription();}

int getQuantity(int binNum) const
{return bin[binNum-1].getQty();}

string displayAllBins() const;

bool addParts(int, int);
bool removeParts(int, int);
};

BinManager::BinManager(int size, string d[], int q[])
{
numBins = size;

for (int item = 0; item < numBins; item++)
{bin[item].setDescription(d[item]);
bin[item].setQty(q[item]);
}
}

string BinManager::displayAllBins()const
{
string binInfo = "";

for (int binNum =1; binNum <= numBins; binNum++)
{
binInfo +=" " + toString(binNum) + " " +
bin[binNum-1].getDescription()+" "+
toString(bin[binNum-1].getQty())+"\n";
}
return binInfo;
}

string BinManager::toString(int num) const
{
ostringstream ss;
ss<<num;
return ss.str();
}

bool BinManager::addParts(int binNum, int numToAdd)
{
int loc = binNum -1;

if (numToAdd > 0)
{
bin[loc].setQty(bin[loc].getQty()+numToAdd);
return true;
}
else
return false;
}

bool BinManager::removeParts(int binNum, int numToRemove)
{
int loc = binNum -1;

if (numToRemove > 0 && numToRemove <= bin[loc].getQty())
{
bin[loc].setQty(bin[loc].getQty()-numToRemove);
return true;
}
else
return false;
}

void displayMenu();
int getChoice(int);
void addParts(BinManager &);
void removeParts(BinManager &);
void displayBins(const BinManager &);

const int BINS_IN_USE = 9,
MAX_CHOICE = 4,
QUIT = 4;

int main()
{
int choice;

string itemNames[]={"regular pliers ", "N. nose pliers ",
"screwdriver ", "P. head screwdriver", "Wrench-large ",
"wrench-small ", "drill ", "cordless drill ",
"hand saw "};

int quantity[] ={25, 5, 25, 5, 7, 18, 51, 16, 12};

BinManager inventory(9, itemNames, quantity);

displayBins(inventory);

do
{
displayMenu();
choice = getChoice(MAX_CHOICE);

switch (choice)
{
case 1: addParts(inventory);
break;
case 2: removeParts(inventory);
break;
case 3: displayBins(inventory);
break;
}
} while(choice != QUIT);

system("cls");


HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,127+12);

cout<<" **********************\n"
<<" End Of Day Inventory \n"
<<" ********************** ";
SetConsoleTextAttribute(screen,15);
displayBins(inventory);//I want to output to text and or print out on a local printer

ofstream outputFile;
outputFile.open("inventory.txt");
cout<<"\n\nWriting to file.\n";
//Not sure what to put here to output to text file?
outputFile.close();
cout<<"Printing......\n\n";
//Not sure what to do to bring up the printing dialog?
system("pause");
return 0;
}

void displayMenu()
{
//I want to display updated displayBins(inventory) here again to keep from scrolling up the screen
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,3);
cout<<"\n\n\n Inventory Control Menu \n\n";
SetConsoleTextAttribute(screen,15);
cout <<"1 - Add parts to a bin\n";
cout <<"2 - Remove parts from a bin\n";
cout <<"3 - Display bin status report\n";
cout <<"4 - Quit\n\n";
}

int getChoice(int maxChoice)
{
int choice;
cin>>choice;

while (choice < 1 || choice > maxChoice)
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,4);
cout<<"Choice must be between 1 and "<<maxChoice
<<". Re-enter choice: ";
SetConsoleTextAttribute(screen,15);
cin>>choice;// keep to a number, else loop again
}
return choice;
}

void addParts(BinManager &inventory)
{
int binNum,
numToAdd;

cout<<"Enter bin Number (1- " << BINS_IN_USE << "): ";
binNum = getChoice(BINS_IN_USE);

cout<<"\nCurrent status of bin #"<<binNum<<": "
<<inventory.getDescription(binNum)<<" "
<<inventory.getQuantity(binNum)<<"\n\n";
cout<<"Enter number of parts to be added: ";
cin>>numToAdd;// keep to a number, else loop again

if(inventory.addParts(binNum, numToAdd))
cout<<"\nNew status of bin #" <<binNum<<": "
<<inventory.getDescription(binNum)<<" "
<<inventory.getQuantity(binNum)<<"\n\n";
else
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,4);
cout<<"Invalid quanity. Add was not performed.\n";
}
}

void removeParts(BinManager &inventory)
{
int binNum,
numToRemove;

cout<<"Enter bin Number(1-)" << BINS_IN_USE << "): ";
binNum = getChoice(BINS_IN_USE);

cout<<"\nCurrent status of bin #"<<binNum<<": "
<<inventory.getDescription(binNum)<<" "
<<inventory.getQuantity(binNum)<<"\n\n";
cout<<"Enter number of parts to be removed: ";
cin>>numToRemove;// keep to a number, else loop again

if(inventory.removeParts(binNum, numToRemove))
cout<<"\nNew status of bin #" <<binNum<<": "
<<inventory.getDescription(binNum)<<" "
<<inventory.getQuantity(binNum)<<"\n\n";
else
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,4);
cout<<"Invalid quanity. Add was not performed.\n";
}
}

void displayBins(const BinManager &inventory)
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,5);
cout<<"\n______________________________\n"
<<"Bin Part Qty \n"
<<"_______________________________\n";
SetConsoleTextAttribute(screen,15);
cout<< inventory.displayAllBins();
}
you have a good coding style, theres not much I would chainge except name your classes and variables slightly more descriptively and use brackets around a long if statement, one that spans multiple lines. Not much I can say, since its a console based class.
Thanks DeXecipher and duly noted,

However, I would like to keep the user from accidentally entering a letter on all cin's. It causes an infinite loop.

The other thing is, it's annoying to be asked a question but then the user has to scroll up to see what is was to be input. (I figured this one out)

Also, I'd like to be able to print inventory from a local printer, or to a text file.
(Figured this one out)

Do a search for "//" in my code to see where I made the comments.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<iostream>
#include<iomanip>
#include<sstream>	
#include<string>
#include<fstream>
#include<Windows.h>
#include<winspool.h>
#include<ctime>

using namespace std;

class InvBin
{
private:
	string description;
	int qty;

public:
	InvBin(string d = "empty", int q = 0)
	{ description = d; qty = q;}

	void setDescription(string d)
	{description = d;}

	string getDescription() const
	{
		string s = description;
		return s;
	}

	void setQty(int q)
	{qty =q;}

	int getQty() const
	{return qty;}
};

const int MAX_BINS = 30;

class BinManager
{
private:
	InvBin bin[MAX_BINS];
	int numBins;
	string toString(int) const;

public:
	BinManager()
	{numBins = 0;}

	BinManager(int, string[], int[]);

	string getDescription(int binNum) const
	{return bin[binNum-1].getDescription();}

	int getQuantity(int binNum) const
	{return bin[binNum-1].getQty();}

	string displayAllBins() const;

	bool addParts(int, int);
	bool removeParts(int, int);
};

BinManager::BinManager(int size, string d[], int q[])
{
	numBins = size;

	for (int item = 0; item < numBins; item++)
	{bin[item].setDescription(d[item]);
	bin[item].setQty(q[item]);
	}
}

string BinManager::displayAllBins()const
{
	string binInfo = "";

	for (int binNum =1; binNum <= numBins; binNum++)
	{
		binInfo +=" " + toString(binNum) + "   " +
			bin[binNum-1].getDescription()+"    "+
			toString(bin[binNum-1].getQty())+"\n";
	}

	return binInfo;
}

string BinManager::toString(int num) const
{
	ostringstream ss;
	ss<<num;
	return ss.str();
}

bool BinManager::addParts(int binNum, int numToAdd)
{
	int loc = binNum -1;

	if (numToAdd > 0)
	{
		bin[loc].setQty(bin[loc].getQty()+numToAdd);
		return true;
	}
	else
		return false;
}

bool BinManager::removeParts(int binNum, int numToRemove)
{
	int loc = binNum -1;

	if (numToRemove > 0 && numToRemove <= bin[loc].getQty())
	{
		bin[loc].setQty(bin[loc].getQty()-numToRemove);
		return true;
	}
	else
		return false;
}

void displayMenu();
void addParts(BinManager &);
void removeParts(BinManager &);
void displayBins(const BinManager &);
int getChoice(int);
const int	BINS_IN_USE = 9,
			MAX_CHOICE = 4,
			QUIT = 4;

int main()
{
	int choice;

string itemNames[]={"Regular Pliers     ", "N. Nose Pliers     ",
	"Screwdriver        ", "P. Head Screwdriver", "Wrench-Large       ",
	"Wrench-Small       ", "Drill              ", "Cordless Drill     ",
	"Hand Saw           "};

int quantity[] ={25, 5, 25, 5, 7, 18, 51, 16, 12};

BinManager inventory(9, itemNames, quantity);

displayBins(inventory);

do
{
	displayMenu();
	choice = getChoice(MAX_CHOICE);

	switch (choice)
	{
	case 1: addParts(inventory);
		break;
	case 2: removeParts(inventory);
		break;
	case 3: displayBins(inventory);
		break;
	}

} while(choice != QUIT);

system("cls");

HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(screen,127+12);

cout<<"    **********************    \n"
	<<"     End Of Day Inventory     \n"
	<<"    **********************    \n";

SetConsoleTextAttribute(screen,15);
	displayBins(inventory);

	cout<<"\n\nFile opened for printing...\n\n";
	system("notepad.exe InventoryUpdateSheet.txt" );
	system("pause");
	return 0;
}

void displayMenu()
{
	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(screen,3);
	cout<<"\n\n    Inventory Control Menu \n\n";
	SetConsoleTextAttribute(screen,15);
	cout <<" 1   Add parts to a bin\n";
	cout <<" 2   Remove parts from a bin\n";
	cout <<" 3   Display bin status report\n";
	cout <<" 4   Quit and print report\n\n";
	cout <<"Choose [1-4]: ";
}

int getChoice(int maxChoice)
{
	int choice;
	cin>>choice; // keep to a number, else loop again

	while (choice < 1 || choice > maxChoice)
	{
		cin.clear();
		HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(screen,4);
		cout<<"Choice must be between 1 and "<<maxChoice
			<<". Re-enter choice: ";
		SetConsoleTextAttribute(screen,15);
		cin>>choice; 
	}
return choice;
}

void addParts(BinManager &inventory)
{
	int binNum,
		numToAdd;

	cout<<"\nEnter bin number (1- " << BINS_IN_USE << "): ";
	binNum = getChoice(BINS_IN_USE);

	cout<<"\nCurrent status of bin #"<<binNum<<": "
		<<inventory.getDescription(binNum)<<"   "
		<<inventory.getQuantity(binNum)<<"\n\n";
	cout<<"Enter number of parts to be added: ";
	cin>>numToAdd; // keep to a number, else loop again

	if(inventory.addParts(binNum, numToAdd))
	{
		HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(screen,128);
		cout<<"\nNew status of bin #" <<binNum<<":     "
		<<inventory.getDescription(binNum)<<"   "
		<<inventory.getQuantity(binNum)<<"\n\n", 
		displayBins(inventory);
	}
	else
	{
		HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(screen,4);
		cout<<"Invalid quanity. Add was not performed.\n";
	}
}

void removeParts(BinManager &inventory)
{
	int binNum,
	numToRemove;

	cout<<"Enter bin number (1- " << BINS_IN_USE << "): ";
	binNum = getChoice(BINS_IN_USE);

	cout<<"\nCurrent status of bin #"<<binNum<<": "
		<<inventory.getDescription(binNum)<<"   "
		<<inventory.getQuantity(binNum)<<"\n\n";
	cout<<"Enter number of parts to be removed: ";
	cin>>numToRemove;

	if(inventory.removeParts(binNum, numToRemove))
		{
		HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(screen,128);
		cout<<"\nNew status of bin #" <<binNum<<":     "
		<<inventory.getDescription(binNum)<<"   "
		<<inventory.getQuantity(binNum)<<"\n\n", 
		displayBins(inventory);
	}
	else
	{
		HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(screen,4);
		cout<<"Invalid quanity. Add was not performed.\n";
	}
}

void displayBins(const BinManager &inventory)
{
	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(screen,5);
	cout<<"______________________________\n"
		<<"Bin        Part            Qty \n"
		<<"______________________________\n";
	SetConsoleTextAttribute(screen,15);
	cout<< inventory.displayAllBins();
	
	ofstream inventoryPrintout; 
	inventoryPrintout.open("InventoryUpdateSheet.txt");
	time_t now = time(0);
    char* dt = ctime(&now);

	inventoryPrintout
	<<"************************************************\n"<<endl
	<<"---------------Current Inventory----------------\n"<<endl
	<<"           "<< dt<<endl
	<<"************************************************"<<endl
	<<"________________________________________________"<<endl
	<< inventory.displayAllBins()
	<<"________________________________________________\n\n"<<endl
	<<"               [Ctrl+p] to print\n\n"
	<<"After printing close Notepad to return to Program" ;
	
	inventoryPrintout.close();
	
}
Last edited on
Topic archived. No new replies allowed.