Hey, I'm having trouble of making my program work. So far I managed it to read the file and display the numbers (just to verify that it is being read right), but I'm not sure if the storing is functioning well, as when I pick another option, well, it doesn't seem to work. My program is supposed to read the file then allow the user to pick between 4 other options: Bubble Sort, Selection Sort, Linear Search, and Binary Search. My main problem is getting the array from reading the file to pass on to the other options without having to enter the name of the file again. My friend has managed to do it using the getline command, but I wanted to try to do it differently, if possible.

Here's my read file code: (plus the check file code)

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

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

int ReadFile(){
// This function reads the file and stores the number in an array
     ifstream ListNumbers;
     string filename;
     int size = 0;
     float array[100]; // 100 is maximum amount of numbers in the file
     float numbers;
     
     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)
                 size++;
              cout << "The numbers are: " << numbers << endl;
        }
        cout << "The amount of numbers in the file is: " << size << endl;
        cout << "The file " << filename << " exists. " << endl;
        
        return 0;
     }
     else {
         cout << "The file doesn't exist." << endl;
         return 1;
     }
     ListNumbers.close();
}

And here is the code of the Bubble Sort: (I just wanted to start with this one so I could just get the hang of it in order to do the rest)

void 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;
     order = true;
}

Any hints or tips would be appreciated. I just need to be pointed in the right direction, thanks.

Deberias guardar los numeros del archivo a un arreglo para asi poderlos manipular en las otras funciones. =) Esto deberia funcionar.

int read_file(){
// This function reads the file and stores the number in an array
     ifstream filenumbers;
     string filename;
     char i=0;
     int size = 0;
     int array[100]; // 100 is maximum amount of numbers in the file
     int numbers;

     cout << "Enter the name of the file: " << endl;
     cin >> filename;

     if (check_file(filename)==true){
        filenumbers.open(filename.c_str(),fstream::in);

        while (!filenumbers.eof()){             // Keeps reading the file until it ends

        filenumbers >> array[i];

              if (filenumbers){
                 size++;
                i++;
        }
        }

                for (int i=0; i < size ; i++){
                        cout << array[i] << " " endl;
}
        cout << "The amount of numbers in the file is: " << size << endl;
        cout << "The file " << filename << " exists. " << endl;

   return 0;
     }
     else {
         cout << "The file doesn't exist." << endl;
         return 1;
     }
     filenumbers.close();
}

muchas gracias :)

Lo trate pero no me funciono. :/ Tienes otra sugerencia?

Edited 6 Years Ago by crodriguez08: n/a

muchas gracias :)

Lo trate pero no me funciono. :/ Tienes otra sugerencia?

Hmm para serte honesto, estoy teniendo unos problemas tambien. Tan pronto tenga la respuesta la posteo.

This question has already been answered. Start a new discussion instead.