Hey, I'm pretty much almost done with this program, but I'm not sure how to clear a previous file (after opening it) in order to open a new one. My program is supposed to open a file with a maximum of 100 unordered decimals and allow the user to sort and/or search, or open a new file.
// This program is supposed to read a file with a maximum
// of 100 unordered decimal numbers and allows the user
// to sort the numbers and/or do a search.
# include <iostream>
# include <fstream>
# include <string>
usingnamespace std;
// Prototypes
void Instrucciones(void);
int menu(void);
bool CheckFile(string filename);
int ReadFile();
bool BubbleSort(float [], int);
bool SelectionSort(float [], int);
void LinearSearch(float [], float, float);
void BinarySearch(float [], int, float);
int main()
{
// Declaring variables
int opcion = 0;
ifstream ListNumbers;
string filename;
int size = 0;
float array[100];
float numbers;
float value;
bool order;
// This while is so the program will keep looping as long as the option
// chosen is between 1-5.
while (opcion >= 1 || opcion <= 6){
// Display instructions
Instrucciones();
// Display menu
opcion = menu();
// Displays the option chosen by the user
cout << "You've chosen option: " << opcion << endl << endl;
if (opcion == 1){
cout << "Enter the name of the file: " << endl;
cin >> filename;
if (CheckFile(filename)==true){
ListNumbers.open(filename.c_str(),fstream::in);
while (!ListNumbers.eof()){
// Keeps reading the file until it ends
ListNumbers >> numbers;
if (ListNumbers){
array[size] = numbers;
//ListNumbers >> numbers;
cout << "The numbers are: " << array[size] << endl;
size++;
}
}
cout << "The amount of numbers in the file is: " << size << endl;
cout << "The file " << filename << " exists. " << endl;
order = false;
ListNumbers.close();
}
else {
cout << "The file doesn't exist." << endl;
}
}
Allright, well I tried the .clear() command, and it somewhat works, unfortunately, when i open the new file (after opening a previous one), this time it shows the numbers from the new file, but it also shows/keeps the numbers from the previous file, and keep in mind that this isn't the whole program. If you want me to post it completely, let me know.
No, that's the problem, I'm not quite sure how to do it. Would I have to use something like delete [] array? And as for the resetting of counter variables, well I am unsure of where to reset it.
I tried resetting the size variable by inserting size = 0; right before I closed ListNumbers and option 1 works fine, but when I go to do other options they won't work. Any suggestions?