fork download
  1. #include <iostream>
  2. //#include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <climits>
  6. using namespace std;
  7.  
  8. struct Person
  9. {
  10. string firstName;
  11. string lastName;
  12. string phoneNumber;
  13. };
  14. struct Address
  15. {
  16. string streetNum_Name;
  17. string city;
  18. string state;
  19. int zip_code;
  20. };
  21. struct Info
  22. {
  23. Person person;
  24. Address address;
  25. };
  26.  
  27. void printFileHeader(ostream& outFile);
  28. bool openInputFile(istream& inFile);
  29. void openOutputFile(ostream& outFile);
  30. bool readInfo(istream& inFile, Info&);
  31. void printInfo(ostream& outFile, const Info&);
  32.  
  33. int main()
  34. {
  35. //ifstream inFile;
  36. istream& inFile = cin;
  37. //ofstream outFile;
  38. ostream& outFile = cout;
  39. bool fileStreamState;
  40.  
  41. Info person;
  42.  
  43. fileStreamState = openInputFile(inFile);
  44.  
  45. openOutputFile(outFile);
  46. printFileHeader(outFile);
  47.  
  48. //cout << "Processing information. Please Wait...." << endl << endl;
  49.  
  50. while (readInfo(inFile, person)) {
  51. printInfo(outFile, person);
  52. }
  53.  
  54. outFile << setw(7) << "-----" << setw(20) << "---------" << setw(15) << "----------" << setw(20) << "----"
  55. << setw(12) << "------------" << endl;
  56.  
  57. cout << "Program has finished execution." << endl;
  58. //inFile.close();
  59. //outFile.close();
  60. }
  61.  
  62. void printFileHeader(ostream& outFile) {
  63.  
  64. outFile << left << setw(7) << "Entry" << setw(20) << "Last Name" << setw(15) << "First Name" << setw(20) << "City"
  65. << setw(12) << "Phone Number" << endl;
  66. outFile << setw(7) << "-----" << setw(20) << "---------" << setw(15) << "----------" << setw(20) << "----"
  67. << setw(12) << "------------" << endl;
  68. }
  69.  
  70. bool openInputFile(istream& inFile) {
  71.  
  72. /*
  73.   string filename;
  74.  
  75.   cout << "Enter the name of the input file: ";
  76.   cin >> filename;
  77.   cout << filename << endl << endl;
  78.  
  79.   inFile.open(filename.c_str());
  80.  
  81.   while(inFile.fail()) {
  82.  
  83.   cout << string(15,'*') << " File Open Error " << string(15,'*') << endl;
  84.   cout << "==> Input file failed to open properly!!\n";
  85.   cout << "==> Attempted to open file: " << filename << endl;
  86.   cout << "==> Try Again\n";
  87.   cout << string(47,'*') << endl<< endl;
  88.  
  89.   inFile.clear();
  90.  
  91.   cout << "Enter in the name of the input file: ";
  92.   cin >> filename;
  93.   cout << filename << endl << endl;
  94.   inFile.open(filename.c_str());
  95.   }
  96.   */
  97. return 1;
  98. }
  99.  
  100. void openOutputFile(ostream& outFile) {
  101.  
  102. /*
  103.   string filename;
  104.  
  105.   cout << "Enter the name of the output file: ";
  106.   cin >> filename;
  107.   cout << filename << endl << endl;
  108.  
  109.   outFile.open(filename.c_str());
  110.  
  111.   while(outFile.fail()) {
  112.  
  113.   cout << string(15,'*') << " File Open Error " << string(15,'*') << endl;
  114.   cout << "==> Output file failed to open properly!!\n";
  115.   cout << "==> Attempted to open file: " << filename << endl;
  116.   cout << "==> Try Again\n";
  117.   cout << string(47,'*') << endl<< endl;
  118.  
  119.   outFile.clear();
  120.  
  121.   cout << "Enter in the name of the input file: ";
  122.   cin >> filename;
  123.   cout << filename << endl << endl;
  124.   outFile.open(filename.c_str());
  125.   }
  126.   */
  127. }
  128.  
  129. bool readInfo(istream& inFile, Info& record) {
  130.  
  131. getline(inFile, record.person.phoneNumber);
  132. //cout << record.person.phoneNumber << endl;
  133.  
  134. getline(inFile, record.person.lastName);
  135. //cout << record.person.lastName << endl;
  136.  
  137. getline(inFile, record.address.streetNum_Name);
  138. //cout << record.address.streetNum_Name << endl;
  139.  
  140. getline(inFile, record.address.city);
  141. //cout << record.address.city << endl;
  142.  
  143. getline(inFile, record.address.state);
  144. //cout << record.address.state << endl;
  145.  
  146. inFile >> record.address.zip_code;
  147. inFile.ignore(INT_MAX, '\n');
  148. //cout << record.address.zip_code << endl;
  149.  
  150. getline(inFile, record.person.firstName);
  151. //cout << record.person.firstName << endl;
  152.  
  153. return !inFile.fail();
  154. }
  155.  
  156. void printInfo(ostream& outFile, const Info& record) {
  157.  
  158. static int entry = 0;
  159.  
  160. entry++;
  161.  
  162. outFile << left << setw(7) << entry << setw(20) << record.person.lastName << setw(15) << record.person.firstName
  163. << setw(20) << record.address.city << setw(12) << record.person.phoneNumber << endl;
  164. }
Success #stdin #stdout 0s 5512KB
stdin
000.000.0000
lastname
street
city
state 
00000
firstname
000.000.0000
lastname
street
city
state
00000
firstname
stdout
Entry  Last Name           First Name     City                Phone Number
-----  ---------           ----------     ----                ------------
1      lastname            firstname      city                000.000.0000
2      lastname            firstname      city                000.000.0000
-----  ---------           ----------     ----                ------------
Program has finished execution.