943,875 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 611
  • C++ RSS
Feb 24th, 2009
0

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

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Feb 25th, 2009
1

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

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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009
Feb 25th, 2009
0

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

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Feb 25th, 2009
0

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

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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009
Feb 25th, 2009
0

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

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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Feb 25th, 2009
0

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

Your welcome. Glad I could help.
Reputation Points: 23
Solved Threads: 18
Junior Poster
r.stiltskin is offline Offline
105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Updating the time from a thread
Next Thread in C++ Forum Timeline: Sorting using pieces of a struct





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC