file is note reading from the file

Why is this not reading from the file? I have it in the same directory and the write file functions works well enough.

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  //
//  main.cpp
//  GradeCalculator
//
//  Created by Andrew  on 6/4/13.
//
//

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//----------function declarations
string GetInFileName();
int DetermineLetterGrade();
int ReadFile();
string GetComment();
int GetLetterGrade();
int OpenFile();
string GetOutFileName();
int WriteData();
int CalculateGrade();

//-----------Main Body
int main()
{
    //ifstream& DataFile;
    string FileName;
    cout << "Welcome to the GradeCaculator Deluxe." << endl;
    OpenFile();
    ReadFile();
    GetComment();
    WriteData();
    //system ("pause");
    return 0;
}//end main
//-----------GetFileName
//-----------Get file name to read from
string GetInFileName()
{
    string DataFileName;
    cout << "Enter the name of the file to gather info from (including extension): ";
    cin >> DataFileName;
    return DataFileName;
    
}//end GetInFileName
//----------OpenFile
//----------open the user specified file to read from
int OpenFile()
{
    string FileName;
    ifstream DataFile;
    FileName=GetInFileName();
    
    DataFile.open(FileName.c_str());
    
    if (!DataFile)
    {
        cout << "File Failed to open...Program terminated" << endl;
        return 1;
    }//end if
    
    return 0;
}//end OpenFile
//---------ReadFile
//---------read the user specified file
int ReadFile ()
{
    int RowNumber;
    ifstream DataFile;
    int NoOfAssignments;
    float TotalPtsAllowed;
    int index;
    float TotalScore;
    int Grades;
    double Weight;
    float Average;
    float TotalGPA;
    
    RowNumber = 0;
    while (RowNumber < 3)
    {//start outter loop
        DataFile >> NoOfAssignments >> TotalPtsAllowed;
        index = 0;
        TotalScore = 0;
        
        while(index < NoOfAssignments)
        {//start (inner loop)
            DataFile >> Grades;
            index++;
            
            TotalScore = TotalScore + Grades;
            
        }//end while (inner loop)
        DataFile >> Weight;
        cout << TotalScore << "    " << TotalPtsAllowed << endl;
        Average = (TotalScore / TotalPtsAllowed) * Weight;
        TotalGPA = TotalGPA + Average;
        RowNumber++;
    }//end while (outer loop)
    cout << "Total GPA is:: " << TotalGPA << endl;
    return 0;
    
}//end ReadFile
//----------GetComment
//----------Create witty comment based on the letter grade
string GetComment ()
{
    string Quote;
    switch(GetLetterGrade())
    {
        case 'A' : Quote = "The Fonz says...Exactamundo!";
            break;
        case 'B' : Quote = "The Fonz says..Aaaaeeeyyy!";
            break;
        case 'C' : Quote = "The Fonz says he was wrrroooogh about you.";
            break;
        case 'D' : Quote = "The Fonz says...Step into my office.";
            break;
        case 'F' : Quote = "The Fonz says Whoa!";
            break;
    }
    cout << Quote << endl;
    return Quote;
}//end GetComment
//---------GetLetterGrade
//---------Get the letter grade based on the average
int GetLetterGrade()
{
    float TotalGPA;
    char LetterGrade;
    
    if  (TotalGPA > 3.50)
        LetterGrade = 'A';
    else
    {
        if (TotalGPA >= 2.40 && TotalGPA < 3.50)
            LetterGrade = 'B';
        if (TotalGPA >= 1.40 && TotalGPA < 2.40)
            LetterGrade = 'C';
        if (TotalGPA >= 0.040 && TotalGPA < 1.40)
            LetterGrade = 'D';
        if (TotalGPA < .40)
            LetterGrade = 'F';
    }//end else
    cout << "Your Letter Grade is:: " << LetterGrade << endl;
    return LetterGrade;
}//end GetLetterGrade
//----------GetOutFileName
//----------Get name of output file
string GetOutFileName()
{
    string OutDataFileName;
    cout << "Enter the name of the file to write info to (including extension): ";
    cin >> OutDataFileName;
    return OutDataFileName;
    
}//end GetOutFileName
//-----------WriteData
//-----------write the data to user specified file
int WriteData ()
{
    string FileName;
    ofstream OutDataFile;
    FileName=GetOutFileName();
    cout << FileName << endl;
    OutDataFile.open(FileName.c_str());
    //wrtie data out
    //OutDataFile.open("MyGrade.txt");
    if (!OutDataFile)
    {
        cout << "output File Failed to open...Program terminated" << endl;
        return 1;
    }//end if
    OutDataFile << "NAME: A.C. Millard" << endl;
    OutDataFile << "Your Letter Grade is ::" << GetLetterGrade() << endl;
    OutDataFile << GetComment() << endl;
    return 0;
}//end WrtieData
//----------CalculateGrade
//----------perform Grade Calcualtions
int CalculateGrade()
{
    int Weight;
    int Grade;
    int TotalScore;
    int  TotalPtsAllowed;
    Grade=(TotalScore / TotalPtsAllowed) * Weight;
    return Grade;
}


my text file looks like this:

6 600 87 88 89 90 91 92 0.70
4 400 95 96 97 98 0.20
3 300 100 100 100 0.10
You are using local variables for the ifstream, which only exists for the duration of the function or scope which contains it.

For example, function OpenFile() uses ifstream DataFile;, the file is opened. When the function ends, the stream is closed and the ifstream object is destroyed.

Then in function ReadFile() a new ifstream object is defined. But this one is not opened, so no input will be possible.

One solution is to define the ifstream just once in main() and then pass it by reference to the other functions.
@Manga, yes, I've got some cleaning up to do.
@Chervil, I,m not sure exactly how to do a reference variable (ifstream& DataFile).
every time I try it, I get an error. I know we talked about this yesterday and for the most part, I get it. Especially the part about when the function ends, the stream closes.

I've used functions quite a bit in VisualLisp (AutoCAD) but when you declare a variable in that language, it remains useable throughout the program until you redefine it.
Oh, sorry, I just looked at the question without realising we had discussed this before. Perhaps I'm not the right person to answer this.

However, based upon the previous discussion, and then looking at this latest code (which does have some good points, but also a lot of holes) it seems you are trying to run before you learned to walk.

The use of parameters is one of the glaring holes in the code at the start of this thread. There are no parameters passed to any of the functions.

What I'd suggest is to re-visit some of the ideas we looked at yesterday. But don't just read about them. Reading isn't sufficient. Get your compiler and write some code, it need only be ten or twenty lines long, to try out passing parameters to a function. Run the resulting program, see how it behaves, get a feel for these things by practising. I think that is a foundation which is necessary before attempting to build upon it.
Topic archived. No new replies allowed.