Purpose: The purpose of this program is the following:
Read in the input file and organize the data inside from lowest to highest.
Send the new data into an output text file.
Close everything and reopen the output text file onto the screen.
How can I accomplish the above with the Dev C++ compiler? The code below is error-free, as far as I can tell.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{ int a, b, c;
ifstream fin("input.txt");
ofstream fout("output.txt");
fin >> a >> b >> c;
cout << a << b << c <<endl;
while (a != -1)
{
if(a<b && b<c)
fout<<a<<" "<<b<<" "<<c<<endl;
elseif(b<c && c<a)
fout<<b<<" "<<c<<" "<<a<<endl;
elseif(c<a && a<b)
fout<<c<<" "<<a<<" "<<b<<endl;
elseif(b<a && a<c)
fout<<b<<" "<<a<<" "<<c<<endl;
elseif(c<b && b<a)
fout<<c<<" "<<b<<" "<<a<<endl;
elseif(a<c && c<b)
fout<<a<<" "<<c<<" "<<b<<endl;
fin >> a >> b >> c;
}
fin.close();
fout.close();
fin.open("output.txt");
fin >> a >> b >> c;
while (!fin.eof())
{
cout << a << b << c <<endl;
fin >> a >> b >> c;
}
fin.close();
return 0;
}
I am supposed to create a data file of the following integers, with the last line being the sentinel data line:
10 20 30
17 21 18
30 20 49
40 25 35
55 45 35
25 55 5
-1 -1 -1
Your problem description does not make it clear if you are only supposed to sort numbers within a line or across all lines in the text file.
Your code as you have it will only sort numbers within the same line. With that said, IMO, it's a poor design to assume that there are only three numbers per line without that being explicitly stated in the problem description.