Ofstream/Ifstream help

Hello all, I have finished my program, but when I try to use ofstream and ifstream in order to print my output onto a different file, I either get errors or just a blank space in the prompt, would anyone know why this is happening? Thanks.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <fstream>
using namespace std;
bool isitavalidgroup(int, int, int, int&, int&);
void classify(int, int, int);
void rateonescore(int);
int findtotalscore(int, int, int);
void ratethegroup(int, int, int, int);
int main()
{int a, b, c, total, groupno, totgroups=0, validgroups=0, invalidgroups=0;
bool d;

    ofstream outfile ("Assignment4.txt");
    ifstream infile ("Assignment4.txt");
    outfile << "Enter the group number, negative to stop: ";
    infile >> groupno;
do
{
    outfile << "Enter the first SAT score: ";
    infile >> a;
    outfile << "Enter the second SAT score: ";
    infile >> b;
    outfile << "Enter the third SAT score: ";
    infile >> c;
    d= isitavalidgroup(a, b, c, validgroups, invalidgroups);
    outfile << d << endl;
    classify(a, b, c);
    outfile << "Enter the group number, negative to stop: ";
    infile >> groupno;
    totgroups++;
}
    while(groupno>=0);
    outfile << "There were " << validgroups << " valid groups" << endl;
    outfile << "There were " << invalidgroups << " invalid groups" << endl;
    outfile << "There were " << totgroups << " total groups" << endl;
outfile.close();
return 0;
}


//Input: All 3 user-inputted SAT scores, as well as the two counters.
//Process: Determine whether the scores are within the defined range.
//Output: A "+1" on the valid or invalid counter, and whether the result is true or not.
bool isitavalidgroup(int a, int b, int c, int&f, int&g)
{   int scores[3]; bool d = true;
    ofstream outfile ("Assignment4.txt");
    scores[0] = a;
    scores[1] = b;
    scores[2] = c;
    for(int n=0; n<3; n++)
{
    if(scores[n] < 200 || scores[n] > 800)
    {
        outfile << scores[n] << endl;
        d = false;
    }
}
    if(d==true)
    {   f++;
        return true;
    }
    else
    {   g++;
        return false;
    }

}

//Input: All three user-inputed SAT scores.
//Process: Classify serves as the call function to the other three functions.
//Output: Return the output of the three other functions.
void classify (int a, int b, int c)
{
rateonescore(a);
rateonescore(b);
rateonescore(c);
int total= findtotalscore(a, b, c);
ratethegroup(a, b, c, total);
return;
}

//Input: One SAT score at a time.
//Process: Determine whether the score is less than 500, greater than 500, or perfect.
//Output: A statement describing the score.
void rateonescore(int a)
{ofstream outfile ("Assignment4.txt");
if(a<500)
    outfile << "Less than 500" << endl;
else if(a>=500)
    outfile << "500 or above" << endl;
else
    outfile << "Perfect score, 800" << endl;
return;
}

//Input: The three user-inputted SAT scores.
//Process: Add the three scores.
//Output: The sum/total of these scores.
int findtotalscore(int a, int b, int c)
{   int total;
    total= a + b + c;
return total;
}

//Input: The three user-inputted SAT scores and their sum.
//Process: Determine the status of the scores and the total.
//Output: Output a one word response to the scores and the total.
void ratethegroup(int a, int b, int c, int d)
{ofstream outfile ("Assignment4.txt");
if(d>=2100)
{
    if(a>=700 && b>=700 && c>=700)
    outfile << "Outstanding" << endl;
    else if((a>=700 || b>=700) && (c>=700))
    outfile << "Very Good" << endl;
    else if((a>=700 && b>=700) || (c>=700))
    outfile << "Very Good" << endl;
    else if(a>=700 || b>=700 || c>=700)
    outfile << "Lop-Sided" << endl;
}
else if (d<2100 && a<500 && b<500 && c<500)
    outfile << "Weak" << endl;
else
    outfile << "Erratic" << endl;
return;
}
Hello ru12adis,

I have not tested your program yet and I do not know what the input file looks like. Please post a sample of the input file so I know what I am working with.

The first thing I noticed is that you open your output file in main and close it before main ends. Then you try to reopen the same output file in some of your functions, but you do not know if the open was successful or if it failed. I believe when you try to open the output file in the functions it failed and you are writing to something that does not exist. I use this bit of code to check if the file is open or if it failed:

1
2
3
4
5
6
7
8
9
10
11
12
13
outFile.open(oFileName, std::ios::trunc | std::ios::ate);

if (outFile.is_open())
{
	std::cout << "\n File " << oFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));
}
else
{
	std::cout << "\n File " << oFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));
	exit(1);
}

oFileName can be changed to a quoted string or defined as a string and initialized with the filename. Lines 6 and 11 require the header files"<chrono>" and "<thread>". When the open is working correctly I comment out lines 5 and 6. In line 1 I use the "trunc" and "ate" so that every time the file is opened it is fresh and you can see what happened with that run.

I noticed in main that you write to the output then read from the input file. Not sure yet how that is going to look i the output file, but since the output file is opened in main I am thinking it should be passed to the functions that need it and passed by reference.

I will work on the program after breakfast and see what I can come up with.

Hope that helps,

Andy
Topic archived. No new replies allowed.