473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help with c++ program

Hey i made this program that uses classes and files, and im having trouble with the readfile constructor...

here is the code
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <iomanip.h>
  3. #include <fstream.h>
  4. #include <string.h>
  5.  
  6. #define W setw
  7.  
  8. class student{
  9. private:
  10.     char first[20], last[20];
  11.     int age;
  12.     double gpa;
  13. public:
  14.     student();
  15.     void read();
  16.     void show();
  17.     void writefile();
  18.     int readfile(ifstream &);
  19.     double getGPA();
  20.  
  21. };
  22.  
  23. student::student() { gpa = 0;}
  24.  
  25. void student::read(){    
  26.         cout << "Please enter the students first and last name, age, and gpa: ";
  27.         cin >> first >> last >> age >> gpa;
  28. }
  29.  
  30. void student::show(){
  31.     cout << endl << W(10) << first << W(15) << last << W(3) << age << W(6) << gpa;
  32. }
  33.  
  34. void student::writefile(){
  35.     ofstream OS("studentdata.txt", ios::app);
  36.  
  37.     OS << W(10) << first << W(15) << last << W(3) << age << W(6) << gpa << endl;
  38.     OS.close();
  39. }
  40.  
  41. int student::readfile(ifstream & IS){
  42.     IS << first << last << age << gpa;
  43.     return !IS.eof();
  44. }
  45.  
  46. double student::getGPA() {return gpa;}
  47.  
  48. int main(void){
  49. student s;
  50. int i, n=0;
  51. double sum =0;
  52. char answer[1];
  53. ifstream IS("studentdata.txt");
  54.  
  55. while(true){
  56.     s.read();
  57.     s.writefile();
  58.     n++;
  59.     cout << "Do you want to enter another student (y/n)? ";
  60.         cin >> answer;
  61.  
  62.         if (answer[0] == 'n' || answer[0] == 'N'){
  63.             break;
  64.             }
  65. }
  66.  
  67. for(i=0; i<n; i++){
  68.     s.readfile(IS);
  69.     s.show();
  70.     sum += s.getGPA();
  71. }
  72.  
  73. cout << "\n The students average gpa is: " << sum/n;
  74. cout << endl;
  75. return 0;
  76. }
this is the error i get:
Expand|Select|Wrap|Line Numbers
  1. In method `int student::readfile(ifstream &)':
  2. 42: no match for `ifstream & << char[20]'
  3.  
Dec 12 '06 #1
2 1380
nevermind i figured it out...
Dec 12 '06 #2
sorry guys i thought i had it but now im getting a segment fault and i cant figure out what the problem is. here is the code plz help!!

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <iomanip.h>
  3. #include <fstream.h>
  4. #include <string.h>
  5.  
  6. #define W setw
  7.  
  8. class student{
  9. private:
  10.     char first[20], last[20];
  11.     int age;
  12.     double gpa;
  13. public:
  14.     student();
  15.     void read();
  16.     void show();
  17.     void writefile();
  18.     int readfile(ifstream &);
  19.     double getGPA();
  20.  
  21. };
  22.  
  23. student::student() { gpa = 0;}
  24.  
  25. void student::read(){    
  26.         cout << "Please enter the students first and last name, age, and gpa: ";
  27.         cin >> first >> last >> age >> gpa;
  28. }
  29.  
  30. void student::show(){
  31.     cout << endl << W(10) << first << W(15) << last << W(3) << age << W(6) << gpa;
  32. }
  33.  
  34. void student::writefile(){
  35.     ofstream OS("studentdata.txt", ios::app);
  36.  
  37.     OS << W(10) << first << W(15) << last << W(3) << age << W(6) << gpa << endl;
  38.     OS.close();
  39. }
  40.  
  41. int student::readfile(ifstream & IS){
  42.     IS >> first >> last >> age >> gpa;
  43.     return !IS.eof();
  44. }
  45.  
  46. double student::getGPA() {return gpa;}
  47.  
  48. int main(void){
  49. student s, r[20];
  50. int i, n=0;
  51. double sum =0;
  52. char answer[1];
  53. ifstream IS("studentdata.txt");
  54.  
  55. while(true){
  56.     s.read();
  57.     s.writefile();
  58.     cout << "Do you want to enter another student (y/n)? ";
  59.         cin >> answer;
  60.  
  61.         if (answer[0] == 'n' || answer[0] == 'N'){
  62.             break;
  63.             }
  64. }
  65.  
  66. while(r[i].readfile(IS))
  67.     n++;
  68. for(i=0; i<n; i++){
  69.     r[i].readfile(IS);
  70.     r[i].show();
  71.     sum += r[i].getGPA();
  72. }
  73.  
  74. cout << "\n The students average gpa is: " << sum/n;
  75. cout << endl;
  76. return 0;
  77. }
Thanks
Dec 12 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
2
by: Bsnpr8 | last post by:
I need help guys, i have to many stuff to do, because i am in my last 2 weeks of the university, my last assignment is to do a spell checker in C++, i have an idea but nothing is coming out. I really...
6
by: HelpME | last post by:
I wrote a program in Vb.Net that was running fine. However I am unable to install it on a couple of machines. When i run it I get a windows error message that says My Project.exe has...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.