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

Discussion in 'C++' started by tharun518, Feb 25, 2009.

  1. tharun518

    tharun518 New Member

    Joined:
    Feb 25, 2009
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    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.
    Code:
    #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();
    }
     
    Last edited by a moderator: Feb 25, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    Please indent your code. It's very difficult to follow if there's no indentation.
     
  3. tharun518

    tharun518 New Member

    Joined:
    Feb 25, 2009
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    I don't know to indent. Please correct the error. The progaram is to copy content of a file to another file.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    Here's some non-indented code:
    Code:
    int main()
    {
    printf("Hello\n");
    return 0;
    }
    
    And here's some indented code (I used spaces):
    Code:
    int main()
    {
        printf("Hello\n");
        return 0;
    }
    
    When you have multiple nested code blocks, indentation makes the code a lot more readable.
     
  5. tharun518

    tharun518 New Member

    Joined:
    Feb 25, 2009
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    I have tried to intend it.

    Thank you very much for taking time. 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 instructions you give on modern c++.

    I tried 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.


    Code:
     
    #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();
     }
    
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    Don't PM me to demand answers please.

    What input do you give, and what output do you get? Enter say 3 or 4 records and use copy and paste to paste the resulting screen exactly - showing both data entered and results. Paste into a code block to maintain formatting.

    I am a little concerned by "inf.read(( char* )this,sizeof(student))" and the write equivalent. If these functions are designed for text then the data should be converted to text, not just cast to char*. Apart from this the code seems to be OK so the input and output should help me see what's going wrong.
     
  7. tharun518

    tharun518 New Member

    Joined:
    Feb 25, 2009
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    At last my program to transfer content of one file to another. Here is the full program for further reference to other users.
    Code:
    #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();
    }
    
     
    shabbir likes this.
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    I really like this approach and some repu your way
     
  9. tharun518

    tharun518 New Member

    Joined:
    Feb 25, 2009
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    Thank you very much admin. I also had a forum like this. But now its down. I am after the exams.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    PM me the url if you want to.
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: Please correct the error in my program. When i run the program on turbo c++ 3, it

    *slaps forehead* Yes, that would cause the problem:
    Code:
    while (inf.read((char*)this, sizeof(student)))
       {
            inf.read(( char* )this,sizeof( student ) );
    
    Easy mistake to make!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice