C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 11th, 2003, 12:16 AM
magdaolsen magdaolsen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 8 magdaolsen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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]'

Reply With Quote
  #2  
Old April 11th, 2003, 12:23 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,401 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 8 h 41 m 14 sec
Reputation Power: 4080
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.

Reply With Quote
  #3  
Old April 11th, 2003, 12:30 AM
magdaolsen magdaolsen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 8 magdaolsen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #4  
Old April 11th, 2003, 09:59 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,249 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 17 h 27 m 51 sec
Reputation Power: 1985
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 10:03 AM.

Reply With Quote
  #5  
Old April 11th, 2003, 10:09 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,249 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 17 h 27 m 51 sec
Reputation Power: 1985
Quote:
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).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Printing to file error on 1 line of code only

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap