[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

files and classes

Consider the following code ....
#include <fstream.h>
#include <stdio.h>
#include <conio.h>

class word
{
     char wrd[50];
public:
     void inpu()
     {
          cout<<"\n Enter the word ";
          gets(wrd);
     }
     void out()
     {
          cout<<"\n Word "<<wrd;
     }
};
void output()
{
     ifstream f;
     f.open("word.dat");
     word w;
     f.read((char*)&w,sizeof(w));
     while(!f.eof())
     {
           w.out();
           f.read((char*)&w,sizeof(w));
     }
     f.close();
}
void input()
{
     ofstream f;
     f.open("word.dat",ios::app);
     word w;
     w.inpu();
     f.write((char*)&w,sizeof(w));
     f.close();

}
void main()
{
     int ch;
     cout<<"\n 1. input 2.output \n enter the code ";
     cin>>ch;
     switch(ch)
     {
           case 1:
                  input();
                  break;
           case 2:
                  output();
                  break;
     }
     getche();
}


i enter the word car, bus, train in a file....

but the ouput function shows the following:

word car
word us

please tell me what is the problem
0
jebinabraham
Asked:
jebinabraham
1 Solution
 
SCDMETACommented:
You seem to be using old c and C++.

Try using C++ includes:
  #include <fstream>
  #include <iostream>      
  using namespace std;
instead of
  #include <fstream.h>
  #include <stdio.h>
  #include <conio.h>

Use
  cin >> wrd;  
instead of  
  gets(wrd);

and get rid of
  getche();
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now