//This program will read in a text file, //create an Account class to store the information read from the text file, //store them in a vector<Account>, //and prints out all of the objects in the vector //Test cases found in Test.txt #include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <vector> using namespace std; // Declare Class w/ methods and members class Account { public: // Default Constructor Account(); //Constructor with initial balance Account(string name, int number, double init_bal); //deposits money into account, returns new balance (mutator function) double deposit(double money); //withdraws money, returns new balance; (mutator function) double withdraw(double money); //returns the balance (accesor function) -- mark all accessor functions as const double get_balance()const; //returns string string toStr() const; private: // these data members will store information but can't be changed from the outside without using the accessor functions double balance; string account_name; int ss_number; }; // Giving the functions directions on what to do. Account::Account() // Default Constructor just initializes the Account { account_name = ""; balance = 0.00; ss_number = 0; } Account::Account(string name, int number, double init_bal) // Constructor that assigns values to the private data members { account_name = name; ss_number = number; balance = init_bal; } double Account::deposit(double money) // mutator function that will return the new balance after adding "money" to the data member { balance += money; return balance; } double Account::withdraw(double money) // mutator function that returns the new balance after subtracting "money" from data member { balance -= money; return balance; } double Account::get_balance() const // returns the balance { return balance; } string Account::toStr() const // Accessor function: returns the string that will be seen when printed out. { ostringstream out; //Pushes all the data that we need into a stringstream. out << "Name: " << account_name << "\tBalance: $" << fixed << setprecision(2) << balance; return out.str(); // returns the stringstream data. } // The following section will demonstrate the use of functions in creating a new account. /* int main () { string name = "John Doe"; // Initializing variables to be stored in the object double money = 2000.00; int num = 234556789; double to_deposited = 150.00; double to_withdraw = 200.00; Account john(name, num, money); //creating the object with these values cout << john.get_balance(); //prints out the current balance cout << john.deposit(to_deposited); //prints out the new balance after making a deposit cout << john.withdraw(to_withdraw); // prints out the new balance after making a withdrawl cout << john.toStr() << endl; // prints out the name and the balance as defined in our toStr function "Name: John Doe Balance: 1950.00"*/ //The following code will read in a file that has space-delimited values in this order: // First Name, Last Name, SS Number, Initial balance // // Stores all of these values as an Account pointer in a vector. // prints out all of the Names and balances of all the accounts in the "Bank" int main() { vector<Account> bank; // Creates the vector of Account pointers string location; ifstream in_file; //Creating a stream to read in the file cout << "Where should I read the data from?"; getline(cin, location); in_file.open(location); if (in_file.is_open()){ while (!in_file.eof()) { // if there's more stuff, read it in. string line; string fname; string lname; int ss_num; double in_bal; // grab a whole line getline(in_file, line); // make it into a stream istringstream inputstream(line); // If we succeed in reading the input make an account if (inputstream >> fname >> lname >> ss_num >> in_bal) { string name = fname + " " + lname; // Making the name be first + last names Account tempAccount = Account(name, ss_num, in_bal); // allocating memory and creating the acount bank.push_back(tempAccount); // Adding to the vector; // or just bank.push_back(Account(name, ss_num, in_bal)); } else { cout << "Ignoring bad input in input file: " << line << endl; } } } // Now we print out the vector when the whole file is read in. for (auto x : bank) { cout << x.toStr() << endl; } system("pause"); return 0; }
Also, I used this data for input:
Fred Flintstone 1 234.01 Willma Flintstone 2 345.67 Bam Barney Rubble three 123.45 Betty Rubble 4 nothing Pebbles Flintstone 5 23.45
ALSO here is an advanced version with a friend for streams, USE WITH CAUTION
//This program will read in a text file, //create an Account class to store the information read from the text file, //store them in a vector<Account>, //and prints out all of the objects in the vector //Test cases found in Test.txt #include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <vector> using namespace std; // Declare Class w/ methods and members class Account { public: // Default Constructor Account(); //Constructor with initial balance Account(string name, int number, double init_bal); //deposits money into account, returns new balance (mutator function) double deposit(double money); //withdraws money, returns new balance; (mutator function) double withdraw(double money); //returns the balance (accesor function) -- mark all accessor functions as const double get_balance()const; //returns string string toStr() const; //ADVANCE TOPIC USE WITH CAUTION!! friend ostream& operator<<(ostream& os, const Account& dt); private: // these data members will store information but can't be changed from the outside without using the accessor functions double balance; string account_name; int ss_number; }; // Giving the functions directions on what to do. Account::Account() // Default Constructor just initializes the Account { account_name = ""; balance = 0.00; ss_number = 0; } Account::Account(string name, int number, double init_bal) // Constructor that assigns values to the private data members { account_name = name; ss_number = number; balance = init_bal; } double Account::deposit(double money) // mutator function that will return the new balance after adding "money" to the data member { balance += money; return balance; } double Account::withdraw(double money) // mutator function that returns the new balance after subtracting "money" from data member { balance -= money; return balance; } double Account::get_balance() const // returns the balance { return balance; } string Account::toStr() const // Accessor function: returns the string that will be seen when printed out. { ostringstream out; //Pushes all the data that we need into a stringstream. out << "Name: " << account_name << "\tBalance: $" << fixed << setprecision(2) << balance; return out.str(); // returns the stringstream data. } ostream& operator<<(ostream& os, const Account& anaccount) { os << anaccount.toStr(); return os; } // The following section will demonstrate the use of functions in creating a new account. /* int main () { string name = "John Doe"; // Initializing variables to be stored in the object double money = 2000.00; int num = 234556789; double to_deposited = 150.00; double to_withdraw = 200.00; Account john(name, num, money); //creating the object with these values cout << john.get_balance(); //prints out the current balance cout << john.deposit(to_deposited); //prints out the new balance after making a deposit cout << john.withdraw(to_withdraw); // prints out the new balance after making a withdrawl cout << john.toStr() << endl; // prints out the name and the balance as defined in our toStr function "Name: John Doe Balance: 1950.00"*/ //The following code will read in a file that has space-delimited values in this order: // First Name, Last Name, SS Number, Initial balance // // Stores all of these values as an Account pointer in a vector. // prints out all of the Names and balances of all the accounts in the "Bank" int main() { vector<Account> bank; // Creates the vector of Account pointers string location; ifstream in_file; //Creating a stream to read in the file cout << "Where should I read the data from?"; getline(cin, location); in_file.open(location); if (in_file.is_open()){ while (!in_file.eof()) { // if there's more stuff, read it in. string line; string fname; string lname; int ss_num; double in_bal; // grab a whole line getline(in_file, line); // make it into a stream istringstream inputstream(line); // If we succeed in reading the input make an account if (inputstream >> fname >> lname >> ss_num >> in_bal) { string name = fname + " " + lname; // Making the name be first + last names Account tempAccount = Account(name, ss_num, in_bal); // allocating memory and creating the acount bank.push_back(tempAccount); // Adding to the vector; // or just bank.push_back(Account(name, ss_num, in_bal)); } else { cout << "Ignoring bad input in input file: " << line << endl; } } } // Now we print out the vector when the whole file is read in. for (auto x : bank) { //cout << x.toStr() << endl; // Advance version, use with caution: cout << x << endl; } system("pause"); return 0; }Back to top