Please correct the error in my program. When i run the program on turbo c++ 3,.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Please correct the error in my program. When i run the program on turbo c++ 3,.

 
0
  #1
Feb 24th, 2009
Please correct the error in my program. When i run the program on turbo c++ 3, it output 3 times. Also When i enter more than 1 entry only the last entry is outputted. Also it is outputted 3 times.

  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<conio.h>
  4. class student
  5. {
  6. int rollno;
  7. char name[20];
  8. int tm;
  9. public:
  10. void input();
  11. void transfer();
  12. void output();
  13. }
  14. obj;
  15. void student::input()
  16. {
  17. char ch='y';
  18. ofstream outf;
  19. outf.open("mark.dat");
  20. while(ch=='y')
  21. {
  22. cout<<"Enter rollno, name and mark";
  23. cin>>rollno>>name>>tm;
  24. outf.write((char*)&obj,sizeof(obj));
  25. cout<<"Wish to enter more(Y/N)?\n";
  26. cin>>ch;
  27. }
  28. outf.close();
  29. }
  30. void student::transfer()
  31. {
  32. ofstream outf;
  33. ifstream inf;
  34. inf.open("mark.dat");
  35. outf.open("trans.dat");
  36. while(inf)
  37. {
  38. inf.read((char*)&obj,sizeof(obj));
  39. outf.write((char*)&obj,sizeof(obj));
  40. }
  41. outf.close();
  42. inf.close();
  43. }
  44. void student:utput()
  45. {
  46. ifstream inf;
  47. inf.open("trans.dat");
  48. while(inf)
  49. {
  50. inf.read((char*)&obj,sizeof(obj));
  51. cout<<"\nRollno "<<rollno;
  52. cout<<"\nName "<<name;
  53. cout<<"\nTotal "<<tm;
  54. }
  55. inf.close();
  56. }
  57. void main()
  58. {
  59. clrscr();
  60. student obj;
  61. obj.input();
  62. obj.transfer();
  63. obj.output();
  64. getch();
  65. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 76
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training

Re: Please correct the error in my program. When i run the program on turbo c++ 3,.

 
1
  #2
Feb 25th, 2009
Try to get rid of that antique Turbo C++ 3 which was obsolete 10 years ago. You can get an excellent free IDE, Code::Blocks with mingw compiler (GNU C compiler for windows) and join the modern world.

Meanwhile:
  1. #include<fstream.h> // modern compilers use #include <fstream>
  2. #include<iostream.h> // modern compilers use #include <iostream>
  3. using namespace std; // needed by modern c++ compilers, maybe not for Turbo
  4. //#include<conio.h> // not used in standard c++
  5. class student
  6. {
  7. int rollno;
  8. char name[20];
  9. int tm;
  10. public:
  11. void input();
  12. void transfer();
  13. void output();
  14. }; // terminate the class definition here
  15. //obj; // *************don't instantiate an object in the class definition
  16. void student::input()
  17. {
  18. char ch='y';
  19. ofstream outf;
  20. outf.open( "mark.dat" );
  21. while ( ch=='y' )
  22. {
  23. cout<<"Enter rollno, name and mark";
  24. cin>>rollno>>name>>tm;
  25. outf.write(( char* )this,sizeof( student ) ); //******* use "this" pointer and sizeof(student)
  26. cout<<"Wish to enter more(Y/N)?\n";
  27. cin>>ch;
  28. }
  29. outf.close();
  30. }
  31. void student::transfer()
  32. {
  33. ofstream outf;
  34. ifstream inf;
  35. inf.open( "mark.dat" );
  36. outf.open( "trans.dat" );
  37. // **********when you do this you put an extra copy of the last student in outf
  38. // while ( inf )
  39. // {
  40. // instead, read inside the loop control expression this way:
  41. while( inf.read(( char* )this,sizeof( student ) ) ) { // ********use "this" pointer and sizeof(student)
  42. outf.write(( char* )this,sizeof( student ) ); // *********use "this" pointer and sizeof(student)
  43. }
  44. outf.close();
  45. inf.close();
  46. }
  47. void student::output()
  48. {
  49. ifstream inf;
  50. inf.open( "trans.dat" );
  51. // while ( inf ) //******* here you're printing yet another extra copy of the last student
  52. while (inf.read((char*)this, sizeof(student))) // ******* again, read inside loop control expression
  53. {
  54. // inf.read(( char* )this,sizeof( student ) );
  55. cout<<"\nRollno "<<rollno;
  56. cout<<"\nName "<<name;
  57. cout<<"\nTotal "<<tm;
  58. }
  59. inf.close();
  60. }
  61. int main() // ************ return type of main should be int
  62. {
  63. // clrscr();
  64. student obj;
  65. obj.input();
  66. obj.transfer();
  67. obj.output();
  68. getchar(); //******* I guess getch() is a Turbo C thing; I think not used in most modern compilers
  69. return 0;
  70. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Please correct the error in my program. When i run the program on turbo c++ 3,.

 
0
  #3
Feb 25th, 2009
Thank you very much for taking time to correct my program. I am from India and here we learn about only basics of c++ and our syllabus is based on turbo c++ 3. So i use turbo c++ and i don't know much about c++ programing. So i cannot implement all the instructions you given about modern c++.

I tried your new changes. But i still have some little problems. The program is to transfer content of one file to another. When i input only one entry, the program works. But when more than one entry is used it out puts only 1 or 2. Kindly please correct the problem.

Here is the new code by correcting the previous errors.

  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<conio.h>
  4. class student
  5. {
  6. int rollno;
  7. char name[20];
  8. int tm;
  9. public:
  10. void input();
  11. void transfer();
  12. void output();
  13. };
  14. void student::input()
  15. {
  16. char ch='y';
  17. ofstream outf;
  18. outf.open("mark.dat");
  19. while(ch=='y')
  20. {
  21. cout<<"Enter rollno, name and mark\n";
  22. cin>>rollno>>name>>tm;
  23. outf.write((char*)this,sizeof(student));
  24. cout<<"Wish to enter more(Y/N)?\n";
  25. cin>>ch;
  26. }
  27. outf.close();
  28. }
  29. void student::transfer()
  30. {
  31. ofstream outf;
  32. ifstream inf;
  33. inf.open("mark.dat");
  34. outf.open("trans.dat");
  35. while( inf.read(( char* )this,sizeof(student)))
  36. {
  37. outf.write(( char* )this,sizeof( student ) );
  38. }
  39. outf.close();
  40. inf.close();
  41. }
  42. void student::output()
  43. {
  44. ifstream inf;
  45. inf.open("trans.dat");
  46. while (inf.read((char*)this, sizeof(student)))
  47. {
  48. inf.read(( char* )this,sizeof( student ) );
  49. cout<<"\nRollno "<<rollno;
  50. cout<<"\tName "<<name;
  51. cout<<"\tTotal "<<tm<<'\n';
  52. }
  53. inf.close();
  54. }
  55. void main()
  56. {
  57. clrscr();
  58. student obj;
  59. obj.input();
  60. obj.transfer();
  61. obj.output();
  62. getch();
  63. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 76
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training

Re: Please correct the error in my program. When i run the program on turbo c++ 3,.

 
0
  #4
Feb 25th, 2009
You forgot to eliminate 1 line -- see below. Also, in future when you post code, type \[code\] before your program and \[/code\] after it, so the indentation will be preserved.
  1. void student::output()
  2. {
  3. ifstream inf;
  4. inf.open("trans.dat");
  5. while (inf.read((char*)this, sizeof(student)))
  6. {
  7. // the next line is consuming another input & discarding the one you just read
  8. //inf.read(( char* )this,sizeof( student ) ); *********delete this
  9. cout<<"\nRollno "<<rollno;
  10. cout<<"\tName "<<name;
  11. cout<<"\tTotal "<<tm<<'\n';
  12. }
  13. inf.close();
  14. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Please correct the error in my program. When i run the program on turbo c++ 3,.

 
0
  #5
Feb 25th, 2009
Thank you very much. That really worked.

At last my program to transfer content of one file to another. Again thanks. Here is the full program for further reference to other users.
  1. #include<fstream.h>
  2. #include<iostream.h>
  3. #include<conio.h>
  4. class student
  5. {
  6. int rollno;
  7. char name[20];
  8. int tm;
  9. public:
  10. void input();
  11. void transfer();
  12. void output();
  13. };
  14. void student::input()
  15. {
  16. char ch='y';
  17. ofstream outf;
  18. outf.open("mark.dat");
  19. while(ch=='y')
  20. {
  21. cout<<"Enter rollno, name and mark\n";
  22. cin>>rollno>>name>>tm;
  23. outf.write((char*)this,sizeof(student));
  24. cout<<"Wish to enter more(Y/N)?\n";
  25. cin>>ch;
  26. }
  27. outf.close();
  28. }
  29. void student::transfer()
  30. {
  31. ofstream outf;
  32. ifstream inf;
  33. inf.open("mark.dat");
  34. outf.open("trans.dat");
  35. while( inf.read(( char* )this,sizeof(student)))
  36. {
  37. outf.write(( char* )this,sizeof( student ) );
  38. }
  39. outf.close();
  40. inf.close();
  41. }
  42. void student::output()
  43. {
  44. ifstream inf;
  45. inf.open("trans.dat");
  46. while (inf.read((char*)this, sizeof(student)))
  47. {
  48. cout<<"\nRollno "<<rollno;
  49. cout<<"\tName "<<name;
  50. cout<<"\tTotal "<<tm<<'\n';
  51. }
  52. inf.close();
  53. }
  54. void main()
  55. {
  56. clrscr();
  57. student obj;
  58. obj.input();
  59. obj.transfer();
  60. obj.output();
  61. getch();
  62. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 76
Reputation: r.stiltskin is an unknown quantity at this point 
Solved Threads: 9
r.stiltskin r.stiltskin is offline Offline
Junior Poster in Training

Re: Please correct the error in my program. When i run the program on turbo c++ 3,.

 
0
  #6
Feb 25th, 2009
Your welcome. Glad I could help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 371 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC