how to write this code using file.please help me
i made this code but i am unable to do it using file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include<iostream>
#include<string>
using namespace std;
class Appointment
{
public:
Appointment();
void addapointment();
void display();
void setDescription(string);
private:
string name;
string adress;
string description;
};
Appointment::Appointment()
{
}
void Appointment::addapointment()
{
cout<<"enter the name adress and description"<<endl;
cin>>name;
cin>>adress;
cin>>description;
}
void Appointment::display()
{
cout<<"the name of the appointment is"<<' '<<name<<' '<<"adress"<<' '<<adress<<' '<<"discription"<<' '<<description<<endl;
}
int main()
{
ofstream myfile("example.txt")
Appointment p;
cout<<"enter the details of appointment"<<endl;
p.addapointment();
p.display();
}
|
Do you mean you want you want the output to go in a file?
If so, you can get the general idea from this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <fstream>
#include<iostream>
#include<string>
using namespace std;
class Appointment
{
public:
Appointment();
void addapointment();
void display(ostream& out_str);
void setDescription(const string&);
private:
string name;
string adress;
string description;
};
Appointment::Appointment()
{
}
void Appointment::addapointment()
{
cout<<"enter the name"<<endl;
getline(cin, name);
cout<<"enter the adress"<<endl;
getline(cin, adress);
cout<<"enter the description"<<endl;
getline(cin, description);
}
void Appointment::display(ostream& out_str)
{
out_str<<"the appointment details are:"<< endl
<<"name: "<<name<<endl
<< "adress: "<<adress<<endl
<<"description: "<<description<<endl;
}
int main()
{
ofstream myfile("example.txt");
Appointment p;
cout<<"enter the details of appointment"<<endl;
p.addapointment();
p.display(cout);
p.display(myfile);
myfile.close();
return 0;
}
|
Topic archived. No new replies allowed.