A binary file JOKES.TXT is containing objects belonging to a class JOKE. A user defined function in C++ to add more objects belonging to class JOKE at the bottom of it



#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class joke{
                                             int joke_id;
                                             char type[5];
                                             char joke_desc[200];
                                             public:
                                             void newjoke()
                                                            {
                                                            cout<<"\nEnter joke id:";
                                                            cin>>joke_id;
                                                            cout<<"\nEnter joke type:(max 4 letters)";
                                                            gets(type);
                                                            cout<<"\nEnter the joke:\n";
                                                            gets(joke_desc);
                                                            }
                                             void showjoke()
                                                            {
                                                            cout<<"\nJoke id:"<<joke_id;
                                                            cout<<"\nJoke type:";
                                                            puts(type);
                                                            cout<<"\nJoke:\n";
                                                            puts(joke_desc);
            cout<<"HAHAHA";
                                                            }
                                             };
joke jo;
void add_joke()
{
               fstream file;
               file.open("joke",ios::binary|ios::app|ios::in|ios::out);
               int n,i;
               cout<<"\nHow many jokes?";
               cin>>n;
               for(i=0;i<n;i++)
               {              jo.newjoke();
                              file.write((char*)&jo,sizeof(jo));
               }
               file.close();
}
void main()
{
               clrscr();
               cout<<"HAHAHA";
               add_joke();

               jo.showjoke();
               getch();
}



OUTPUT:


HAHAHA
How many jokes?2                                                               
                                                                                
Enter joke id:1                                                                
                                                                               
Enter joke type:(max 4 letters)HUMR                                             
                                                                               
Enter the joke:                                                                
ABCDEFGHIJKLM
                                                                                
Enter joke id:2                                                                
                                                                               
Enter joke type:(max 4 letters)HUMR                                             
                                                                               
Enter the joke:                                                                
QWERTYUIO                                                                      
                                                                               
Joke id:2                                                                      
Joke type:HUMR                                                                 
                                                                                
Joke:                                                                          
QWERTYUIO                                                                      
HAHAHA       

No comments:

Post a Comment