473,799 Members | 3,092 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with c++ program

15 New Member
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 1402
yodadbl07
15 New Member
nevermind i figured it out...
Dec 12 '06 #2
yodadbl07
15 New Member
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
2758
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 storing products in
6
4356
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 result in any way. Who can help me, I thank you very very much. list.cpp(main program) //-------------------------------------------------------------------------- - #pragma hdrstop #pragma argsused
5
3014
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 homework for me.. I am merely asking for help, perhaps pointers as to where to begin.. I've never used access before.. I'm rather cluey when it comes to
7
3301
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 I believe if it's solved, the remaining stuff is easy... my full program until now is here: http://www.geocities.com/tom4_h4wk/progmail.zip the problem is the segmentation fault when main trys to run leficheiro.c.... the *.c2 files are the...
2
2124
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 my other includes then I get lots of errors like C2011: 'fd_set' : 'struct' type redefinition warning C4005: 'FD_CLR' : macro redefinition which I understand are due to the fact that windows.h is being included in another header file as well as...
2
2147
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 need help, could someone do, anything to help me i don't have much time, here are the instructions: Instructions: In this project, you are asked to develop your own spell-checker utility. We make suggestions to help get you started. You should...
6
1646
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 encountered a problem and needs to close. We are sorry for the inconvience. If you were in the middle of something, the information you were working on might be lost
1
1932
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 http://oldmill.uchicago.edu/~wilder/Code/random/. Firstly, I got the notorious "fatal error C1010: unexpected end of file while looking for precompiled header directive" error when I tried to compile 'randtest.c' provided by wilder.
1
1929
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 http://oldmill.uchicago.edu/~wilder/Code/random/. Firstly, I got the notorious "fatal error C1010: unexpected end of file while looking for precompiled header directive" error when I tried to compile 'randtest.c' provided by wilder.
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10473
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.