#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    8
    Rep Power
    0

    Question Printing to file error on 1 line of code only


    I have a program which is supposed to write to a file. I have included <fstream> in my program before main function. I have an fout in my main function and in another function. I get the follwoing errors in the last line of code in the following function. Appreciate any help.
    PHP Code:
    #include <fstream>

    //Function
    void StudentInfo (double Exam1Percentdouble Exam2Percentdouble Exam3Percentdouble Exam4Percentdouble &FinalGrade)
    {

        
    //Declare variables
        
    double exam1exam2exam3exam4;                    //Exam records
        
    int StudentID;
        
    string StudentName;

        
    cout <<"\n\nPlease enter the student name: ";
        
    cin >> ws;
        
    getline (cinStudentName);
        
    cout <<"Please enter the ID number for " << StudentName <<":\t";
        
    cin >> StudentID;

        
    cout <<"\nPlease enter the grade for the 1st exam:\t";
        
    cin >> exam1;
        
        
    cout <<"Please enter the grade for the 2nd exam:\t";
        
    cin >> exam2;
        
        
    cout <<"Please enter the grade for the 3rd exam:\t";
        
    cin >> exam3;
        
        
    cout <<"Please enter the grade for the 4th exam:\t";
        
    cin >> exam4;
        
        
    FinalGrade = ((exam1*Exam1Percent) + (exam2*Exam2Percent) + (exam3*Exam3Percent) + (exam4*Exam4Percent));

        
    //Print Student Data
        
    fout << StudentID << "\t  " << StudentName << "\t\t\t" << FinalGrade;
            

    error C2065: 'fout' : undeclared identifier
    error C2297: '<<' : illegal, right operand has type 'char [4]'
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    That's because you haven't declared what fout is. You have to declare it of type fstream like this:
    Code:
    //Print Student Data
        fstream fout("filename.txt", ios_base::out | ios_base::trunc);
        fout << StudentID << "\t  " << StudentName << "\t\t\t" << FinalGrade;
        fout.close();
    Hope this helps.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    8
    Rep Power
    0
    I have opened the output file in my main function. Do I have to copy the statement to the other function as well?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    If you open fout in a function -- in any function, even in main() -- , then it is only known within that function. To make it known to a function that main calls, you need to pass it as an argument to that function.

    A visual aid that we were given for figuring out scope is to view the entire source file as a flat field. Something declared on that field is visible while you are on that field. Now represent a function as a hole in the ground and everything declared in that function is visible within that hole and everything on the field is visible within that function (the hole only comes up to knee height, so you can still see the field). Also visible to you is the presence of the other functions, but you cannot see what is inside their holes just as they cannot see what is inside your hole. You can toss messages about what you have to the other functions (ie, pass parameters in function calls), but that's all.

    Of course, that was for Pascal, wherein having functions within functions (holes within holes) complicates the picture a bit, but the analogy still held. Hope it helps.


    So another solution would be to declare fout in the file outside of the main function, on the flat field of the analogy I gave above. Then it would be known globally to ALL functions within that file.

    However, some academicians try to discourage the use of global variables. Some may even consider it almost as heretical as using goto . You will need to know what your instructor's attitude towards global variables is before you use that approach. I wouldn't want for you to get points taken off.
    Last edited by dwise1_aol; April 11th, 2003 at 11:03 AM.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Originally posted by magdaolsen
    I have opened the output file in my main function. Do I have to copy the statement to the other function as well?
    Eek, no! You open the output file just once. What you need to do is to make fout known to the function that you call (read my other response on this). If you use one to open a file, then the other one will know nothing about that.

    BTW, if you declare fout in main() and then also declare it in the function that you call from main, those are two different variables that have nothing to do with each other.

    In this case, you should only declare fout once, but make it known to all functions that will need it, either by passing it as a parameter or by declaring it globally (bearing in mind the caveats about what your instructor would think of your solution).

IMN logo majestic logo threadwatch logo seochat tools logo