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?
//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>
usingnamespace std;
void main()
{
int i=0,j, flag=1;
constint 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