google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: write a c++ program to read distance from user. store it in the file and display it to the user.

Saturday, February 21, 2015

write a c++ program to read distance from user. store it in the file and display it to the user.

Create a class  distance containing feet and inches as data member.
write a c++ program to read distance from user. store it in the file
 and display it to the user.
 use operator overloading for the following
 1.<<to write distance object in inches format to a file
 2.>>to read inches from file.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class distance
{
   int feet;
   int inches;

 public:
 friend void operator <<(fstream &,distance &);
 friend void operator >>(fstream &,distance &);

};
void operator <<(fstream &fout,distance &d)
{
cout<<"\nEnter distance : ";
cout<<"\n\nEnter feet : ";
cin>>d.feet;
cout<<"\n\nEnter inches : ";
cin>>d.inches;
int finches=(d.feet*12)+d.inches;
fout<<finches<<"\n";
}
void operator >>(fstream &fin,distance &d)
{
int finches;
fin>>finches;
d.inches=finches%12;
d.feet=finches/12;
cout<<"\nDistance is : ";
cout<<d.feet<<" feet ";
cout<<d.inches<<" inches ";
}
int main()
{
clrscr();
fstream file;
distance d;
int n;
file.open("distance.dat",ios::out);

file.seekg(0);
cout<<"\nEnter how many distance you want to store";
cin>>n;
for(int i=0;i<n;i++)
{
file<<d;
}
file.close();
file.open("distance.dat",ios::in);
file.seekg(0);
for (i=0;i<n;i++)
{
file>>d;
}
getch();
return 0;
}

--------------------------------------------------------------------------------------
Output :-
Enter how many distance you want to store1                                     
                                                                               
Enter distance :                                                               
                                                                               
Enter feet : 2                                                                 
                                                                               
                                                                               
Enter inches : 3                                                               
                                                                               
Distance is : 2 feet 3 inches 

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...