Help with C++ project
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Help with C++ project

  1. #1
    Join Date
    Oct 2010
    Posts
    36

    Help with C++ project

    Hey I have a project for school where I have to make a bank class program using classes. I have to make a base class named Account and then 3 derived classes, Checking, Savings, and Loan. The problem I have is this program is read from a file our teacher created, so in main I have a while loop going through the file and if it reads an s it makes a savings object, if it is l it makes a loan object, etc. This is working fine but this objects also take in an account number, so transactions can be processed later. Here is the code for main:
    Code:
    main()
    {
    
       ifstream in;
       char transactionType;
       int accountNumber;
       string accountName;
       double initialBalance;
       double theInterestRate;
       double theCheckFee;
       double theTransactionAmount;
       string theAccountType;
    
       in.open("hello.txt");
    
       if (in.fail())
          cout << "Error reading file";
    
       Account account[5];
    
       while (!in.eof())
       {
          in >> transactionType;
    
          if (transactionType == 's')
          {
             in >> accountNumber;
             in >> accountName;
             in >> initialBalance;
             in >> theInterestRate;
             Savings savingAccount(accountNumber, accountName, initialBalance,
                                   theInterestRate);
       cout << "new account..." << endl << accountName << " (savings)";
             cout << savingAccount;
    
             account[accountNumber] = savingAccount;
          }
    
          else if (transactionType == 'c')
          {
             in >> accountNumber;
             in >> accountName;
             in >> initialBalance;
             in >> theCheckFee;
             Checkings checkingAccount(accountNumber, accountName, initialBalance,
                                       theCheckFee);
             cout << "new account..." << endl << accountName << " (checking)";
             cout << checkingAccount;
    
             account[accountNumber] = checkingAccount;
          }
    
          else if (transactionType == 'l')
          {
             theAccountType = "loan";
             in >> accountNumber;
             in >> accountName;
             in >> initialBalance;
             in >> theInterestRate;
             initialBalance = -(initialBalance);
             Loan loanAccount(accountNumber, accountName, initialBalance,
                              theInterestRate, theAccountType);
             cout << "new account..." << endl << accountName << " (loan)";
             cout << loanAccount;
    
             account[accountNumber] = loanAccount;
          }
     else if (transactionType == 't')
          {
             in >> accountNumber;
             in >> theTransactionAmount;
    
             account[accountNumber].addTransaction(theTransactionAmount);
    
             cout << account[accountNumber];
          }
    
          else if (transactionType == 'u')
          {
             in >> accountNumber;
    
          }
    
          else
             cout << "Invalid Transaction Type";
       }
    So I save the loan, savings, or checking objects back into an account array so I can sort through the accounts in the transaction part of the function. But then I am changing the Account object and the loan, checkings, or savings, aren't even taken in at all. And since transactions are different depending on if it is a loan, savings, or checking, it doesn't work. So how would I make this work? Virtual functions? Or is there a way to point to my loan, savings, or checking account with an account number without actually changing the values. Idk. I am confused so I'm sure I'm not making any sense.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: Help with C++ project

    You're experiencing what is called object slicing. Since your array is of type Account, it isn't capable of storing the different account types. You need to store an array, or better a vector or Account pointers, then create the sub account objects using new, instead of using local variables inside your if statements.

    You'd have something like
    Code:
    Account* account[5];
    account[accountNumber] = new Savings;

  3. #3
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    Thanks that actually makes sense! Except when I set account[accountNumber] to new Savings how do I initialize Savings.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help with C++ project

    Either give Savings a constructor taking the necessary arguments, or else you can initially hold it in a local Savings* and then assign that into the account array once you're done setting it up.

    Don't forget that ever "new" must eventually have a corresponding "delete"!

  5. #5
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    Alright I'll try giving that a shot!

  6. #6
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    Alright so that whole deal worked. But I came across another problem. In my transaction if statement it says,
    Code:
    else if (transactionType == 't')
          {
             in >> accountNumber;
             in >> theTransactionAmount;
    
             account[accountNumber].addTransaction(theTransactionAmount);
    
             cout << account[accountNumber];
          }
    And when I try to compile it I get an error that says "error: request for member &#226;addTransaction&#226; in &#226;account[accountNumber]&#226;, which is of non-class type &#226;Account*&#226;"

    Why is this? Oh and addTransaction is of type Account.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help with C++ project

    With pointers, you use "->" rather than ".".

  8. #8
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    Alright Awesome that worked! Now just one more problem (hopefully).
    When I use cout in my transaction if statement, it outputs a bunch of hexadecimal numbers. Why does it do this? And I have the << operator overloaded and it works in my other if statements. It is
    Code:
    ostream& operator << (ostream &out, const Account &account1)
    {
       cout.setf(ios::fixed);
       cout.precision(2);
    
       out << ": balance is $" << account1.initialBalance << endl << endl;
    
       return out;
    }

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help with C++ project

    Well, for starters, you're calling setf and setprecision on cout, but actually operating on out---not necessarily the same stream. Fix that.

    I don't know why the output would look like hex though, from what you've shown.

  10. #10
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    Alright I have virtual functions that look like this:
    Code:
    double Account::addTransaction(double theTransactionAmount)
    {
       initialBalance += theTransactionAmount;
    
       return initialBalance;
    }
    
    double Loan::addTransaction(double theTransactionAmount)
    {
       initialBalance -= theTransactionAmount;
    
       return initialBalance;
    }
    
    double Checkings::addTransaction(double theTransactionAmount)
    {
       initialBalance += theTransactionAmount;
    
       return initialBalance;
    }
    
    double Savings::addTransaction(double theTransactionAmount)
    {
       initialBalance += theTransactionAmount;
    
       return initialBalance;
    }
    If that has anything to do with it. Thanks for helping by the way.

  11. #11
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    It works when I output a normal object but when I output the pointer it gets messed up. Is there a reason for this? Do I need to set the pointer object to something?

  12. #12
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    Alright so I think it's just that my overloaded extraction operator isn't working with the pointer object. Is there a way I can get it to work?

  13. #13
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Help with C++ project

    Quote Originally Posted by prestonprice57 View Post
    It works when I output a normal object but when I output the pointer it gets messed up.
    Of course, because you're doing as you stated -- you're outputting a pointer. A pointer is just an integral value.
    Do I need to set the pointer object to something?
    Pointers must always point to something before you use them.

    Dereference the pointer, and you get the object you want to output:
    Code:
    #include <iostream>
    
    int main()
    {
       int i = 10;
       int *pi = &i;  // pi is pointing to the i variable
       std::cout << *pi;
    }
    You see that 10 is printed.

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Oct 2010
    Posts
    36

    Re: Help with C++ project

    Alright that makes sense. But how do I dereference a pointer to an object? Cause right now I have,
    Code:
      account[accountNumber] = new Loan(accountNumber, accountName,
                                               initialBalance, theInterestRate);

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Help with C++ project

    All pointers are dereferenced the same way.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center