New class:help constructor init. string before obj

Discussion in 'Programmer's Corner' started by Ruby, May 17, 2014.

  1. Ruby

    Thread Starter New Member

    Oct 29, 2009
    6
    0
    Hi. New to classes, old in most procedures. Classes just thrown the monkey wrench on the gears. My new project is 4 files: a header customer.h, loan.h, an object file client.cpp, and loan_Main.cpp.
    The loan class needs customer name and ID(Client::ID), so I try to access them through public functions with "LoanID = Client.getPin(); and CliName[35] = Client.getName();" The compiler error is: "assignment must be for an especific object"-not exactly that, but it is the meaning that I get. So I have tried creating a demo object, right at the Client class constructor, to initialize Name[35], and Pin-a 4 char array, with no success at all. Delved into every book I own, but all I'm still stocked on this. Surely missing some of the rules. It looks like it needs to have a Client object before Name could be called or referenced to. I'm using Visual Studio Pro, version 2010. Any ideas would be highly appreciated.:confused:

    Code ( (Unknown Language)):
    1.  
    2. class Loan
    3.     {
    4.     private:
    5.         double loanAmount;
    6.         int numberPayments;        
    7.         float annualInterest;
    8.     public:
    9.         int loan();//construct
    10.         double setLoanAmount;
    11.         int setnumberPayments;        
    12.         float setannualInterest;
    13.         double getLoanAmount();
    14.         int getnumberPayments();
    15.         float getannualInterest();
    16.         float CalcmonthlyInterest();
    17.         double CalcPayment(int, float, double);
    18.         double CalcamountoPay();
    19.         char getLoanID(){
    20.         char LoanID[4] = Client.getPin();}//get PIN from Client
    21.         char getcustomerName(){
    22.         char CliName[35] = Client.getName();}//get customer name from Client
    23.         Loan::~Loan();
    24. class Client
    25.     {
    26.     private:
    27.         char Name[NameSize];
    28.         char Pin[PinSize];
    29.         char address[AddresSize];
    30.         char Phone[PhoneSize];
    31.         double Payment;
    32.     public:
    33.         Client &DemoClient;
    34.         void DemoClient(){char Nam[NameSize] = {" "};//start constructor
    35.             for (int i = 0; i < NameSize; i++){
    36.                 Name[i] = Nam[i];}}
    37.  
    38.         char Client::setName(){
    39.             cout<<"\nEnter Customer name: ";
    40.             cin>>Name; cin.ignore();}
    41.         char Client::setPin(){
    42.             cout<<"\nEnter Personal Identification Number (PIN): ";
    43.             cin>>Pin;
    44.             int count = 0;
    45.             for(int i = 0; i < 4; i++){
    46.                 if(Pin[i] < 0 || Pin[i] > 9){
    47.                     count++;}
    48.                 while(count > 0){i = 0;
    49.                 cout<<"\nInvalid PIN. Enter a VALID PIN: ";
    50.                 cin>>Pin[4];}
    51.                 }
    52.             cout<<"PIN accepted ";}
    53.         char Client::setAddress(){
    54.             cout<<"\nEnter Customer address: ";
    55.             cin>>address; cin.ignore();}
    56.         char Client::setPhone(){cout<<"\nEnter home telephone number: ";
    57.         cin>>Phone;}
    58.         char Client::getName(){return Name[NameSize];}
    59.         char Client::getPin(){return Pin[PinSize];}
    60.         char Client::getAddress(){return address[50];}
    61.         char Client::getPhone(){return Phone[10];}
    62.        
    63.     }//end Customer class
    64. [/i][/i][/i][/i]
     
    Last edited by a moderator: May 17, 2014
  2. Ruby

    Thread Starter New Member

    Oct 29, 2009
    6
    0
    Sorry to disturb once again. There was an error on this function loop, which is used to validate user input for PIN. The loop before was using a while loop, after detecting a bad pin character. But the while loop had no way to reset, so it would loop forever... DON'T RUN, I MEAN DO NOT COMPILE IT! Here it is corrected, with an if conditional statement, which breaks at the end, returning to the for loop, with counter "i" reset to zero::D

    Code ( (Unknown Language)):
    1.    
    2.    char Client::setPin(){
    3.             cout<<"\nEnter Personal Identification Number (PIN): ";
    4.             cin>>Pin;
    5.             int count = 0;
    6.             for(int i = 0; i < 4; i++){
    7.                 if(Pin[i] < 0 || Pin[i] > 9){
    8.                     count++;}
    9.                 if(count > 0){i = 0;
    10.                 cout<<"\nInvalid PIN. Enter a VALID PIN: ";
    11.                 cin>>Pin[4];}
    12.                 }
    13.             cout<<"PIN accepted ";}
    14. [/i][/i]
     
    Last edited by a moderator: May 17, 2014
Loading...