How to create a file with Dev C++

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.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <iostream>
#include <fstream>

using namespace 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;
    else if(b<c && c<a)
        fout<<b<<" "<<c<<" "<<a<<endl;
    else if(c<a && a<b)
        fout<<c<<" "<<a<<" "<<b<<endl;
    else if(b<a && a<c)
        fout<<b<<" "<<a<<" "<<c<<endl;
    else if(c<b && b<a)
        fout<<c<<" "<<b<<" "<<a<<endl;
    else if(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;
}
What makes you think there are only three values in the file? Your problem statement says nothing about only three values.

Duplicate thread. Please don't spam the forum with multiple threads for the same topic. It wastes everyone's time, including yours.

http://www.cplusplus.com/forum/general/185618/
Last edited on
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.
Topic archived. No new replies allowed.