Vectors

Can somebody please help me with my vector and sorting functions. the trim and program are working but i need to output my file in "report.txt" with sorted accounts. THEN … use std::sort to sort the std::vector on balance and output to the console (std::cout) the accounts in this order to see who our largest account holders are. Any help will be highly appreciated.

For this assignment, create an account class with the following members:
class account {
private:
std::string account_code;
std::string first_name;
std::string last_name;
double balance;
public:
. . .
};
This class should have a constructor that initializes ALL members in the initialization list with data
passed in as arguments to the constructor as well as accessors for all members.
2) Write an application that populates a std::vector<account>. The application will use a std::ifstream
opened on the file, “account.dat”, which will be supplied to you. If the file cannot be opened, throw an
exception.
3) The application will then read account information from the file in the following format
account_code: 10 characters
first_name: 15 characters
last_name: 25 characters
balance: 8 digits, decimal place, 2 digits

For each set of data read, the application should use that data to construct an account object and push_back that object onto the vector. Each set of data is separated by a newline, which must be
skipped between data sets. Trim spaces from the right of the first name and last name fields upon input using the trim_right function provided.
4) Use std::sort to sort the std::vector on account number.
5) The application will then create a std::ofstream object and output the vector of accounts to a file,
"report.txt". Use any format you wish, but make sure to include all members on the output. At the
bottom of the report, include the sum of all account balances.
6) THEN … use std::sort to sort the std::vector on balance and output to the console (std::cout) the
accounts in this order to see who our largest account holders are.

account.dat

1234567890Fred Murtz 00002000.01
9237405759Nolan Ryan 07237895.75
5285064395Debbie Schneiderman 00201537.31
5839659462Freidman Hochstrasser 00048392.42
9587498357Bayern Richard 00003457.12
7395659299Milhouse Van Houten 00000002.85
2956294386Julian Schwinger 00335821.93
9765324598Joanne Bryant 08459873.08
4385638273Richard Ames 00008468.74


#pragma once
#if !defined ACCOUNT_H
#define ACCOUNT_H
#include "account.h"
#include<iomanip>
#include<vector>


class account {

private:

std::string account_code;
std::string first_name;
std::string last_name;
double balance;

public:
account() : account_code(), first_name(), last_name(), balance() { }
account(const std::string& ac, const std::string& fn, const std::string&ln, double bal) :
account_code(ac),
last_name(fn),
first_name(ln),
balance(bal)

{}

void set_account(std::string account)
{
account_code = account;
}

void set_fname(std::string fname)
{
first_name = fname;
}


void set_lname(std::string lname)
{
last_name = lname;
}


void set_bal(double bal)
{
balance = bal;
}


std::string get_account()
{
return account_code;
}

std::string get_fname()
{
return first_name;
}


std::string get_lname()
{
return last_name;
}


double get_balance()
{
return balance;
}




void read()
{
std::string lin;
std::ifstream inpfile{ "Account.dat" };
std::vector<account>accou;


//if loop to handle the file not found exception
if (inpfile)

{
while (getline(inpfile, lin)) {
std::string lin1 = lin.substr(0, 10);
set_account(lin1);

std::string lin2 = lin.substr(10, 15);
set_fname(lin2);

std::string lin3 = lin.substr(25, 25);
set_lname(lin3);

std::string lin4 = lin.substr(50, 11);
balance = std::stod(lin4);
set_bal(balance);


std::cout << "\nAccount Number: " << get_balance() << '\n'; //}
std::cout << "Name: " << generic::trim(get_fname()) << " " << generic::trim(get_lname()) << '\n';
std::cout << "Balance: " << std::fixed << std::setw(8) << std::setprecision(2) << get_balance() << '\n';
}
}

else
{
std::cout << "No file found exception" << std::endl;
}
}



};


#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include"trim.h"
#include<stdexcept>
#include "account.h"
#include<algorithm>

//main method to test the class
int main() {
try {


std::vector <account> ac;
account accou;
accou.read();
ac.push_back(accou);

std::sort(ac.begin(), ac.end());

std::ofstream outfile{ "report.txt" };

for (std::vector<account>::iterator itr = ac.begin(); itr != ac.end(); ++itr)
//std::cout << *itr <<'\n';
//outfile << *itr << '\n';

std::cin.ignore();
return 0;
}
catch (std::exception const& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
return -1;
}
}




Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/algorithm/sort/?kw=sort
Topic archived. No new replies allowed.