bool function to return true or false

Dec 10, 2015 at 6:26pm
Hello!

I have made a simple program that checks if a file is sorted or not in terms of the numbers in it. It is just a assignment for us new to c++.

The code currently just say that the file is unsorted the numbers of times it finds a number that is not in the correct order. If it dont find any numbers out of order it wont give any output.

So after some thinking a bit about it i thought that i can fix it with a bool function but i am so new to this that i dont know where to start even after spending several hours trying to learn how to fix my problem.

I was thinking that if it finds a number out of order it will return false and if the program dont find any numbers out of order it will return true and based on that i can give the correct output "This file is sorted" or "This file is not sorted"

Thanks for the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

ifstream numbers;
int newnr;
int oldnr;

numbers.open("A");

if (!numbers.is_open())
  cout << "The file did not open correctly" << endl;

numbers >> oldnr;

while (!numbers.eof())
{
  numbers >> newnr;

  if (oldnr > newnr)
    cout << "The file is not sorted" << endl;

  oldnr = newnr;

}
  return 0;
}
Dec 10, 2015 at 6:37pm
I was thinking that if it finds a number out of order it will return false and if the program dont find any numbers out of order it will return true and based on that i can give the correct output "This file is sorted" or "This file is not sorted"


I thinks it's a good idea. I would create a function like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool IsFileSorted(string filename)
{
  your code here
}

Then in your main function you could write it like this:
int main()
{
  if( IsFileSorted("filename"))
  {
     // output sorted
  }
  else
  {
    // output not sorted
  }
}
Dec 10, 2015 at 6:46pm
You need to close the file after you are done with it

Also you should do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

if (!numbers.is_open())
{
  cout << "The file did not open correctly" << endl;
}
else if (numbers.is_open())
{
numbers >> oldnr;
while(numbers)
{
numbers >> newnr;
....


}
numbers.close();

Dec 10, 2015 at 6:56pm
@alex067 -
You need to close the file after you are done with it

ifstream's destructor will take care of closing the file, although it certainly does not hurt to do so yourself.

Line 4: It is best to exit the program if you can't open the file. As you have it, you continue to line 8 as if the file opened successfully.

Line 6: There is no need to repeat the if condition in the else. If the file is not open (line 2), then it can only be open.


Last edited on Dec 10, 2015 at 6:57pm
Dec 10, 2015 at 7:09pm
Well you dont need to FORCE to exit the program since this is all in the main, and if the file is closed the program will just by default execute since the next line that will execute is return 0
Dec 10, 2015 at 9:40pm
alex067 wrote:
you dont need to FORCE to exit the program

It's a matter of safety. Yes, the loop at line 9 won't execute because numbers.fail() is true and will fall through to the return 0.

However, consider if code is added after the loop that depends on the fact that records were read. You now have possibly unpredictable behavior because the loop failed. It is better practice to deal with the failure to open the file by not continuing to execute the program.
Dec 10, 2015 at 10:42pm
I'll take this opportunity to point out a nice trick. What should you do if there is an error with the file? A handy trick is to have your function return true/false to indicate success/error. Then pass in a bool variable as a reference that will indicate if the file is sorted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool isFileSorted(stream &is, bool &isSorted)
{
    int last, cur;
    is >> last;
    while (is >> cur) {
        if (cur < last) {
            isSorted = false;
            return true;
        }
    }
    if (!is.eof()) return false;
    isSorted = true;
    return true;
}


If you write functions like this, you avoid the nasty ambiguity of whether a false return means "there was an error" or whether it means "the answer is false."

Dec 10, 2015 at 11:53pm
If you write functions like this, you avoid the nasty ambiguity of whether a false return means "there was an error" or whether it means "the answer is false."

That function signature is very confusing. I think I'd rather let the caller check the status of the stream after the call.
Dec 11, 2015 at 12:27am
Well if you want to talk about safety @Abstract then you contradicted yourself by saying its not important to close the file..
Dec 11, 2015 at 12:38am
Well if you want to talk about safety @Abstract then you contradicted yourself by saying its not important to close the file..

I invite you to google RAII.

It isn't important to manually close the file, because the stream is an object that closes itself when it is destroyed.
Dec 11, 2015 at 12:41am
Ok then if closing the file isn't about safety, then telling the program to prematurely break in an if else statement IN the main is also not about safety..

do you see my point?

actually the snippet of the code i posted does this:

if it doesnt open, the while loop and everything before it will not execute, and will simply return to return 0 in the amin
Dec 11, 2015 at 12:46am
Ok then if closing the file isn't about safety, then telling the program to prematurely break in an if else statement IN the main is also not about safety..

do you see my point?

I see your point. I see his point. I don't see what any of it has to do with closing the file.
Dec 11, 2015 at 2:34pm
Thanks for the help, i managed to fix it. However i am trying to put my function in a seperate .cpp file just to learn it but i having problems getting it to work. I am making a new thread about it.
Topic archived. No new replies allowed.