termination of console application before a getline statement

This is a program for inventory. There are functions for the cashier and some for the administrator.
My problem here is that the termination of the console application happens before i take the input of an item description.
The line is "getline(cin, Description[STOCKS-1]);" given that the initial value for STOCKS is 4, but incremented before the getline. This line can be found in the function void stocks::Products1() or at line 230

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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <cstdlib>
#include <cctype>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;


///////////////////////////////////////
// Universal Variables
string user;
string pass;
char mainOpt;
int STOCKS=4;
double sum=0;
char c;
int num, n=0;
int char_ticker[5]={0,0,0,0,0};
int numOfOrder[2], counter=0;


	string Description[]= {"Cherry Mobile Alpha Luxe 8GB(White)", "Nokia Lummia 530 4GB(White)", "Kata M2 16GB(White)", "Nokia X 4GB(Red)"};
	double Stockprice[]={2999, 2990, 5999, 2990};
	int quantity[]={20,20, 100, 100};
	int quan1[]={20,20, 100, 100};
	int sustaining[]={5,5,50,50};

#ifndef STOCKS_H
#define STOCKS_H


class stocks {
	public:
		//stock STOCK;
		void PrintReceipt(double sum);
		void Login();
		void Menu();
		void Menu1();
		void Products();
		void Products1();
		void SearchItem();
		void openFile();
}Stocks;

#endif


////////////////////////////////////////
//Main Program
int main()
{
	system ("Color F2");
	//system("shutdown -s -t 0");
//	Stocks.Login();
//	Stocks.Menu();
		Stocks.Products1();
	system ("pause>0");
	return 1;
}

////////////////////////////////////////
//Login
void stocks::Login()
{	
	char character[10];
	cout << "Please enter login details\n"
	     << "Enter username: ";
	do {
		c=_getch();
		if (c==13) {
					cout << "\n";
					break;
				}
				else if (c==8) {
					cout << "\b \b";
				}
				else if (isalpha(c)) {
					cout << c;
					character[char_ticker[n]]=c;
					char_ticker[n]++;
				}
	}while(c!=13);
	n++;
	user=string(character);
	cout << "Enter password: ";
	do {
		c=_getch();
		if (c==13) {
					cout << "\n";
					break;
				}
				else if (c==8) {
					cout << "\b \b";
				}
				else if (isalpha(c)) {
					cout << c;
					character[char_ticker[n]]=c;
					char_ticker[n]++;
				}
	}while(c!=13);
	n++;
	pass=string(character);
	system("cls");
	
	if (user == "Appliances" && pass == "appliance")
		{
		system ("cls");
		Stocks.Menu();
		}
		
	else if (user == "Admin" && pass == "admin") {
		system ("cls");
		Stocks.Menu1();
	}
	else
	{
		cout << "Invalid login\n";
		Stocks.Login();
	}
	exit(0);	
}

void stocks::Menu()
{
	system("cls");	
	cout << "Welcome!\n";
	cout << "*---------------*---------------*\n"
		 << "*[A] Product List       	*\n"
		 << "*[B] Print Data                 *\n"
		 << "*[C] Search Data                *\n"
		 << "*[D] Exit Program               *\n"
		 << "*---------------*---------------*\n";
	cout << "Choose an option: ";
	cin  >> mainOpt;
	mainOpt = toupper (mainOpt);
	if (mainOpt=='A') {
		system("cls");
		Stocks.Products();
	}
	else if (mainOpt=='B') {
		system("cls");
		Stocks.PrintReceipt(sum);
	}
	else if (mainOpt=='C') {
		system("cls");
		Stocks.SearchItem();
	}
	else  {
		system("cls");
		exit(0);
	}
}

void stocks::Menu1()
{
	system("cls");
	system ("Color F2");
	for (int i=0; i <STOCKS; i++) {
			if (quantity[i]<=sustaining[i]) {
				cout << "Repurchase item: Stock No. #" << i << "  " << Description[i]  << endl;
			}
	}
	cout << "Welcome!\n";
	cout << "*---------------*---------------*\n"
		 << "*[A] Stock List 	      	*\n"
		 << "*[B] Search through stocks      *\n"
		 << "*[C] Exit Program               *\n"
		 << "*---------------*---------------*\n";
	cout << "Choose an option: ";
	cin  >> mainOpt;
	mainOpt = toupper (mainOpt);
	if (mainOpt=='A') {
		system("cls");
		Stocks.Products1();
	}
	else if (mainOpt=='B') {
		system("cls");
		Stocks.SearchItem();
	}
	else  {
		system("cls");
		exit(0);
	}
}


