Guest User

Untitled

a guest
Jul 17th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. //Classes, Reading files and dynamic arrays
  5. using namespace std;
  6.  
  7. class client{
  8. public:
  9. string name;
  10. double owes;
  11. //default barebones constructor
  12. client(){}
  13.  
  14. // This shows the data of the client
  15. void show()
  16. {cout<<name<<" "<<owes<<endl;}
  17.  
  18.  
  19. };
  20. //Global variables
  21.  
  22. vector<client> cl;
  23.  
  24. //The input and output streams
  25. ofstream myout;
  26. ifstream myin;
  27.  
  28. void createclients()
  29. {
  30. // We write the client data to a text file
  31.  
  32.  
  33. client current;
  34.  
  35. myout.open("clientdata.txt");
  36.  
  37. int choice = 1;
  38. while(!choice == 0)
  39. {
  40. cout << "Enter client data "<< endl;
  41.  
  42. cin>> current.name>>current.owes;
  43. // write the data to the file
  44.  
  45. myout<<current.name<<" "<<current.owes<<endl;
  46.  
  47. cout << "more (enter 1 to continue 0 to stop)? "<< endl;
  48. cin >> choice;
  49.  
  50. }
  51. myout.close();
  52. }
  53. void readclients()
  54. {
  55. //************************************
  56.  
  57. myin.open("clientdata.txt");
  58. // Read the client data to the end of the file
  59. while(! myin.eof())
  60. {
  61. //Reading from the file into a local client variable
  62. client current;
  63. myin >> current.name>>current.owes;
  64.  
  65. // pushing the data onto the vector
  66.  
  67. cl.push_back(current);
  68.  
  69. }
  70.  
  71.  
  72.  
  73. }
  74.  
  75. void showthevector()
  76. {
  77. // we process vector like an array
  78. // The vectors length is cl.size() . But remember it first index is zero
  79. for(int i = 0;i<cl.size();i++)
  80. cl[i].show();
  81.  
  82.  
  83.  
  84.  
  85. }
  86.  
  87. int main(){
  88.  
  89. // Make the client file
  90. createclients();
  91. // Read the client data into a vector
  92. readclients();
  93. // Skip two lines
  94. cout<<endl<<endl;
  95. // Print out the vector data
  96. showthevector();
  97.  
  98. system("pause");
  99.  
  100.  
  101. }
Add Comment
Please, Sign In to add comment