I'm trying to output data to a file. I half killed myself trying to get the program to work , everything works all data is working except for a few errors will show later. Heres my code
// Week 13: In Class Exercise - 5 Solution
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
// Structure definition
struct record
{
string lastName;
float quiz1;
float quiz2;
float midterm, midterm_one;
float final, final_one;
int quiz_average;
char final_grade;
float total_grade;
};
const int NUMBER_SCORES = 5;
// Function prototypes
void programDescription();
void writerecord(ofstream&, record);
void readrecord(ifstream&, record&);
void gradeAverage(record);
int main()
{
// Program description
programDescription();
// Declare variables
record stu1;
ofstream outFile;
ifstream inFile;
// open file for input
inFile.open("gradesIn.txt");
if(inFile.fail())
{
cout << "Error opening file" << endl;
exit(1);
}
// open file for output
outFile.open("gradesOut.txt");
if(outFile.fail())
{
cout << "Error opening file" << endl;
exit(1);
}
// Process data as read and display in tabular format
//Display header line
cout << endl << endl
<< "Last Quiz Quiz Mid-Term Final" << endl
<< "Name 1 2 1 Exam" << endl
<< "------ ----- ----- -------- ------" << endl;
//Loop to read in data
//Loop to display array
while (inFile >> stu1.lastName && !inFile.eof())
{
readrecord(inFile, stu1);
// Display structure data
cout << fixed << setprecision(2);
cout << left << setw(14);
cout << stu1.lastName << setw(9) << stu1.quiz1 << setw(10)
<< stu1.quiz2 << setw(12) << stu1.midterm << setw(15)
<< stu1.final << endl;
writerecord(outFile, stu1);
gradeAverage(stu1);
}
// Close data files
inFile.close();
outFile.close();
return 0;
}
void programDescription()
{
cout << "Welcome to the program student record";
cout << endl << "this program takes in 2quizes, a final and midyear";
cout << endl << "and then calculates the average by what they are worth by";
cout << endl << "the quizes together are worth 25% and midyear counts for 25%";
cout << endl << "and the final counts for 50%";
cout << endl;
}
// Modify function to write contents of entire array of structures to file
void readrecord(ifstream& inFile, record& stu1)
{
inFile >> stu1.quiz1 >> stu1.quiz2 >> stu1.midterm >> stu1.final;
}
void writerecord(ofstream& outFile, record stu1)
{
outFile << left << setw(13) << stu1.lastName << setw(13)
<< stu1.quiz1 << setw(13) << stu1.quiz2 << setw(13) << stu1.midterm << setw(13)
<< stu1.final << stu1.final_grade << stu1.total_grade;
}
void gradeAverage(record stu1)
{
stu1.quiz_average = ((stu1.quiz1/10.) + (stu1.quiz2/10.))/2.*.25;
stu1.final_one = (stu1.final/100)*.5;
stu1.midterm_one = (stu1.midterm/100)*.25;
stu1.total_grade = (stu1.quiz_average + stu1.final_one + stu1.midterm_one)*100;
cout << endl << endl;
cout << "Quiz 1: " << (stu1.quiz1/10)*100 << "%";
cout << "Quiz 2: " << (stu1.quiz2/10)*100 << "%";
cout << endl;
cout << "Mid-Term Exam: " << stu1.midterm << "/100";
cout << endl;
cout << "Final Exam: " << stu1.final << "/100";
cout << endl;
cout << "Final Grade: " << stu1.total_grade << "%";
cout << endl;
cout << "Letter Grade: ";
if (stu1.total_grade >= 90)
cout << "A" << endl;
else if (stu1.total_grade >= 80)
cout << "B" << endl;
else if (stu1.total_grade >=70)
cout << "C" << endl;
else if (stu1.total_grade >=60)
cout << "D" << endl;
else
cout << "F" << endl;
}
I am trying to output this section to the writerecord function
stu1.quiz_average = ((stu1.quiz1/10.) + (stu1.quiz2/10.))/2.*.25;
stu1.final_one = (stu1.final/100)*.5;
stu1.midterm_one = (stu1.midterm/100)*.25;
stu1.total_grade = (stu1.quiz_average + stu1.final_one + stu1.midterm_one)*100;
cout << endl << endl;
cout << "Quiz 1: " << (stu1.quiz1/10)*100 << "%";
cout << "Quiz 2: " << (stu1.quiz2/10)*100 << "%";
cout << endl;
cout << "Mid-Term Exam: " << stu1.midterm << "/100";
cout << endl;
cout << "Final Exam: " << stu1.final << "/100";
cout << endl;
cout << "Final Grade: " << stu1.total_grade << "%";
cout << endl;
cout << "Letter Grade: ";
if (stu1.total_grade >= 90)
cout << "A" << endl;
else if (stu1.total_grade >= 80)
cout << "B" << endl;
else if (stu1.total_grade >=70)
cout << "C" << endl;
else if (stu1.total_grade >=60)
cout << "D" << endl;
else
cout << "F" << endl;
oh and these are my errors
1>c:\users\rbduc_000\dropbox\visual studio 2010\projects\inclass\inclass\inclass.cpp(112): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\rbduc_000\dropbox\visual studio 2010\projects\inclass\inclass\inclass.cpp(113): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>c:\users\rbduc_000\dropbox\visual studio 2010\projects\inclass\inclass\inclass.cpp(114): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data HELP ME PLEASE