AllExperts > C++ 
Search      
C++
Volunteer
Answers to thousands of questions
 Home · More C++ Questions · Answer Library  · Encyclopedia ·
More C++ Answers
Question Library

Ask a question about C++
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Zlatko
Expertise
I can answer questions about C / C++ programming, software design, algorithms, and interprocess communication. I have access to Microsoft Visual Studio and gcc as my development platforms. I regret that I cannot answer questions about Turbo C/C++ graphics.

Experience
I have been developing software professionally in C and C++ for UNIX and Microsoft Windows since 1991.

Education/Credentials
I have a Bachelor of Applied Science in Computer Engineering from the University of Waterloo located in Waterloo, Ontario, Canada. I hold the IEEE Certified Software Development Professional designation and SUN Java Programmer and Java Developer credentials.

 
   

You are here:  Experts > Computing/Technology > C/C++ > C++ > c++

C++ - c++


Expert: Zlatko - 11/6/2009

Question
could you pls help mi,i realli realli need your help.i'm hving a veri hard time doing it.i'm trying to keeps track of customers’ portfolios, which include several bank accounts n investments.i'm nt sure whether i'm doing it correct anot,could u pls guide mi.i will be so appericated for all the help frm u.


The first file, customer.txt, has the following format:
customerID  firstName    lastName   address       phoneNum        Type
10001       Mickey    Mouse       Sengkang     91111111     Gold      ok
50020       Humpty    Dumpty       JurongWest     94444444     NORmal   ok
1001       Ben            Jerry       JurongEast     93333333     Normal   Error, ignore

The second file, account.txt, has the following format:
customerID accountNum   type     balance        numYears
10001      700001   SAVINGS    10000.00   2      ok
10001      70001   SAVINGS    10000.00   2      Error, ignore


The third file, investment.txt, has the following format:
customerID investNum    type    investValue     currValue
10001      3300001   Stock   10000.00   12000.00   ok
50020      33a00   Stock   10000.00   12000.00   Error, ignore


For the three input files above, all fields are not case-sensitive. If any entry has an invalid field, your program will ignore the entry entirely.
For bank accounts, the interest rates are derived based on following table:
CustomerType     AccountType  
                Savings
Normal           1.5%
Gold             Normal + 1.0%

Final balance is computed as original balance plus interest earned.
For investment accounts, the gain/loss is computed as the difference between current value and invested value.

output:

*** Overall Customer Statistics ***       
Number of customers type Normal: 1      
Number of customers type Gold: 1      
Total number of customers: 2      
     
     
*** Individual Customer Statistics ***       
*** Customer Particulars ***      
Customer ID: 10001      
Name: Mickey Mouse      
Address: Sengkang      
Phone: 91111111      
Customer Type: Gold


*** Bank accounts ***   
Number: 700001   
Type: Savings   
Original Balance: $10000.00   
Num of years: 2   
Interest earned: $500.00    
Final Balance: $10500.00   
  

*** Investment accounts ***   
Number: 3300001   
Type: Stock   
Invested value: $10000.00   
Current value: $12000.00   
Gain/ Loss: $2000.00   
  


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

using namespace std;

class Customer
{
 public:
   Customer () {}
   void setCustomer(int, string, string, string, int, int);
   void display();

 private:
   int customerID;
   string firstName;
  string lastName;
   string address;
  int phoneNum;
  int type;
};

void Customer::setCustomer(int id, string fn, string ln, string a, int pn, int ct)
{
    customerID = id;
   firstName = fn;
   lastName = ln;
    address = a;
   phoneNum = pn;
   type = ct;
}

void Customer::display()
{
   cout << customerID << '\t' << firstName << '\t' << lastName
       << '\t' << address << '\t' << phoneNum << '\t' << type << endl;
}

class Account
{
 public:
   Account () {}
   void setAccount(int, int, int, int);
   void display();

 private:
   int accountNum;
   int type;
  int balance;
  int numYears;
};

void Account::setAccount(int an, int tp, int bl, int ny)
{
    accountNum = an;
   type = tp;
   balance = bl;
    numYears = ny;
}

void Account::display()
{
   cout << accountNum << '\t' << type << '\t' << balance
       << '\t' <<numYears << endl;
}

class Investment
{
 public:
   Investment () {}
   void setInvestment(int, int, int, int);
   void display();

 private:
   int investNum;
  int type;
  int investValue;
  int currentValue;

};

void Investment::setInvestment(int in, int tp, int iv, int cv)
{
    investNum = in;
   type = tp;
   investValue = iv;
    currentValue = cv;
}

void Investment::display()
{
   cout << investNum << '\t' << type << '\t' << investValue
       << '\t' << currentValue << endl;
}

int main()
{
 char buffer[256];

 ifstream in ("customers.txt");

 if (!in.is_open())
 {
    cout << "Error opening file";
    exit (1);
 }

 while (!in.eof() )
 {
   in.getline (buffer,100);
   cout << buffer << endl;
 }
 
 system("pause");
 return 0;
}  

Answer
Hello Lisa.


You've made a good start defining the types of classes you need but you are having trouble reading from the file. I'm going to make some suggestions for the Customer class, then if you like my ideas, you can make similar changes to the other classes. You might also need to save your customers somewhere so you can count them before printing them out. Do you know how to use the standard template vector class? If not, I can help you with that, but the first thing to do is write code to read the files.


You will need these header files:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>


I suggest that each class has its own input function to read from its own file. Each class should know how its file is formatted. The main program should not have to worry about that.

For example, the Customer class could have a method like this:
bool read(ifstream& fin);
to read from fin. It reads one customer, and returns true if everything was correct, false otherwise.

The whole class looks like this:

class Customer
{
public:
   enum CustomerType
   {
       Normal = 1,
       Gold
   };

   Customer () {}
   void setCustomer(int, string, string, string, int, CustomerType);
   void display();
   bool read(ifstream& fin);

private:
   int customerID;
   string firstName;
   string lastName;
   string address;
   int phoneNum;
   CustomerType type;

   static const int ID_LENGTH = 5;
   static const int PHONE_LENGTH = 8;
};

I've made a few additions to your class. I've added constants for the correct ID length and phone length and I'm using an enumeration instead of a plain integer for storing the customer type.

My idea of the read method looks like this:

bool Customer::read(ifstream& fin)
{
   // First read individual strings from the file
   string idText;
   string firstNameText;
   string lastNameText;
   string addressText;
   string phoneText;
   string typeText;


   // 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 >> firstNameText >> lastNameText >> addressText >> phoneText >> typeText;


   // Now check that each input is valid

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

   // Check names
   if (firstNameText.length() == 0) return false;
   if (lastNameText.length() == 0) return false;

   // Check address
   if (addressText.length() == 0) return false;

   // Check phone
   if (phoneText.length() != PHONE_LENGTH || !allDigits(phoneText)) return false;

   // Check customer type
   CustomerType inputType;
   toLowerCase(typeText);
   if (strcmp(typeText.c_str(), "normal") == 0) inputType = Normal;
   else if (strcmp(typeText.c_str(), "gold") == 0) inputType = Gold;
   else return false;

   // Now assign all inputs to the customer
   setCustomer(atoi(idText.c_str()),
               firstNameText,
               lastNameText,
               addressText,
               atoi(phoneText.c_str()),
               inputType);
   return true;
}


Notice that I first read everything into temporary variables that end in "Text". Then I check each input for the correct length, and the correct format. If everything looks OK, then I call setCustomer.

Now you need two helper functions. The function allDigits(const string& input) returns true if the string is all digits, and false otherwise. It looks like this:

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

For strings that are not case sensitive, it is best to put them into lower case with this function:

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

The isdigit, and tolower functions are part of the standard C library.


In the main function you can open the customer file, and then have a customer object read the file like this:

int main()
{
   ifstream in ("customer.txt");

   if (!in.is_open())
   {
       cout << "Error opening file";
       exit (1);
   }

   Customer c;
   while (!in.eof())
   {
       if (c.read(in)) c.display();
   }
}


Good luck. Let me know how it goes.
Kind regards.
Zlatko


Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.