Link to home
Start Free TrialLog in
Avatar of Ricky Nguyen
Ricky NguyenFlag for Australia

asked on

C++ coding Class-based Problem #2

Hi Expert,

Here is problem #2 I'm trying to separate a big problem into numerous smaller problems so I can distribute more points.

Here is my overall Code. (not 100% complete)

#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>

using namespace std; 
  
class SavingsAccount
{
      private:
         int accNumber;
         float balance;
         float interestRate;  
         
      public:
         void setUpAccount( int, float, float );
         void addDeposit(float);
         void subWithdraw (float);
         void addInterest();
         float getBalance();
         bool matchAccount (int);
}; 

class Customer
{
      private:
         string name;
         string address;
         SavingsAccount saveAcnt;
         
      public:
         void createCust(string, string, int);
         bool seachAccounts (int);
         void applyTrans (char, float);
         void accessAccount (char);
};

class Transaction
{
      private: int accountNumber;
      private: float amount;
      private: char transType;
            
      public: void readRecord (ifstream);
      public: int findAccount (Customer[], int);
      public: void process (Customer[]);
          
};




                                float SavingsAccount:: getBalance()
                                    {
                                          return balance;
                                    }
                                                                      
                                                               void setUpaccount(int accountNumber, float amount, float transType)
                                                               {
                                                               ofstream myfile;
                                                               ofstream out("example.txt");
                                                               myfile.open ("example.txt", ios::in | ios::out);
                                                               cout<<"Enter account Number:  "<< endl;
                                                               cin >>accountNumber;
                                                                                                          
                                                               cout<<"Enter Starting Balance:  "<< endl;
                                                               cin >>amount;
                                                                                                          
                                                               cout<<"Enter Interest Rate:  "<< endl;
                                                               cin >>transType;
                                                               }
                                                                                                          
                                                                                                          
     
                                                                                  void adddeposit(float balance, float deposit)
                                                                                  {                                                                                 
                                                                                  cout <<"Enter an amount to deposit: "<< endl; 
                                                                                  cin >> deposit;
                                                                                  balance = balance+ deposit;
                                                                                  } 
     
                                                                                    void subWithdraw(float withdraw, float balance)
                                                                                    {
                                                                                    cout << "Enter an amount to withdraw $" << endl;
                                                                                    cin >> withdraw;
                                                                                    balance = balance - withdraw;
                                                                                    }     
                                                                                                                                                                        
                                                                                    void addinterest(float interestRate)
                                                                                    {
                                                                                    interestRate = 5.0;
                                                                                    }   
                                                                                                                                                                                    
                                                                                                               bool matchAccount(int a, int accNumber)
                                                                                                               {
                                                                                                               cout << "enter an account number";
                                                                                                               cin >> a;
                                                                                                               if (a==accNumber)
                                                                                                                  {
                                                                                                               cout<<"This account Number already exsists"<<endl; 
                                                                                                                 
                                                                                                                   }
                                                                                                               }
                                                                                                                                                                                     
                                                                                                                                                                                                  
                                                                                                                                                                                        
     

int main()
{
  setUpaccount(int accountNumber, float amount, float transType);
  system("pause");                                                       
  return 0;
}

Open in new window


Here is the code I am working on in this problem.

void setUpaccount(int accountNumber, float amount, float transType)
 {
 ofstream myfile;
 ofstream out("example.txt");
 myfile.open ("example.txt", ios::in | ios::out);
 cout<<"Enter account Number:  "<< endl;
 cin >>accountNumber;
                                                                                                          
 cout<<"Enter Starting Balance:  "<< endl;
 cin >>amount;
                                                                                                          
 cout<<"Enter Interest Rate:  "<< endl;
 cin >>transType;
    }

Open in new window


 
bool matchAccount(int a, int accNumber)
 {
 cout << "enter an account number";
  cin >> a;
            if (a==accNumber)
                {
                  cout<<"This account Number already exsists"<<endl; 
                 }
 }

Open in new window


What I'm trying to do is make the setUpaccount function write to a file without deleting the previous content of said file, when I input myfile instead of cin I get a whole heap of errors about myfile not working etc even though I have the fstream library in my code. (although I think my logic on how this works is wrong.)

Another thing I'm trying to do is make my boolean function check said file for duplicate accNumbers when the user types in whatever accNumber when setting up a new account. Depending on the results either allowing the user to input that account number or displaying an alert in the form of a cout and ending the creation of that account.

Thanks in Advance,

Rick.

Avatar of Brendan M
Brendan M
Flag of Australia image

can you post some errors?
Avatar of Ricky Nguyen

ASKER

Hey again brendanmeyer :),

Currently the bool function doesn't produce any errors but I know that it doesn't perform its task properly for example I'm pretty sure a cout statement alone won't prevent the accNumber from being placed into the notepad file I'm also wondering what sort of coding I would have to apply for the bool function to check the notepad in the first place.

As for my writing to file problem with the setUpaccount function currently I am running with.

  
void setUpaccount(int accountNumber, float amount, float transType)
 {
 ofstream myfile;
 ofstream out("example.txt");
 myfile.open ("example.txt", ios::in | ios::out);
 cout<<"Enter account Number:  "<< endl;
 cin >>accountNumber;
 myfile <<accountNumber;
                                                              
                                                                                                          
 cout<<"Enter Starting Balance:  "<< endl;
 cin >>amount;
  myfile <<amount;
                                                                                                          
   cout<<"Enter Interest Rate:  "<< endl;
   cin >>transType;
   myfile <<transType;
                                                              
}

Open in new window


For the code above I'm getting an expected primary-expression before "type" when I call the function.

int main()
{
  setUpaccount(int accountNumber, float amount, float transType);
  system("pause");                                                       
  return 0;
}

Open in new window


Hope this helps clarify my question.


ASKER CERTIFIED SOLUTION
Avatar of Brendan M
Brendan M
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

SWEET! :)

The code compiles now without a hitch and is put into the file that was specified although the numbers are compressed and don't have spacing at the moment, but how do I get my Boolean function to check the contents of a notepad file that already exists that contains the same data (accNumber, etc, etc) and then tell the user whether or not the accNumber they have entered in is valid.

If it helps the program in its complete form is meant to:

(class)Transactions ----(debit from or credit to) ---> (Class)Account -----Which belongs to---> (class)Customer

Those transactions are held in a notepad where they can be traced and displayed on screen by searching for an accNumber, hence why it is vital to not have duplicates.

Just figured out that I probably need to implement my array so that I can search that array for the data before the boolean takes effect. I will post this problem in another Question later :)

Thanks for the help.

More questions to come :)