Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- //Classes, Reading files and dynamic arrays
- using namespace std;
- class client{
- public:
- string name;
- double owes;
- //default barebones constructor
- client(){}
- // This shows the data of the client
- void show()
- {cout<<name<<" "<<owes<<endl;}
- };
- //Global variables
- vector<client> cl;
- //The input and output streams
- ofstream myout;
- ifstream myin;
- void createclients()
- {
- // We write the client data to a text file
- client current;
- myout.open("clientdata.txt");
- int choice = 1;
- while(!choice == 0)
- {
- cout << "Enter client data "<< endl;
- cin>> current.name>>current.owes;
- // write the data to the file
- myout<<current.name<<" "<<current.owes<<endl;
- cout << "more (enter 1 to continue 0 to stop)? "<< endl;
- cin >> choice;
- }
- myout.close();
- }
- void readclients()
- {
- //************************************
- myin.open("clientdata.txt");
- // Read the client data to the end of the file
- while(! myin.eof())
- {
- //Reading from the file into a local client variable
- client current;
- myin >> current.name>>current.owes;
- // pushing the data onto the vector
- cl.push_back(current);
- }
- }
- void showthevector()
- {
- // we process vector like an array
- // The vectors length is cl.size() . But remember it first index is zero
- for(int i = 0;i<cl.size();i++)
- cl[i].show();
- }
- int main(){
- // Make the client file
- createclients();
- // Read the client data into a vector
- readclients();
- // Skip two lines
- cout<<endl<<endl;
- // Print out the vector data
- showthevector();
- system("pause");
- }
Add Comment
Please, Sign In to add comment