lundi 21 septembre 2020

I am running my code, but I am getting an error " Usage: ./main inputfilename outputfilename exit status 1"

///// Everytime I run this code, I am getting a fatal error while my code appear to be /////correct. so I don't know wha running this code it is going on with my code.

/*Purpose of Assignment 1: Implement and compare the running time of different sorting algorithms.

Goal of Part 1: Set up the program for necessary components in preparation for part 2, including reading a word list file, sorting the words using bubble sort, and checking if the word list included are sorted, etc. Description:

  1. Create a simple text file with less than 10 word strings.

  2. Sort data using bubble sorting

  3. Submit report on a word document */

    #include #include #include #include

    using namespace std;

    // PROTOTYPE for functions used by main()

    bool check_ordered(string data[], size_t n); void bubble_sort(string data[ ], size_t n);

    int main(int argc, char** argv) { // Check the number of parameters if (argc != 3) { // Tell the user how to run the program cerr << "Usage: " << argv[0] << " inputfilename outputfilename" << endl; return 1; }

     char* inputfilename = argv[1]; // read inputfilename
     ifstream inputfile; // file stream for input text file
     //------------------------------------------------------------------
     // TO-DO: Open the file and read in words
     // ------
     // Step 1: Open the file for read
     inputfile.open(inputfilename);
    
     // Please make sure your file can handle errors when openning a file
     // i.e., if the specified file does not exist, your program will
     // print out the error message and exit the program
     if (!inputfile) {
         cerr << "input file doesn't exist!" << endl;
         return 1;
     }
    
     // Step 2: Read the words into a string array
     size_t total_word;
    
     inputfile >> total_word;
     string words[total_word];
    
     // Keep in mind that the first line in the file contains the number
     // of words, and each rest line contains a single word
     for (size_t i{0};i < total_word;i++) {
         inputfile >> words[i];
     }
    
     // Step 3: Close the file
     inputfile.close();
    
     //------------------------------------------------------------------
     // TO-DO: Sort the string array in alphabetic order
     // ------
     // Please call the bubble_sort function here
     bubble_sort(words, total_word);
    
     //------------------------------------------------------------------
     // TO-DO: Check if the string array words is sorted (if not, exit program)
     // ------
     // Please call the check_ordered function here
     bool is_sorted = check_ordered(words, total_word);
     if (!is_sorted) {
         cerr << "string array is not sorted!" << endl;
         return 1;
     }
    
    
     char* outputfilename = argv[2]; // read output inputfilename
     ofstream outputfile; // file stream for output text file
     // TO-DO: Open the file and write all sorted words
     // ------
     // Step 1: Open the file for write
     outputfile.open(outputfilename);
    

    // Please make sure your file can handle errors when openning a file if (!outputfile) { cerr << "output file doesn't exist!" << endl; return 1; }

     // Step 2: Write the words into the file, one word per line
     // Please print one word per line.
     for (size_t i{0};i < total_word;i++) {
         outputfile << words[i] << endl;
     }
     // You don't need print the number of words in the first line.
    
     // Step 3: Close the file
     outputfile.close();
     return 0;
    

    }

     // Check whether a string array is sorted and print result
    

    bool check_ordered(string data[], size_t n) { //------------------------------------------------------------------ // TO-DO: Please include your implementation here for (size_t i{0};i < n - 1;i++) { if (data[i] > data[i + 1]) { return false; } } return true; }

    //////////////// Bubble Sort ////////////////////////////

    void swap(string & s1, string & s2) { string temp; temp = s1; s1 = s2; s2 = temp; } void bubble_sort(string data[ ], size_t n) // Using bubble sort to sort elements of data in ascending order // i.e., data[0] <= data[1] <= ... <= data[n-1] { size_t k = 0; bool switched = true; while (k < n-1 && switched) { switched = false; k++; for (size_t i = 0; i < n-k; ++i) { if (data[i] > data[i+1]) { swap(data[i], data[i+1]); switched = true; } } } }

Aucun commentaire:

Enregistrer un commentaire