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>

using namespace 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;  
  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;
     }
    
}
   
  else if(opcion == 2){ 
       order = BubbleSort(array, size);
  }
  else if(opcion == 3){        
       order = SelectionSort(array, size);
  }
  else if(opcion == 4){       
       LinearSearch(array, numbers, value);
       
  }
  else if(opcion == 5){ 
       if (order == true)          
          BinarySearch(array, size, value);
       else if (order == false){
            cout << "Please sort the numbers first. " << endl;
       }
  }
  else {
       return 0;
  }

}
} // End main

// This function displays the instructions and the menu.
void Instrucciones(){
cout << "This program allows the user to create a class and input student records. \n" << endl;
}

int menu(){

  int opcion;

  cout << "Menu:" << endl;
 
cout << "\t1) Read File" << endl;
cout << "\t2) Bubble Sort" << endl;
cout << "\t3) Selection Sort" << endl;
cout << "\t4) Linear Search" << endl;
cout << "\t5) Binary Search" << endl;
cout << "\t6) Exit Program \n" << endl;
 
// Selection of the options; if the option chosen is great than 6
// or less than 1, then the program will ask the user for a number
// between 1-6.
   do{  
     cout << "Choose an option from the Menu: " << endl;
     cin >> opcion;
     while ((opcion < 1) || (opcion > 6)){
                 cout << "Error: Choose between the options 1-6: " << endl;
                 cin >> opcion;
                 }
     } while ((opcion < 1) || (opcion > 6));
     return opcion;
}

bool CheckFile(string filename){
// This function verifies to see whether the class exists or not

    ifstream ListNumbers;
     
    ListNumbers.open(filename.c_str(),fstream::in);
    if (ListNumbers.good()){
        ListNumbers.close();
        return true ;
    }
    return false;
}


bool BubbleSort(float array[], int size){
     bool swap;
     float temp;
     
     do {
         swap = false;
         for (int i = 0; i < (size - 1); i++){
             if (array[i] > array[i + 1]){
                temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                swap = true;
             }
         }
     } while (swap);
     cout << "This is the array in ascending order: " << endl;
          for (int i = 0; i < size; i++)
              cout << array[i] << endl;
     
     cout << endl;
     return true;
}

bool SelectionSort(float array[], int size) {
     int minIndex;
     float minValue;
     
     for (int startScan = 0; startScan < (size - 1); startScan++){
         minIndex = startScan;
         minValue = array[startScan];
         for (int i = startScan + 1; i < size; i++){
             if (array[i] < minValue){
                minValue = array[i];
                minIndex = i;
             }
         }
         array[minIndex] = array[startScan];
         array[startScan] = minValue;
     }
     cout << "This is the array in ascending order: " << endl;
          for (int i = 0; i < size; i++)
              cout << array[i] << endl;
     
     cout << endl;
     return true;
}

void LinearSearch(float array[], float numbers, float value){
    int i = 0;
    int position = -1;
    bool found = false;
    
    cout << "What is the number you are searching for? " << endl;
    cin >> value;
    
    while (i < numbers && !found){
          if (array[i] == value){
             found = true;
             position = i;
          }
          
          i++;
    }
   if (position == -1)
      cout << "The number does not exist. " << endl;
   else {  
        cout << "The position of the number you are searching for is at: "
        << position << endl;
   }
}

void BinarySearch(float array[], int size, float value){
    int first = 0;
    int last = size - 1;
    int middle;
    int position = -1;
    bool found = false;
    
       cout << "What is the number you are searching for? " << endl;
       cin >> value;
    
       while (!found && first <= last){
          middle = (first + last) / 2;
          if (array[middle] == value){
             found = true;
             position = middle;
          }
          else if (array[middle] > value)
               last = middle - 1;
          else
              first = middle + 1;
       }
       if (position == -1)
            cout << "The number does not exist. " << endl;
       else {  
            cout << "The position of the number you are searching for is at: "
            << position << endl;
       }
}

Any help or hints would be appreciated, thanks.

Since the ifstream variable ListNumbers is only used inside the iblock of the if statement on line 51 if you declared it on line 52 instead of line 26 then there would be no problem you will have a new file object every time the user choose to open a file.

The only addition is to reset size inside the same if block before reading the file so you fill the array in from the start again.

Okay, I changed the location of the ifstream ListNumbers, but when I reset the size to 0 right before I closed ListNumbers, the option 1 works fine, but when I try to do other options with the file, it won't work, it only comes out in blank since size = 0 I believe. Any suggestions?

I didn't say clear size before closing the file I said clear size before reading the file.

Come on put some thought into it size has to be the number of records you just read so once if have finished reading a file you don't want to clear size because you will destroy that file. However as you come to read another file you want to start at the beginning of your array again and you want to start counting read records again so that you don't add to what was read from the last file; therefore before reading the file again you need to clear size.

Oh okay, thanks. Sorry for having misunderstood. It works fine now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.