[Solved] Unexpected close and other issues

Three semesters ago I took a C programming class where I had to write a program (which worked completely) similar to this one and I am currently learning C++ (on my own) so I decided to turn that program into C++ and make some changes since I had a lot of fun with this one. After changing it to C++ syntax and adding the "print" function I have the following problems:

The program unexpectedly closes after I choose the "Remove Account" option and it executes. All other functions return to the menu. At first I thought it was because it was the last function written in the list but none of the other functions have a call back to main either and they don't cause it to close.

Another problem I am experiencing is that the "Quit" option does nothing. I know there is no code under that option but when the code was still written in C it closed without needing any code there. I tried briefly searching for a close command for C++ and for some reason all of the solutions I found gave me errors.

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

using namespace std;

void deposit (float information[10][3]);
void withdraw (float information[10][3]);
void interest (float information[10][3]);
void display (float information[10][3]);
void account_add (float information[10][3]);
void account_remove (float information[10][3]);
void print (float information[10][3]);

//---------------------------------------------------------------------------------

int main (void)
{
    int choice;
    float information[10][3]={{0},{0}};

    while (choice !=7)
    {
        cout << endl << "What would you like to do?" << endl;
        cout << "\t<1> Deposit\n\t<2> Withdraw\n\t<3> Add interest\n\t<4> Display Account Information\n\t<5> Print Account Information\n\t<6> Add an Account\n\t<7> Remove an Account\n\t<8> Quit\n";
        cout << endl << "Your Choice: ";
        cin >> choice;

        switch (choice)
        {
        case 1:
            deposit(information);
            break;
        case 2:
            withdraw(information);
            break;
        case 3:
            interest(information);
            break;
        case 4:
            display(information);
            break;
        case 5:
            print(information);
            break;
        case 6:
            account_add(information);
            break;
        case 7:
            account_remove(information);
            break;
        case 8:
            break;
        }
    }

return 0;
}

//---------------------------------------------------------------------------------

void deposit (float information[10][3])
{
	int account;
	float amount;
	int type;
	int accountnum;
	int usedaccount=0;

	cout << "The accounts in use are: ";
		for (accountnum=0; accountnum<10; accountnum++)
		{
			if (information[accountnum][0]/1 != 0)
			{
				cout << accountnum+1;
				usedaccount = usedaccount+1;
			}
		}
	if (usedaccount == 0)
    {
        cout << "None" << endl;
    }
    else
    {
        cout << endl << "What is your account number?" << endl;
        cin >> account;
        cout << "How much do you want to deposit?" << endl;
        cin >> amount;
        cout << "Which account do you want to deposit to?\n<1> Checking\n<2> Savings" << endl;
        cin >> type;

        information[account-1][type] = information[account-1][type] + amount;
    }
}

//---------------------------------------------------------------------------------

void withdraw (float information[10][3])
{
	int account;
	float amount;
	int type;
	int accountnum;
	int usedaccount=0;

	cout << "The accounts in use are: ";
		for (accountnum=0; accountnum<10; accountnum++)
		{
			if (information[accountnum][0]/1 != 0)
			{
				cout << accountnum+1;
				usedaccount = usedaccount+1;
			}
		}
	if (usedaccount == 0)
    {
        cout << "None" << endl;
    }
    else
    {
        cout << endl << "What is your account number?" << endl;
        cin >> account;
        cout << "How much do you want to withdraw?" << endl;
        cin >> amount;
        cout << "Which account do you want to withdraw from?\n<1> Checking\n<2> Savings" << endl;
        cin >> type;

        information[account-1][type] = information[account-1][type] - amount;
    }
}

//---------------------------------------------------------------------------------

void interest (float information[10][3])
{
	int account;
	int months;
	int i;

	cout << "How many months do you want to accrue interest for? ";
	cin >> months;

	for (i=0; i<months; i++)
		for (account=0; account<10; account++)
			information[account][2] = information[account][2]*1.01;
}

//---------------------------------------------------------------------------------

void display (float information[10][3])
{
	int row;
	int column;
	int accounts=0;

	cout << endl << "Account Number" << setw(20) << "Checking Balance" << setw(20) << "Savings Balance" << endl << endl;

	for (row=0; row<10; row++)
	{
		if (information[row][0]!=0)
		{
			accounts++;
			for (column=0; column<3; column++)
			{
				if (column == 0)
				{
					cout << setw(14) << information[row][column];
				}
				else
				{
					cout << setw(20) << information[row][column];
				}
			}
		cout << endl;
		}
	}
	if (accounts == 0)
	{
		cout << "There are no accounts." << endl;
	}
}

//---------------------------------------------------------------------------------

void print (float information[10][3])
{
	int row;
	int column;
	int accounts=0;
	char file[10];

    cout << endl << "What do you want to name the file?" << endl << "*Remember to include the .txt extension.*" << endl << "*Only use up to 10 characters.*" << endl;
    cin >> file;
    cout << endl;

    ofstream myfile;
    myfile.open (file, ios::out | ios::trunc);

	myfile << "Account Number" << setw(20) << "Checking Balance" << setw(20) << "Savings Balance" << endl << endl;

	for (row=0; row<10; row++)
	{
		if (information[row][0]!=0)
		{
			accounts++;
			for (column=0; column<3; column++)
			{
				if (column == 0)
				{
					myfile << setw(14) << information[row][column];
				}
				else
				{
					myfile << setw(20) << information[row][column];
				}
			}
		myfile << endl;
		}
	}
	if (accounts == 0)
	{
		myfile << "There are no accounts." << endl;
	}
	myfile.close();
}

//---------------------------------------------------------------------------------

void account_add (float information[10][3])
{
	int account;
	int accountnum;
	float checking;
	float savings;
	int accountopen=0;

	for (accountnum=0; accountnum<10; accountnum++)
	{
		if (information[accountnum][0]/1 == 0)
		{
			accountopen++;
		}
	}
	if (accountopen == 0)
	{
		cout << endl << "There are no more accounts available. Please remove one and try again." << endl;
	}
	else
	{
		cout << "What account number would you like to create?" << endl;
		cout << "The accounts available are: ";

		for (accountnum=0; accountnum<10; accountnum++)
		{
			if (information[accountnum][0]/1 == 0)
			{
			    cout << " ";
				cout << accountnum+1;
				if ((accountnum+1) < 10)
				{
				    cout << ",";
				}
			}
		}

		cout << endl;
		cin >> account;
		information[account-1][0]=account;

		cout << "How much would you like to deposit into your checking account? ";
		cin >> checking;
		information[account-1][1]= checking;

		cout << "How much would you like to deposit into your savings account? ";
		cin >> savings;
		information[account-1][2]=savings;
	}
}

//---------------------------------------------------------------------------------

void account_remove (float information[10][3])
{
	int account;
	int column;
	int accountnum;
	int usedaccount=0;

	cout << "The accounts in use are: ";
		for (accountnum=0; accountnum<10; accountnum++)
		{
			if (information[accountnum][0]/1 != 0)
			{
				cout << accountnum+1;
				usedaccount = usedaccount+1;
			}
		}
    if (usedaccount == 0)
    {
        cout << "None" << endl;
    }
    else
    {
        cout << "What is the number of the account you wish to remove?" << endl;
        cin >> account;

        for (column=0; column<3; column++)
        {
            information[account-1][column] = 0;
        }
    }
}

Last edited on
Have a closer look at the condition in your main loop (line 22).
When will it terminate?

Cheers,
Jim
Wow, I can't believe I missed that! Thank you so much, it was bothering me that I couldn't find it.
Topic archived. No new replies allowed.