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;
}
If I understood correctly, then it works on your original file, but not when you change it. So when you change the file, do you also change the other files you got inside functions to match?

I'd recommend passing the ofstream and ifstream object to the function via reference rather than declaring another inside your functions.
Topic archived. No new replies allowed.