1.11M Members

Output data to file

 
0
 

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

 
0
 

On line 112, try to put a (int) after the "=" . The program is expecting an int in this case. Try the same for your other errors when they want a float or a double. Just cast it as that type. It is trying to warn you that you will loose information when doing this. If your program dosen't have to match the graders input exactly, this shouldn't matter.

 
0
 

thanks for replying but I tried what you suggested but I am still getting the same errors

 
0
 

If you didnt get my message I got it to work. I added the int like you said and then I just put f next to the decimal points on both lines and the errors are gone now

 
0
 

I knew you could do it! Way to go!

 
0
 

THANK YOU but now do you know anything about my other problem trying to get that section to output to the file instead of the cmd screen

 
0
 

Are you getting any other errors? Have youu used fwrite()? Google how to write to a file. It looks like you have opened a file called outFile. Try using the man page for fwrite.

 
0
 

Usually it's done using the <fstream> header.

warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

This warning applies to line 112

stu1.quiz_average = ((stu1.quiz1/10.) + (stu1.quiz2/10.))/2.*.25;

Your quiz_average is declared as an int, but your answer is a double. Try converting your quiz_average to double in your struct record.

I am trying to output this section to the writerecord function

ofstream offers you the << operator, which means that everything you write in a cout, you can write it in an ofstream variable, in the same way. Just have a look at this class: Click Here

Have youu used fwrite()?

No need for a fwrite().

 
0
 

I solved my conversion problem. but I'm not understanding about ofstream. First I tried to call the gradeAverage function in the outFile. Is that possible or is that wrong

 
0
 

Change

void gradeAverage(record stu1)

to

void gradeAverage(record &stu1)

and try it again.

And also, what i've ment by the ofstream operator and cout is this:

void writerecord(ofstream& outFile, record stu1)
{
        outFile << endl << endl;
        outFile << "Quiz 1: " << (stu1.quiz1/10)*100 << "%";
        outFile << "Quiz 2: " << (stu1.quiz2/10)*100 << "%";
        outFile << endl;
        outFile << "Mid-Term Exam: " << stu1.midterm << "/100";
        outFile << endl;
        outFile << "Final Exam: " << stu1.final << "/100";
        outFile << endl;
        outFile << "Final Grade: " << stu1.total_grade << "%";
        outFile << endl;
        outFile << "Letter Grade: ";
        if (stu1.total_grade >= 90)
            outFile << "A" << endl;
        else if (stu1.total_grade >= 80)
            outFile << "B" << endl;
        else if (stu1.total_grade >=70)
            outFile << "C" << endl;
        else if (stu1.total_grade >=60)
            outFile << "D" << endl;
        else
            outFile << "F" << endl;
}

But first, you have to call the gradeAverage() function, that the writerecord one.

 
0
 

I've changed everything you said but now it says "no instance of overloaded function gradeAverage matches the argument list. I changed the prototype

 
0
 

Change it in the header also, if using one.

 
0
 

Post the exact contents of the gradesIn.txt and gradesOut.txt files.

 
0
 
gradesOut
Franks
Quiz 1: 100%
Quiz 2: 90%
Mid-Term: 96/100
Final Exam: 88.5/100
Final Grade: -9.25596e+061%
Letter Grade: F

Jones
Quiz 1: 80%
Quiz 2: 90%
Mid-Term: 88.5/100
Final Exam: 83/100
Final Grade: 92%
Letter Grade: A

Merrill
Quiz 1: 80%
Quiz 2: 70%
Mid-Term: 66/100
Final Exam: 69.5/100
Final Grade: 84.875%
Letter Grade: B

Smith
Quiz 1: 90%
Quiz 2: 100%
Mid-Term: 95.2/100
Final Exam: 93.6/100
Final Grade: 70%
Letter Grade: C

Wilson
Quiz 1: 70%
Quiz 2: 80%
Mid-Term: 76/100
Final Exam: 74.5/100
Final Grade: 94.35%
Letter Grade: A





gradesIn
Franks    10 9  96.0 88.5
Jones     8 9   88.5 83.0
Merrill   8 7   66.0 69.5
Smith     9 10  95.2 93.6
Wilson    7 8   76.0 74.5
 
0
 

I cant fix franks final grade score

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: