Ok, here is the error: "warning C4700: uninitialized local variable 'stude' used"
And here is the code (You guys are so great! )
Code:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
class Student
{
public:
void calculate();
int writeToFile();
void average();
char name[20];
int age, grade;
int numtests;
int i;
int *iPtr;
float avg;
};
int main()
{
Student stud;
//Ask for students name
cout << "Enter the student's name: ";
cin.get(stud.name, 20, '\n');
cout << "Enter the student's age and grade seperated by a space: ";
cin >> stud.age >> stud.grade;
stud.calculate();
stud.average();
stud.writeToFile();
cout <<"\n";
return 0;
}
//Calculate grades
void Student::calculate()
{
Student st;
cout << "Enter the number of tests you are entering: ";
cin >> st.numtests;
st.iPtr = new int[st.numtests];
for (st.i=0; st.i<st.numtests; st.i++)
{
cout << "Enter test score #" << st.i + 1 << " : ";
cin >> st.iPtr[st.i];
}
}
//Calculate Averages
void Student::average()
{
Student stude;
cout << "Calculating the average of your percentages.....\n";
for (stude.i=0; stude.i < stude.numtests; stude.i++) //The warning points to this line
{
stude.avg += stude.iPtr[stude.i];
}
stude.avg /= stude.numtests;
cout << "The agervage of the percentiles = " << stude.avg << endl;
}
//WritetoFile function
int Student::writeToFile()
{
Student stu;
cout << "Now writing data to file.....";
fstream a_file("C:\\grades.txt");
if (a_file.is_open() )
{
cout << "The file opened incorrectly, program terminating.";
return 0;
}
else
{
a_file << "The student's name is: " << stu.name << endl;
a_file << "The student's grade is: " << stu.grade << endl;
for (stu.i = 0; stu.i < stu.numtests; stu.i++)
{
a_file << "Test score #" << stu.i + 1 << " is " << stu.iPtr[stu.i] << endl;
delete [] stu.iPtr;
}
a_file << "The average of the test grades is: " << stu.avg << endl;
a_file.close();
cout << "Data finished writing and file closed......";
}
return 0;
}