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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MAX = 80;
const int MAXNO = 5;
enum Title {Miss, Mrs, Mr, Dr, Unknown};
struct Date
{
int day;
int month;
int year;
};
struct MyInfo
{
char name [MAX];
Title title;
char nationality [MAX];
Date dob;
int noOfHobbies;
char hobby [MAXNO] [MAX];
int noOfWishes;
char wish [MAXNO] [MAX];
};
void getMyInfo (fstream&, char[], MyInfo&);
string convertmonth(MyInfo&);
void displayMyInfo(MyInfo&);
int main()
{
srand(time (NULL));
fstream afile;
char filename [MAX];
cout << "Enter your info file name: ";
cin >> filename;
MyInfo a;
getMyInfo(afile,filename,a);
displayMyInfo(a);
}
void getMyInfo(fstream& afile, char filename [],MyInfo& a)
{
afile.open(filename, ios::in);
if (!afile)
{
cout << "file opened for reading failed" << endl;
exit (-1);
}
cout << endl;
cout << "Begin reading of " << filename << endl;
cout << "File name " << filename << " closed for reading" << endl;
cout << endl;
afile.getline(a.name, MAX);
afile.getline(a.nationality, MAX);
afile >> a.dob.day;
afile.ignore(3);
afile >> a.dob.month;
afile.ignore(3);
afile >> a.dob.year;
afile >> a.noOfHobbies;
afile.clear ();
afile.ignore (100, '\n');
int MAXNO=1;
while(MAXNO<=a.noOfHobbies)
{
afile.getline(a.hobby[MAXNO], MAX);
MAXNO++;
}
afile >> a.noOfWishes;
afile.clear ();
afile.ignore (100, '\n');
MAXNO=1;
while(MAXNO<=a.noOfWishes)
{
afile.getline(a.wish[MAXNO], MAX);
MAXNO++;
}
afile.close ();
}
string convertmonth(MyInfo& a){
switch (a.dob.month)
{
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
}
}
void displayMyInfo(MyInfo& a)
{
cout << "My Information" << endl;
cout << "Name: " << a.name << endl;
cout << "National: " << a.nationality << endl;
cout << "Date of birth: " << a.dob.day << " " << convertmonth(a) << ", " << a.dob.year << endl;
cout << "I have " << a.noOfHobbies << " hobbies" << endl;
for(int MAXNO=1;MAXNO<=a.noOfHobbies;MAXNO++)
{
cout << " " << MAXNO << ": " << a.hobby[MAXNO] << endl;
}
cout << "I have " << a.noOfWishes << " wishes" << endl;
for(int MAXNO=1;MAXNO<=a.noOfWishes;MAXNO++)
{
cout << " " << MAXNO << ": " << a.wish[MAXNO] << endl;
}
}
| |