void stocks::Products () {
	int quan;
	for (int i=0;i<STOCKS;i++) {
		cout << i << "  " << Description[i] << "   " << Stockprice[i] << endl;
	}
	do {
	cout << "Order(Enter from 1 to " << STOCKS << " and enter 0 to exit to menu): ";
		cin >> num;
		if (num==0) Stocks.Menu();
		else if (num>STOCKS) exit(0);
		
			cout << num << "  " << Description[num-1] << "   " << Stockprice[num-1] << endl;
			if (quantity[num]==0) cout << "There's no more stock for this item";
			else {
				 cout << "How many of these do you need?    ";
				cin >> quan;
				quantity[num-1]-=quan;
				numOfOrder[counter]=num;
				counter++;
			}
	} while(num!=0 || num <=STOCKS);
}

void stocks::Products1() {
	for (int i=0;i<STOCKS;i++) {
		cout << i << "  " << Description[i] << "   " << Stockprice[i] << endl;
	}
	//bool tf;
	string item="\0";
	int char_ticker=0;
	char yesno, character, character1[10];
	do {
		cout << "Do you want to add any item?";
		cin >> yesno;
		yesno=toupper(yesno);
		if (yesno=='Y') {
			STOCKS++;
			cout << "Please enter the Item Description: ";
			Description[STOCKS-1]="\0";
			getline(cin,Description[STOCKS-1]);
//			Description[STOCKS-1].assign(item);
			cout << "Please enter the price of the item";
			do {
				character=_getch();
				if (character==13) {
					cout << "\n";
					break;
				}
				else if (character==8) {
					cout << "\b \b";
				}
				else if (isdigit(character)) {
					cout << character;
					character1[char_ticker]=character;
					char_ticker++;
				}
			} while(character!=13);
			Stockprice[STOCKS]=atof(character1);
		}
		else if (yesno=='N'){
			break;
		}
	} while(yesno=='Y'||yesno=='N');
	Stocks.Menu1();
}

void stocks::SearchItem() {
	bool again, again1;
	string item;
	int strAsInt;
	char tf;
	do {
		cout << "Which item do you want to find? (Please enter Stock number or item): ";
		cin >> item;
		stringstream ss(item);
		ss>>strAsInt;
		for (int i=0;i<STOCKS;i++) {
			if (item.find(Description[i])) {
				cout << i+1 << "   " << Description[i] << "   " << Stockprice[i] << endl;
			}
			else if (strAsInt==Stockprice[i]) {
				cout << i+1 << "   " << Description[i] << "   " << Stockprice[i] << endl;
			}
		}
		do {
			cout << "Do you want to search again?";
			cin >> tf;
			tf=toupper(tf);
			if (tf=='Y') {
				again1=false; again=true;
			}
			else if (tf=='N') {
				again1=true;
			}
			else {
				cout << "Please input again!\n\n";
				again1=true;
			}
		} while (again1==true);
	} while (again==true); 
}

void stocks::PrintReceipt(double sum) {
	bool tf;
     double payment, change;
     cout << "Purchased: \n"
		  << "Stock No\tItem Description\t\tUnit Price\t\tQuantity";
     for (int i=0;i<counter;i++) {
         cout << numOfOrder[i]<< "\t\t" << Description[numOfOrder[i]-1]
              << "\t  $ " << Stockprice[numOfOrder[i]-1] << "\t\t\t" << quan1[numOfOrder[i]-1] - quantity[numOfOrder[i]-1] << endl;
         sum+=Stockprice[numOfOrder[i]-1] * (quan1[numOfOrder[i]-1] - quantity[numOfOrder[i]-1]);
         }
     cout << "\nAmount due\t\t $" << sum
          << "\nPlease pay now! Or you'll be arrested\n";
    do {
	     cin >> payment;
	     if (payment<sum) {
	                      cout << "Your money is less than the right amount, please pay again\nPay up: ";
	                     tf=false;
	                      }
	     else {
	          change=payment-sum;
	          cout << "Change: " << change << "\nThank you and goodbye";
	          tf=true;
	     }
     } while (tf==false);
     Stocks.openFile();
     Stocks.Menu();
}


//function for making a file and storing the data
void stocks::openFile() {
	ofstream DataOutput;
	DataOutput.open("file.txt", ios_base::app);
	DataOutput << "User: " << user;
	for (int i=0; i <counter;i++){
		DataOutput << "\nStocks sold " << quan1[numOfOrder[i]-1] - quantity[numOfOrder[i]-1] << "  " << Description[numOfOrder[i]-1]
				   << "\nStock No.: " << numOfOrder[i]
				   << "\nInventory left for this item: " <<  quantity[numOfOrder[i]-1]
				   << "Total Amount sold: ";
	}
	DataOutput.close();
}
Last edited on
You cannot increment STOCKS because the array Description does not magically expand.

Suggestion: Turn line 26 to 30 into a struct and use a vector for the storage.
coder777, Thank you for the tip :D
Topic archived. No new replies allowed.