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"
#include <iostream>
#include <fstream>
usingnamespace 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;
}
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
}
}
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
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.
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;
returntrue;
}
}
if (!is.eof()) returnfalse;
isSorted = true;
returntrue;
}
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."
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.
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
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.
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.