| | |
Please correct the error in my program. When i run the program on turbo c++ 3,.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
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)
#include<fstream.h> #include<iostream.h> #include<conio.h> class student { int rollno; char name[20]; int tm; public: void input(); void transfer(); void output(); } obj; void student::input() { char ch='y'; ofstream outf; outf.open("mark.dat"); while(ch=='y') { cout<<"Enter rollno, name and mark"; cin>>rollno>>name>>tm; outf.write((char*)&obj,sizeof(obj)); cout<<"Wish to enter more(Y/N)?\n"; cin>>ch; } outf.close(); } void student::transfer() { ofstream outf; ifstream inf; inf.open("mark.dat"); outf.open("trans.dat"); while(inf) { inf.read((char*)&obj,sizeof(obj)); outf.write((char*)&obj,sizeof(obj)); } outf.close(); inf.close(); } void student:utput() { ifstream inf; inf.open("trans.dat"); while(inf) { inf.read((char*)&obj,sizeof(obj)); cout<<"\nRollno "<<rollno; cout<<"\nName "<<name; cout<<"\nTotal "<<tm; } inf.close(); } void main() { clrscr(); student obj; obj.input(); obj.transfer(); obj.output(); getch(); }
--
Index of mp3
Index of mp3
•
•
Join Date: Feb 2009
Posts: 76
Reputation:
Solved Threads: 9
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:
Meanwhile:
C++ Syntax (Toggle Plain Text)
#include<fstream.h> // modern compilers use #include <fstream> #include<iostream.h> // modern compilers use #include <iostream> using namespace std; // needed by modern c++ compilers, maybe not for Turbo //#include<conio.h> // not used in standard c++ class student { int rollno; char name[20]; int tm; public: void input(); void transfer(); void output(); }; // terminate the class definition here //obj; // *************don't instantiate an object in the class definition void student::input() { char ch='y'; ofstream outf; outf.open( "mark.dat" ); while ( ch=='y' ) { cout<<"Enter rollno, name and mark"; cin>>rollno>>name>>tm; outf.write(( char* )this,sizeof( student ) ); //******* use "this" pointer and sizeof(student) cout<<"Wish to enter more(Y/N)?\n"; cin>>ch; } outf.close(); } void student::transfer() { ofstream outf; ifstream inf; inf.open( "mark.dat" ); outf.open( "trans.dat" ); // **********when you do this you put an extra copy of the last student in outf // while ( inf ) // { // instead, read inside the loop control expression this way: while( inf.read(( char* )this,sizeof( student ) ) ) { // ********use "this" pointer and sizeof(student) outf.write(( char* )this,sizeof( student ) ); // *********use "this" pointer and sizeof(student) } outf.close(); inf.close(); } void student::output() { ifstream inf; inf.open( "trans.dat" ); // while ( inf ) //******* here you're printing yet another extra copy of the last student while (inf.read((char*)this, sizeof(student))) // ******* again, read inside loop control expression { // inf.read(( char* )this,sizeof( student ) ); cout<<"\nRollno "<<rollno; cout<<"\nName "<<name; cout<<"\nTotal "<<tm; } inf.close(); } int main() // ************ return type of main should be int { // clrscr(); student obj; obj.input(); obj.transfer(); obj.output(); getchar(); //******* I guess getch() is a Turbo C thing; I think not used in most modern compilers return 0; }
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
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.
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)
#include<fstream.h> #include<iostream.h> #include<conio.h> class student { int rollno; char name[20]; int tm; public: void input(); void transfer(); void output(); }; void student::input() { char ch='y'; ofstream outf; outf.open("mark.dat"); while(ch=='y') { cout<<"Enter rollno, name and mark\n"; cin>>rollno>>name>>tm; outf.write((char*)this,sizeof(student)); cout<<"Wish to enter more(Y/N)?\n"; cin>>ch; } outf.close(); } void student::transfer() { ofstream outf; ifstream inf; inf.open("mark.dat"); outf.open("trans.dat"); while( inf.read(( char* )this,sizeof(student))) { outf.write(( char* )this,sizeof( student ) ); } outf.close(); inf.close(); } void student::output() { ifstream inf; inf.open("trans.dat"); while (inf.read((char*)this, sizeof(student))) { inf.read(( char* )this,sizeof( student ) ); cout<<"\nRollno "<<rollno; cout<<"\tName "<<name; cout<<"\tTotal "<<tm<<'\n'; } inf.close(); } void main() { clrscr(); student obj; obj.input(); obj.transfer(); obj.output(); getch(); }
--
Index of mp3
Index of mp3
•
•
Join Date: Feb 2009
Posts: 76
Reputation:
Solved Threads: 9
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
before your program and
after it, so the indentation will be preserved.
C++ Syntax (Toggle Plain Text)
void student::output() { ifstream inf; inf.open("trans.dat"); while (inf.read((char*)this, sizeof(student))) { // the next line is consuming another input & discarding the one you just read //inf.read(( char* )this,sizeof( student ) ); *********delete this cout<<"\nRollno "<<rollno; cout<<"\tName "<<name; cout<<"\tTotal "<<tm<<'\n'; } inf.close(); }
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
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.
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)
#include<fstream.h> #include<iostream.h> #include<conio.h> class student { int rollno; char name[20]; int tm; public: void input(); void transfer(); void output(); }; void student::input() { char ch='y'; ofstream outf; outf.open("mark.dat"); while(ch=='y') { cout<<"Enter rollno, name and mark\n"; cin>>rollno>>name>>tm; outf.write((char*)this,sizeof(student)); cout<<"Wish to enter more(Y/N)?\n"; cin>>ch; } outf.close(); } void student::transfer() { ofstream outf; ifstream inf; inf.open("mark.dat"); outf.open("trans.dat"); while( inf.read(( char* )this,sizeof(student))) { outf.write(( char* )this,sizeof( student ) ); } outf.close(); inf.close(); } void student::output() { ifstream inf; inf.open("trans.dat"); while (inf.read((char*)this, sizeof(student))) { cout<<"\nRollno "<<rollno; cout<<"\tName "<<name; cout<<"\tTotal "<<tm<<'\n'; } inf.close(); } void main() { clrscr(); student obj; obj.input(); obj.transfer(); obj.output(); getch(); }
--
Index of mp3
Index of mp3
•
•
Join Date: Feb 2009
Posts: 76
Reputation:
Solved Threads: 9
Re: Please correct the error in my program. When i run the program on turbo c++ 3,.
0
#6 Feb 25th, 2009
![]() |
Other Threads in the C++ Forum
- Previous Thread: Updating the time from a thread
- Next Thread: Sorting using pieces of a struct
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





