programmer dreamer
programmer dreamer

Reputation: 173

fstream 'outfile' does not name a type

i'm trying to make my outfile stream as global but ends up " 'outfile' does not name a type" error popping out. i've tried doing some google search but none relates with fstream..

my code:

    #include<iostream>
    #include<string>
    #include<iomanip>
    #include<fstream>
    #include<cstdlib>

    using namespace std;

    void determinepointer(string[],int,float[]);
    void printresults(string[],float[],int,char[][9],int[]);
    float calcGPA(float[],int[],int);

    ofstream outfile;
    outfile.open("StudentsTranscript.txt");//the problematic part

    int main()
    {
        ifstream infile;
        int coursenum;

        infile.open("StudentsFile.txt");

        infile.ignore(40,'\t');
        infile>>coursenum;

        char coursecode[coursenum][9];
        int credithr[coursenum];

        infile.ignore(13);

        for (int i =0;i<coursenum;i++)
        {
            infile>>coursecode[i];
            infile.ignore(1);

            if(coursecode[i][7]=='1')
                credithr[i]=1;
            else if(coursecode[i][7]=='2')
                credithr[i]=2;
            else if(coursecode[i][7]=='3')
                credithr[i]=3;
            else if(coursecode[i][7]=='4')
                credithr[i]=4;
            else
            {
                cout<<"invalid course code, please re-check & run this program again";
                exit(0);
            }
        }

        infile.ignore(10,'\t');

        outfile<<"STUDENT'S INDIVIDUAL TRANSCRIPT\n\n";

        while(!infile.eof())
        {
            char name[40];
            string grade[coursenum];
            float coursepointer[coursenum];
            infile.getline(name,40,'\t');
            outfile<<name<<endl;

            for (int i =0;i<coursenum;i++)
            {
                infile>>grade[i];
                infile.ignore(2);

            }

            determinepointer(grade,coursenum,coursepointer);
            printresults(grade,coursepointer,coursenum,coursecode,credithr);

        }

        infile.close();


        cout<<"done";

        return 0;

    }

**the coding is too long, full code's here: https://drive.google.com/file/d/0B_ir83gzFmIBQ0dXaGhPNXBJZkE/view?usp=sharing

Upvotes: 3

Views: 8960

Answers (2)

Budo Zindovic
Budo Zindovic

Reputation: 817

You can do like @oLen suggests or (if you require the outfile to be defied globally, you can do the following:

ofstream outfile("StudentsTranscript.txt");

Upvotes: 1

oLen
oLen

Reputation: 5547

Put the statement

outfile.open("StudentsTranscript.txt");//the problematic part

inside the main function. You can just declare the variable outside a function, not use it.

Upvotes: 1

Related Questions