Hi. So i have to do the program where we read from a file a certain amount of numbers(not more than 100), and then perform either a bubble or selection sort and a linear or binary search. I wrote the thing down and it read fine, but I can't get it to do anything else. I've been working on it for a while but nothing seems to work. Any help?

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void display_instructions();
void menu();
bool check_file(string filename);
int read_file(int[], int);
void bubble_sort(int[], int);
void selection_sort(int[], int);
int linear_search(int[], int, int);
int binary_search(int[], int, int);




int main() {
		
		display_instructions();
                menu();

}




void display_instructions () {
                cout << "     " << endl ;
                cout << " ************************************************* " << endl ;
                cout << " This program reads an assortment of numbers and \n " ;
                cout << "sorts them in the desired manner." << endl ;
                cout << " ************************************************* " << endl ;
                cout << "     " << endl ;
                }

void menu () {
        char option;
        while (option != 'Q'){
                cout << " \t Menu : " << endl ;
                cout << " \t \t Press 1 to read a file " << endl ;
                cout << " \t \t Press 2 to perform a bubble sort " << endl ;
                cout << " \t \t Press 3 to perform a selection sort " << endl ;
                cout << " \t \t Press 4 to perform a linear search " << endl ;
                cout << " \t \t Press 5 to perform a binary search " << endl ;
                cout << "     " << endl ;
                cout << " Choose an option and press enter or \n press \" Q \" and Enter to quit: " ;

                cin >> option;

                switch (option) {
                        int array[100], size, value;

                        case '1' :
							bool check_file(string filename);
							read_file(array , size) ;
							break;
						case '2' :
							bubble_sort(array, size);
							break;
                        case '3':
							selection_sort(array, size);
							break;
                        case '4':
							linear_search(array, size, value);
							break;
						case '5':
							binary_search(array, size, value);
							break;

                }
        }
}
bool check_file(string filename){
    ifstream filenumbers;

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

int read_file(int array[100], int size){        
     ifstream filenumbers;
     string filename;
     int i=0;
     size = 0;
	 
     cout << "Enter the name of the file to be read: " << endl;
     cin >> filename;

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

        while (!filenumbers.eof()){     

			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();
     return size;
}

void bubble_sort(int array[],int size){
        bool swap;
        int 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); 
			for (int i=0; i<size ; i++) {
                cout << array[i] << " , " << endl;
			}
}


void selection_sort(int array[],int size){
        int startscan, minindex, minvalue;

        for (startscan=0 ; startscan<(size-1) ; startscan++) {
                minindex=startscan;
                minvalue=array[startscan];
                for(int index=startscan+1 ; index<size ; index++) {
                        if (array[index] < minvalue) {
                                minvalue = array[index];
                                minindex = index;
                        }
                }
                array[minindex] = array[startscan];
                array[startscan] = minvalue;
        }
}
int linear_search(int array[], int size, int value) {
		cout << "Insert the number you wish to find:" << endl;
		cin >> value;
        int index = 0;      // Used as a subscript to search array 
        int position = -1;  // To record position of search value 
        bool found = false; // Flag to indicate if value was found 
        while (index < size && !found) {
                if (array[index] == value) { // If the value is found 
                        found = true; // Set the flag 
                        position = index;
						}
                index++; // Go to the next element 
        }
		cout << position; // Record the value's subscript 
        return position; // Return the position, or -1

}


int binary_search(int array[], int size, int value) {
        int first = 0,             // First array element 
                last = size - 1,       // Last array element 
                middle,                // Mid point of search 
                position = -1;         // Position of search value 
                bool found = false;        // Flag 
        while (!found && first <= last) {
                middle = (first + last) / 2;     // Calculate mid point 
                if (array[middle] == value) {     // If value is found at mid 
                        found = true;
                        position = middle;
                }
        else if (array[middle] > value)  // If value is in lower half 
                last = middle - 1;
                else
                        first = middle + 1;           // If value is in upper half 
        }
		
        return position;
}

Recommended Answers

All 2 Replies

> switch (option) {
> int array[100], size, value;
Your array and size go out of scope (and lose their values) every time you make a new choice.

Move these to an outer scope, one which persists for as long as you need it to.

And please fix your indentation! It's very hard to read.

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.