Guest User

Untitled

a guest
Apr 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. /* Justin Kirsche
  2. * 4002-210 Lab 1 Excercise 4
  3. */
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <iomanip>
  9. #include <algorithm>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. //prototype
  15. int openFile(ifstream &fin);
  16.  
  17.  
  18. //main
  19. int main ()
  20. {
  21. const int pos1=0;
  22. string filename;
  23. ifstream fin;
  24. string info;
  25. string name;
  26. string address;
  27. string city;
  28. string state;
  29. string zip;
  30. string full;
  31. int i=0;
  32.  
  33. //calling on the function
  34. openFile(fin);
  35. getline(fin,info);
  36.  
  37.  
  38. //formatting data
  39. while(i<info.length())
  40. {
  41.  
  42. //Find Name
  43. if(info.find(',')>0)
  44. {
  45. int pos2=info.find(',');
  46. name=info.substr(pos1,pos2);
  47. info.erase(pos1,pos2+1);
  48. }
  49. //Find Address
  50. if(info.find(',')>0)
  51. {
  52. int pos2=info.find(',');
  53. address=info.substr(pos1,pos2);
  54. info.erase(pos1,pos2+1);
  55. }
  56. //Find City
  57. if(info.find(',')>0)
  58. {
  59. int pos2=info.find(',');
  60. city=info.substr(pos1,pos2);
  61. info.erase(pos1,pos2+1);
  62. }
  63. //Find State
  64. if(info.find(',')>0)
  65. {
  66. int pos2=info.find(',');
  67. state=info.substr(pos1,pos2);
  68. info.erase(pos1,pos2+1);
  69. }
  70. //Find Zip and validate
  71. if(info.find(',')>0)
  72. {
  73. int pos2=info.find(',');
  74. zip=info.substr(pos1,pos2);
  75. info.erase(pos1,pos2+1);
  76. }
  77. //Concatenate
  78. full= city + ", " + state + " " +zip;
  79.  
  80. //output
  81. cout <<name<<"\n"<<address<<"\n"<<full<<endl;
  82.  
  83. if((zip.length()==5)||(zip.length()==10))
  84. {
  85. cout << "Your zip code is valid."<<endl;
  86. }
  87. else
  88. cout << "Your zip code is invalid."<<endl;
  89.  
  90. cout<<"----------"<<endl;
  91.  
  92. getline(fin,info);
  93. i++;
  94. }
  95.  
  96.  
  97. system("pause");
  98. return 0;
  99. }
  100.  
  101.  
  102.  
  103. //Open File Function
  104. int openFile(ifstream &fin)
  105. {
  106.  
  107. do{
  108. string filename;
  109. string info;
  110. cout <<"Enter file name:";
  111. cin>>filename;
  112. fin.open(filename.c_str());
  113.  
  114. if(!fin.is_open())
  115. {
  116. cerr <<"Unable to open file";
  117. }
  118. }
  119. while(!fin.is_open());
  120.  
  121.  
  122. return 0;
  123. }
Add Comment
Please, Sign In to add comment