Guest User

Untitled

a guest
Apr 27th, 2018
59
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.  
  32.  
  33. //calling on the function
  34. openFile(fin);
  35. getline(fin,info);
  36.  
  37.  
  38. //formatting data
  39. for(int i=0;i<1;i++)
  40. {
  41. //Find Name
  42. if(info.find(',')>0)
  43. {
  44. int pos2=info.find(',');
  45. name=info.substr(pos1,pos2);
  46. info.erase(pos1,pos2+1);
  47. }
  48. //Find Address
  49. if(info.find(',')>0)
  50. {
  51. int pos2=info.find(',');
  52. address=info.substr(pos1,pos2);
  53. info.erase(pos1,pos2+1);
  54. }
  55. //Find City
  56. if(info.find(',')>0)
  57. {
  58. int pos2=info.find(',');
  59. city=info.substr(pos1,pos2);
  60. info.erase(pos1,pos2+1);
  61. }
  62. //Find State
  63. if(info.find(',')>0)
  64. {
  65. int pos2=info.find(',');
  66. state=info.substr(pos1,pos2);
  67. info.erase(pos1,pos2+1);
  68. }
  69. //Find Zip and validate
  70. if(info.find(',')>0)
  71. {
  72. int pos2=info.find(',');
  73. zip=info.substr(pos1,pos2);
  74. info.erase(pos1,pos2+1);
  75. }
  76. //Concatenate
  77. full= city + ", " + state + " " +zip;
  78.  
  79. //output
  80. cout <<name<<"\n"<<address<<"\n"<<full<<endl;
  81.  
  82. if((zip.length()==5)||(zip.length()==10))
  83. {
  84. cout << "Your zip code is valid."<<endl;
  85. }
  86. else
  87. cout << "Your zip code is invalid."<<endl;
  88.  
  89. cout<<"----------"<<endl;
  90. getline(fin,info);
  91. }
  92.  
  93.  
  94. system("pause");
  95. return 0;
  96. }
  97.  
  98.  
  99.  
  100. //Open File Function
  101. int openFile(ifstream &fin)
  102. {
  103.  
  104. do{
  105. string filename;
  106.  
  107. cout <<"Enter file name:";
  108. cin>>filename;
  109. fin.open(filename.c_str());
  110.  
  111. if(!fin.is_open())
  112. {
  113. cerr <<"Unable to open file"<<endl;
  114. fin.clear();
  115. }
  116.  
  117. }
  118. while(!fin.is_open());
  119.  
  120.  
  121. return 0;
  122. }
Add Comment
Please, Sign In to add comment