-2

I'm trying to create a function and link it to a header file and call the function to my main.cpp. This is the code from one function which I'll be calling in my main.cpp file. I'm trying to create a sort function that determines whether the integers in the file are sorted in order or not.

The file I'll be reading from can both be sorted and not sorted and output for the user the results, depending on the outcome of the file. Hopefully, I'm explaining in a clear! :S

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include "SortingFunc1.h"

int file_sort_checker() {
  int nums;
  std::string in_file_name;
  std::ifstream resultat;
  resultat.open("A");
  resultat >> nums;

  while (resultat.eof()) {
    bool resultat = std::is_sorted(in_file_name.begin(), in_file_name.end());

    if (resultat)
      std::cout << "Filen är sorterad!" << nums << std::endl;
    else {
      std::cout << "Filen är inte sorterad!" << nums << std::endl;
    }
    resultat >> nums;
  }

  resultat.close();
}
David G
  • 94,763
  • 41
  • 167
  • 253
Amiu
  • 21
  • 7
  • This code doesn't need refactoring; it's needs *factoring*. I'd start by explaining everything you're currently doing [to you rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging), because frankly this makes no sense. The task itself requires you read one integer, then continue reading subsequent integers from the file, each time comparing to the last one read, and so long as the first one is greater or equal to the last, the "sorted" state still holds. As soon as you read a number that is less than the prior number, the sort is broken and you can return as-such. – WhozCraig Dec 02 '21 at 23:10
  • 1
    What section of the code are you trying to refactor? Why are you refactoring? – Thomas Matthews Dec 02 '21 at 23:10
  • 2
    This code checks to see if the characters in `in_file_name` are sorted. Sadly `in_file_name` is an empty string and it has nothing to do with the file contents. You should read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Retired Ninja Dec 02 '21 at 23:11
  • This code doesn't work! I'm trying to make it work, but I can't now! That is why I ask here! If you want to know! – Amiu Dec 02 '21 at 23:12
  • 1
    What you could do is: `std::string message; if (resultat) message = "Filen är sorterad!"; else message = "Filen är inte sorterad!"; std::cout << message << nums << std::endl;` – Thomas Matthews Dec 02 '21 at 23:13
  • Refactoring code and making it work are distinct and mutually exclusive things. Refactoring is the process of restructuring code without changing its observable behaviour - which, among other things, means that code which doesn't "work" will still not work after refactoring. – Peter Dec 02 '21 at 23:13
  • 1
    You will need to decide if you want to read all of the data into a container, like a `std::vector` and then determine if that data is sorted or if you want to do it on the fly by remembering the last item read and comparing it with the current item to see if those two are sorted then repeating that process until you reach the end of the file or you read a value that is less than the previous. – Retired Ninja Dec 02 '21 at 23:15
  • 1
    Your function is checking if the **filename** is sorted, not the file. If you want to use `std::is_sorted`, you need an `ifstream` iterator. – Thomas Matthews Dec 02 '21 at 23:15
  • Thank you Thomas, I'll check the iterator out. Could you please give an example of itirator! – Amiu Dec 02 '21 at 23:18

1 Answers1

1

Here is a code fragment that checks if numbers in a file are sorted, ascending:

std::ifstream resultant("A");
int previous_number;
int number;
resultant >> previous_number;
bool is_sorted = true;
while (resultant >> number)
{
    if (number < previous_number)
    {
        std::cout << "File not sorted\n";
        is_sorted = false;
        break;
    }
    previous_number = number;
}

The previous number is primed by reading the first number into the variable.
The loop then compares the next number read to the previous. The loop will continue if the next number is greater than or equal to the previous number.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • How can I learn like you Thomas! Can you please give me some advice! – Amiu Dec 02 '21 at 23:26
  • I'm 22 years old and study computer engineering my second course in college is about c++ and file handling. I can write the simple program but when it is too complicated I do not know how to think as you did here! – Amiu Dec 02 '21 at 23:28
  • 1
    This is what I would call a fundamental or brute-force algorithm. From looking at a sorted container (list) of numbers, we know that it is sorted if number[x} < number[y]. So, we read in the first number as number[x] then read in the next number as number[y], and compare them. Next, we copy the number read as the previous number and repeat. Simple algorithm. Another method would be to read all the numbers into `std::vector` then compare the elements in the vector. – Thomas Matthews Dec 02 '21 at 23:41