Adding an entry to an array

I'm writing a program that will allow the user to load an array from a file, and then either print the contents of the array unsorted, print the contents sorted, or allow the user to add an entry to the array. Unfortunately I have successfully programmed the entire thing except adding an entry to the array. Given the current state of my program, could anyone assist me in letting me know how one may go about adding an entry to an array?

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
//Prog2_2_1
//Bubble Sort with menus

#include <iostream>
#include <string>
#include <algorithm>
#include "conio.h"
#include <stdio.h>
#include <iomanip> //input & output manipulation of files
#include <fstream> //filestream
#include <stdlib.h>
using namespace std;

void main()
{
	int i=0,j, flag=1;
	const int max=25;
	char fload = ' ';
	char cchie = ' ';
	string stfile_array[max],x,temp,newent;
	ifstream inFile;

	// initializes arry
	for (int z=0; z< max; z++)
		stfile_array[z]="blank";

	// prints initialized array

	/*for (z=0; z< max; z++)
		cout <<z<<"] "<<stfile_array[z]<<"\n";
		*/

	//system("PAUSE");

	cout<<"Would you prefer to load numbers or words into the array? \n";
	cout<<"Type 'n' for numbers and 'w' for words. \n";
	cin>> fload;
	fload = toupper(fload);
	system ("CLS");

	switch (fload)
	{
/*NUMBERS NUMBERS NUMBERS NUMBERS NUMBERS NUMBERS NUMBERS NUMBERS NUMBERS NUMBERS*/
case ('N'):
	{
	inFile.open("numbers.txt"); //Opens file. THIS FILE MUST BE IN UR PROJECT FOLDER!!

	if(!inFile) {
	cout<<"Unable to open file";
	exit(1); //terminate with error
	}

	while (inFile>>x)
	{
		stfile_array[i]=x;
		i++;
	}
	inFile.close();

	cout << "Would you like to print the [u]nsorted contents?\n";
	cout << "Or perhaps [s]ort the contents?\n";
	cout << "Or [a]dd an entry to the array?\n";
	cin >> cchie;
	cchie = toupper(cchie);
	system ("CLS");

	switch (cchie)
	{
/*PRINT UNSORTED PRINT UNSORTED PRINT UNSORTED PRINT UNSORTED*/
case ('U'):
{

//prints out array with unsorted contents
	for (z=0; z<max; z++)
		cout <<z<<"] "<<stfile_array[z]<< "\n";

	system("PAUSE");
	break;
} //Closes case 'U'

/*SORT SORT SORT SORT SORT*/
case ('S'):
{
	for (i=0; (i<=max) && flag; i++) //flag value 1
	{
		flag=0;
		for (j=0; j < (max-1);j++)
		{ if(stfile_array[j+1] < stfile_array[j])
		{temp = stfile_array[j]; //swap elements
		stfile_array[j] = stfile_array[j+1];
		stfile_array[j+1]=temp;
		flag = 1; // indicates that a swap occured.
		} // conditional

	//print changes as they occur
		for(int printout=0; printout<max; printout++)
			cout<<stfile_array[printout]<<"\n";
		system("PAUSE");
		} // 2nd for loop

	} // 1st for loop
	break;
} //Closes case 'S'

/*ADD AN ENTRY ADD AN ENTRY ADD AN ENTRY*/
case ('A'):
{
	cout << "What number would you like to add?\n";
	cin >> newent;
	while (stfile_array[j]!="blank")
	{
		j++;
		stfile_array[j]=newent;
		cout <<j<<"] "<<stfile_array[j]<< "\n";
	}
	break;
}

	break;
	} //Closes case 'N'
	} //Closes switch

/*WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS WORDS*/
case ('W'):
		{
	inFile.open("words.txt"); //Opens file. THIS FILE MUST BE IN UR PROJECT FOLDER!!

	if(!inFile) {
	cout<<"Unable to open file";
	exit(1); //terminate with error
	}

	while (inFile>>x)
	{
		stfile_array[i]=x;
		i++;
	}
	inFile.close();

	cout << "Would you like to print the [u]nsorted contents?\n";
	cout << "Or perhaps [s]ort the contents?\n";
	cout << "Or [a]dd an entry to the array?\n";
	cin >> cchie;
	cchie = toupper(cchie);
	system ("CLS");

	switch (cchie)
	{
/*PRINT UNSORTED PRINT UNSORTED PRINT UNSORTED PRINT UNSORTED*/
case ('U'):
{

//prints out array with unsorted contents
	for (z=0; z<max; z++)
		cout <<z<<"] "<<stfile_array[z]<< "\n";

	system("PAUSE");
	break;
} //Closes case 'U'

/*SORT SORT SORT SORT SORT*/
case ('S'):
{
	for (i=0; (i<=max) && flag; i++) //flag value 1
	{
		flag=0;
		for (j=0; j < (max-1);j++)
		{ if(stfile_array[j+1] < stfile_array[j])
		{temp = stfile_array[j]; //swap elements
		stfile_array[j] = stfile_array[j+1];
		stfile_array[j+1]=temp;
		flag = 1; // indicates that a swap occured.
		} // conditional

	//print changes as they occur
		for(int printout=0; printout<max; printout++)
			cout<<stfile_array[printout]<<"\n";
		system("PAUSE");
		} // 2nd for loop

	} // 1st for loop
	break;
} //Closes case 'S'

/*ADD AN ENTRY ADD AN ENTRY ADD AN ENTRY*/
case ('A'):
{
	cout << "What word would you like to add?\n";
	cin >> newent;
	while (stfile_array[i]!="blank")
	{
		i++;
		stfile_array[i]=newent;
		cout <<i<<"] "<<stfile_array[i]<< "\n";
	}
	break;
}

	break;
	} //Closes case 'W'
	}
	}
} // end program 
The easy way is to use a std::vector.
Topic archived. No new replies allowed.