CAN SOMEBODY PLEASE tell me whats wrong with my program Its

CAN SOMEBODY PLEASE tell me what\'s wrong with my program? It\'s working but I\'m confused with the sorting and output file. ANY COMMENTS WILL BE HIGHLY APPRECIATED. THANK YOU SO MUCH! p.s. no worries with the trim, it\'s working in my program(static library):

1) 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. 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. 7) Be sure to use proper exception handling at all stages.

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

//this is my account class

#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\" };      
      


       //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 << \"\ Account Number: \" << get_balance() << \'\ \';   //}
               std::cout << \"Name: \" << generic::trim(get_fname()) << \" \" << generic::trim(get_lname()) << \'\ \';
               std::cout << \"Balance: \" << std::fixed << std::setw(8) << std::setprecision(2) << get_balance() << \'\ \';
           }
       }

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

  
};

#endif

//main method to test the class

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


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 <<\'\ \';  
           outfile << *itr << \'\ \';

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

Solution

#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\" };      
      


       //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 << \"\ Account Number: \" << get_balance() << \'\ \';   //}
               std::cout << \"Name: \" << generic::trim(get_fname()) << \" \" << generic::trim(get_lname()) << \'\ \';
               std::cout << \"Balance: \" << std::fixed << std::setw(8) << std::setprecision(2) << get_balance() << \'\ \';
           }
       }

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

  
};

#endif

//main method to test the class

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


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 <<\'\ \';  
           outfile << *itr << \'\ \';

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

CAN SOMEBODY PLEASE tell me what\'s wrong with my program? It\'s working but I\'m confused with the sorting and output file. ANY COMMENTS WILL BE HIGHLY APPRECI
CAN SOMEBODY PLEASE tell me what\'s wrong with my program? It\'s working but I\'m confused with the sorting and output file. ANY COMMENTS WILL BE HIGHLY APPRECI
CAN SOMEBODY PLEASE tell me what\'s wrong with my program? It\'s working but I\'m confused with the sorting and output file. ANY COMMENTS WILL BE HIGHLY APPRECI
CAN SOMEBODY PLEASE tell me what\'s wrong with my program? It\'s working but I\'m confused with the sorting and output file. ANY COMMENTS WILL BE HIGHLY APPRECI
CAN SOMEBODY PLEASE tell me what\'s wrong with my program? It\'s working but I\'m confused with the sorting and output file. ANY COMMENTS WILL BE HIGHLY APPRECI
CAN SOMEBODY PLEASE tell me what\'s wrong with my program? It\'s working but I\'m confused with the sorting and output file. ANY COMMENTS WILL BE HIGHLY APPRECI

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site