0

For a problem, essentially I had to read integers from an input file, add them to an integer array, sort the array, and write a table with the number of appearences in the array for each value. Here is my code below. My input file is just 1 line (-3 4 1 1 3 4). My output file only prints the code produced by this line (output << setw(6) << left << "N" << "Count" << endl;) and nothing else. It works if I use cout, but doesnt print anything to the output file.

int main()
{
     ifstream input;
     input.open("TextFile2.txt");
     if (input.fail()) {
        cout << "file could not be opened." << endl;
        return 0;
     } 
     int arr[50];
     int num;
     int count = 0;
     while (input >> num) {
         arr[count] = num;
         count += 1;
     }
     selectionSort(arr, count);
     input.close();

     ofstream output;
     output.open("output.txt");
     printData(arr, count, output);
     output.close();

 }

 void swap(int& a, int& b) {
     int temp = a;
     a = b;
     b = temp;
 }

 void selectionSort(int arr[], int size) {
     for (int i = 0; i < size; i += 1) {
         int min_num = arr[i];
         int min_indx = i;
         for (int j = i; j < size; j += 1) {
             if (arr[j] < min_num) {
                 min_num = arr[j];
                 min_indx = j;
             }
         }
         swap(arr[i], arr[min_indx]);
     }
 }

 void printData(int arr[], int size, ofstream& output) {
     output << setw(6) << left << "N" << "Count" << endl;
     int prev = NULL;`
     for (int i = 0; i < size; i++) {
         int count = 0;
         if (arr[i] == prev) 
             continue;
    

         for (int j = 0; j < size; j++) {
             if (arr[i] == arr[j])
                 count += 1;
         }
         output << setw(6) << left << arr[i] << count << endl;
         prev = arr[i];
     }
 }`
p402
  • 1
  • 1
    You check the input stream for having been opened successfully, but not the output stream. Add this check, too, there might be reasons for this operation failing, too (e.g. only read-access to the directory or a text file without write permission already existing). – Aconcagua Mar 22 '23 at 07:30
  • 1
    `NULL` is an outdated (obsolete) C *macro* producing a null pointer. Sometimes simply defined as `#define NULL 0`, sometimes, though, as `#define NULL (void*)0` – you shouldn't be using this at all any more (there's now C++ `nullptr` *keyword* instead) – and especially not (even on older standard or in C) to initialise integrals. – Aconcagua Mar 22 '23 at 07:34
  • your code compiles for me (after removing the few ' left in there) and outputs correctly as well: N Count -3 1 1 2 3 1 4 2 – The Wrecker Mar 22 '23 at 07:38
  • A simpler algorithm would be using `std::map` – are you allowed to? `while(input >> num) { ++theMap[num]; } for(auto& pair : theMap) { output << pair.first << ": " << pair.second << '\n'; }` Sorting is done implicitly by the map ;) – Aconcagua Mar 22 '23 at 07:39
  • Even if your not allowed your loop can be written simpler (without nested loop): `size_t start = 0; for(i = 1; ...) { if(arr[i] != arr[start] { output << arr[start] << ' ' << i - start << '\n'; start = i; } } /* last sequence needs to be printed explicitly */ output << arr[start] << ' ' << size - start << '\n';` – Aconcagua Mar 22 '23 at 07:45
  • Add a test if the output stream is still OK after each write (`output << ... ; if(!output) { /* something has gone wrong!!! print an error message*/ }` – you might get a hint from. – Aconcagua Mar 22 '23 at 07:53
  • 2
    Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Mar 22 '23 at 07:54

0 Answers0