C++/c++

Advertisement


Question
could u pls help mi.i will be so greatly appericated for all the help frm u.my problem is tat i do nt knw y the first 2 data is printed out.as for the other 2 data is correct,i dun knw y it did nt print out.

sample text file:
600031   Checkings    -20000.00   5//error
500020   Savings         20ab   3//error
900007   FixedDeposit  30000.00  4//correct
900010   FIXEDdeposit  5000.00   1//correct


//wrong should nt print out as the balance is "negative".
*** Bank accounts ***
Number: 600031
Type: 2
Original Balance: $-20000//error,ignore
Number of Years: 5
Checkings

//wrong should nt print out as the balance hv characters in it.
*** Bank accounts ***
Number: 500020
Type: 1
Original Balance: $20//error,ignore
Number of Years: 3
Savings

sample output:

code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;


class Account
{
public:
  enum AccountType
  {
      Savings = 1,
      Checkings,
      FixedDeposit

  };

  Account () {}
  void setAccount(int, int, AccountType, int, int);
  void display();
  bool read(ifstream& fin);


private:
  int customerID;
  int accountNum;
  int balance;
  int numYears;
  AccountType type;

  static const int ID_LENGTH = 5;
  static const int ACCOUNT_LENGTH = 6;

};

void Account::setAccount(int id, int an, AccountType at, int bl, int ny)
{
  customerID = id;
  accountNum = an;
  balance = bl;
  numYears = ny;
  type = at;
}

void Account::display()
{
  cout << endl;
  cout << "*** Bank accounts ***      " << endl;
  cout << "Number: " << accountNum << "  " << "
Type: " << type << "  "
       << "
Original Balance: " << "$" << balance << "  " << "
Number of Years: " << numYears << endl;
  
  switch(type)
 {
  case Savings:       cout << "Savings
";
         break;
  case Checkings:     cout << "Checkings
";
         break ;
  case FixedDeposit:  cout << "FixedDeposit
";
         break;
  default:          cout << "invalid value
";
 }   
}

bool allDigits(const string& input)
{
  for(size_t ix = 0; ix < input.size(); ++ix)
  {
      if (!isdigit(input[ix])) return false;
  }
  return true;
}

void toLowerCase(string& input)
{
  for(size_t ix = 0; ix < input.size(); ++ix) input[ix] = tolower(input[ix]);
}

bool Account::read(ifstream& fin)
{
  // First read individual data from the file
  string idText;
  string accountNumText;
  string typeText;
  string balanceText;
  string numYearsText;



  // Read the entire line in, then create a string stream from the line.
  // The string stream will break the line down into its pieces.
  string line;
  getline(fin, line);
  istringstream ss (line);
  ss >> idText >> accountNumText >> typeText >> balanceText >> numYearsText;


  // Now check that each input is valid

  // Check ID
  if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

  // Check AccountNum
  if (accountNumText.length() != ACCOUNT_LENGTH || !allDigits(accountNumText)) return false;

  // Check account type
  AccountType inputType;
  toLowerCase(typeText);
  if (typeText == "savings")inputType = Savings;
  else if (typeText == "checkings")inputType = Checkings;
  else if (typeText == "fixed deposit")inputType = FixedDeposit;
  else return false;
  
  // Check balance
  if (balanceText.length() == 0) return false;

  // Check years
  if (numYearsText.length() == 0) return false;

  // Now assign all inputs to the account
  setAccount(atoi(idText.c_str()),
      atoi(accountNumText.c_str()),
      inputType,
      atoi(balanceText.c_str()),
      atoi(numYearsText.c_str()));
  return true;

}


int main()
{
  ifstream accounts("account.txt");
  
    if (!accounts.is_open())
  {
      cout << "Error opening accounts file";
      exit (1);
  }
  
  Account a;
  while (!accounts.eof())
  {
      if (a.read(accounts)) a.display();
  }
  
  return 0;
}  

Answer
You need to validate that the string balanceText contains a valid positive number.

Write a function to check if a string is a literal number that is positive:

bool is_positive_double( const std::string& str )
{
   std::istringstream stm(str) ;
   double d = -1.0 ;

   // try to read a double from the stream
   if( stm >> d )
   {
       char cruff_remaining ;
       // if there are more chars left
       if( stm >> cruff_remaining ) return false ;

       else return d > 0.0 ; // no more chars, success if number read is positive
   }

   return false ; // read of double failed
}

Now use this function to validate the balance. instead of

 // Check balance
 if (balanceText.length() == 0) return false;

write

 // Check balance
 if ( !is_positive_double(balanceText) ) return false;

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.