I have been getting used to c++ as I have been trying various scenarious on programs and exand on them with new concepts. This is my first try at exporting data from command line to a text file and the program runs fine with all the calculations. The real issue is that none of the outputs are exported to the text file. It is probably some simple oversight and want to say thanks in advance for helping me. Note- This program is supposed to take the inputs of a teacher and export the individual grade w/ letter the number of assignments and class average with letter to the text file.

#include <iostream>
#include <cmath>
#include <cfloat>
#include <fstream>

int i;
float testScore[100];
float sum = 0;
float clavg;
char grade;
int main()
{
    std::fstream myfile;
    myfile.open("grades.txt"); //name of file 
    std::cout<<"Please Enter Your Students Grades."<<std::endl;
    std::cout<<"Type 999 to get Avg"<<std::endl;
    for (i=0; i<100; i++)
    {
        std::cin>>testScore[i];
        if(testScore[i] == 999)
        {
            break;
        }
            if (testScore[i] >= 90 && testScore[i]<=100)
            {
                grade = 'A';

            }

            if (testScore[i] >= 80 && testScore[i]<=89)
            {
                grade = 'B';

            }

            if (testScore[i] >= 70 && testScore[i]<=79)
            {
                grade = 'C';

            }

            if (testScore[i] >= 60 && testScore[i]<=69)
            {
                grade = 'D';

            }

            if (testScore[i] >=0 && testScore[i]<=59)
            {
                grade = 'F';

            }

            sum+=testScore[i];
            myfile<<testScore[i]<<" "<<grade<<std::endl;
    }
    clavg = (sum/i);

    if (clavg >= 90 && clavg<=100)
            {
                grade = 'A';

            }

            if (clavg>= 80 && clavg<=89)
            {
                grade = 'B';

            }

            if (clavg >= 70 && clavg<=79)
            {
                grade = 'C';

            }

            if (clavg>= 60 && clavg<=69)
            {
                grade = 'D';

            }

            if (clavg >=0 && clavg<=59)
            {
                grade = 'F';

            }
            myfile<<"Total Number of Assignments = "<<i<<std::endl;
            myfile<<"Find Grade = "<<clavg<<" "<<grade<<std::endl;

            myfile.close();

            system("pause");
}

also would like to know if there is a command I can use to clean up all my if statements. Would I maybe be able to create these conditions in a seperate class and use them in a header?

add this after line 14 to see if your file is being opened.

if(!myfile)
    std::cout << "Unable to open file" << std::endl;

I did that, no error message appeared and still no text is being printed out =( i also tried removing the pause system to see if that was interfering but no luck

Edited 3 Years Ago by gobiking

in line 13 use ofstream instead of fstream. ofstream will allow you to write the file

you can also use the fstream command but when opening a file with fstream you will to also pass in an mode. for you example it will be

myfile.open("grades.txt",std::ios::out); //name of file

the ios::out will allow you to write the file.

you can look at this link for more info.
http://www.cplusplus.com/doc/tutorial/files/

Edited 3 Years Ago by k99rs

ok... biggest idiot moment ever..... forgot the file gets placed into my project folder.... was looking at my notepad file... sooo yeah.... gonna hit my head against the wall for 40min brb XD.

This question has already been answered. Start a new discussion instead.