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

asked on

C++ coding class-based Problem #4

Hi experts.

Currently I'm having some troubles with understanding a portion of a c++ program, what I'm trying to make this program do is the following:

How would I search and edit a text file;

E.G.

User enters a number to be searched, for example an account number: 3243

the program searches within the text file for 3243 and brings back a number associated with 3243 so it will bring back:

3243
454.65.
D

I understand that arrays need to be implemented to make this work but I'm unsure of how to do it.

Here is my class code - I can post the rest if needed:

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[]);
          
};

Open in new window


Thanks in advance,

Rick.


Avatar of Infinity08
Infinity08
Flag of Belgium image

Which part of this is it that you have problems with ?

Is it the file operations ? Searching in the file ? Retrieving data from the file ?
Or is it the logic needed to get the needed information ?
Or is it how to store the data in memory ?
Or something else ?


Why don't you give it a try, to see how far you get, and then let us know where you get stuck ?
Yes please post the rest of the code AND also outline file format (if that's already given).
Avatar of Ricky Nguyen

ASKER

Thanks for the fast replies,

Here is all the code at this moment.

#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;
                                         myfile.open ("example.txt", ios::app | ios:: out );
                                         cout<<"Enter account Number:  "<< endl;
                                         cin >>accountNumber;
                                         myfile <<accountNumber << "\n";
                                                              
                                                                                                          
                                         cout<<"Enter Starting Balance:  "<< endl;
                                         cin >>amount;
                                         myfile <<amount << "\n";
                                                                                                          
                                         cout<<"Enter Account Type(C, D, I:  "<< endl;
                                         cin >>transType;
                                         myfile <<transType<< "\n";
                                          }
                                                                                                          
                                                                                                          
     
                                    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; 
                                             }
                                         }
                                                                                                               
                                   void createCust(string name, string address, int)
                                         {
                                         cout <<"Enther the Customers Name: " <<endl;
                                         cin >> name;
                                                                                                                                
                                         cout <<"Enter the Customers Address: "<<endl;
                                         cin >> address;
                                                                                                                                
                                         }    
                                                                                                                                                                                     
                                    void readRecord ()                       
                                         {
                                         string line;
                                         ifstream myfile ("trans.dat");
                                                  if (myfile.is_open())
                                                  {
                                                     while ( myfile.good() )
                                                     {
                                                     getline (myfile,line);
                                                     cout << line << endl;
                                                     }
                                                     myfile.close();
                                                  }
                                          else cout << "Unable to open file"; 
                                          }                                                                                                                                                                       
                                                                                                                                                                                        
     

int main()
{
    int menuchoice;
    menuchoice = 0;

  
 
 
 	while(menuchoice == 0)	//ends while loop for menuchoice when 5 is inputted
	{
		cout <<"====================================="<<endl
             << "	1. Create a New Account" << endl	//menu
			<< "	2. Display All Records" << endl
			<< "	3. Search for Specific Records" << endl
			<< "	4. Search for Customer Information" << endl
			<< "	5. Exit The Program" << endl
			<< "====================================="<<endl
			<< endl
			<< "	Enter your choice<1-5>:";
		cin >> menuchoice;	//menuchoice input
		cout << endl;
    
    		if(menuchoice < 1 || menuchoice > 5)	//input varification loop
		{
			cout << "ERROR:	Enter your choice between 1 and 5";	//error message
				
		}
				if(menuchoice == 1)	//runs when menuchoice = 1
				{
					cout << "Account Creation"<<"\n"; 	
					  int accountNumber;
                     float amount, transType;
					setUpaccount(accountNumber, amount, transType);
				}
				if(menuchoice == 2)	//runs when menuchoice = 2				
				{   
					cout << "Records"<<"\n";	
					readRecord();
				}
				if(menuchoice == 3)	//runs when menuchoice = 3				
				{
					cout << "Find a specific Record"<<"\n"; 	
				}
				if(menuchoice == 4)	//runs when menuchoice = 4
				{
					cout << "Find specific Customer Information"<<"\n"; 	
				}
				if(menuchoice == 5)	//runs when menuchoice = 5
				{
					cout << "..."; 	
					break;

				}
		menuchoice = 0;
    }
 
  system("pause");                                                       
  return 0;
}

Open in new window


Currently I'm a bit confused overall as to how to go about implementing this array.

Currently I'm trying to search in a specific data file(trans.dat) and retrieve a specific record and the way that is meant to be achieved is by searching for a customers accNumber.

Hope this helps clarify the questions.
>> Currently I'm a bit confused overall as to how to go about implementing this array.

I'm not sure what array you're referring to. But for what you described, you don't (necessarily) need an array.



>> Currently I'm trying to search in a specific data file(trans.dat) and retrieve a specific record and the way that is meant to be achieved is by searching for a customers accNumber.

That sounds like a good starting point.

You can read the file record by record, and check if it matches the search criteria (the account number). If so, you have found the right record, and can display it to the user. If not, you just check the next record.

Do you know how to read the file one record at a time ?
Sorry if I wasn't clear, I'm referring to the customer[] array which is located under the transaction class and I'm not all too sure as to how to actually read a file on a record by record basis but what you've described in your comment is pretty much what I'm aiming for in this program.

so for example: search -----> Find record -------> Display that record and the info associated with it on screen.

In a broader sense, the overall program would:

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

I hope this further clarifies the question.
>> Sorry if I wasn't clear, I'm referring to the customer[] array which is located under the transaction class

I'm not sure what you intend to use that for, but it's probably easier not to use it for searching in the file.


>> I'm not all too sure as to how to actually read a file on a record by record basis

Ok. Then first you (we) need to know what a record looks like in the file. Can you clarify that ?

Can you write a function that reads one record from the file, and returns a Customer object containing the information for that record ?
The record is in this format.

--- one record ---
246890   < --- number to be searched
250.00
C

--- another record ---
123456  <--- number to be searched
100.00
C

--- another record ---
123584 <--- number to be searched
0.00
I

Here is exactly what is in the file


246890
250.00
C
123456
100.00
C
123456
0.00
I
937568
1200.00
C
246890
150.00
D
123456
50.00
D
337761
50.60
C
123456
150.00
C
337761
75.00
C
337761
40.00
D
246890
0.00
I
937568
250.00
D
846579
1000.00
C
293567
500.00
C
917355
400.00
C
846579
100.00
D
293567
200.00
D
846579
100.00
D
917355
200.00
D
937568
0.0

I would like to specifically search out one of the records, by the top number of each record

Here is the array I'm using at the moment.

                    int i;                    
                    string customer[100];
                    string accInfo;
                    i = 0;
                    ifstream myfile("trans.dat");
                    while(myfile >> accInfo)
                    {
                    customer[i] = accInfo;
                    i++;
                    cout << accInfo;
                    } 

Open in new window


However the array comes back in a messy and unreadable display.

Hope this helps.

Update:

I managed to get the array to a readable format

                 
                    int i;                    
                    string customer[100];
                    string accInfo;
                    i = 0;
                    ifstream myfile("trans.dat");
                    while(myfile >> accInfo)
                    {
                    customer[i] = accInfo;
                    i++;
                    cout << accInfo << "\n";
                    }                    

Open in new window


Trying to implement some sort of search function now.
>> The record is in this format.

Ok, so each record consists of 3 lines.

Try writing a function that reads the next three lines from the file, parses the lines for the data, and creates a new Customer object with that data. Then have the function return that new Customer object.

Can you do that ?

>>Try writing a function that reads the next three lines from the file, parses the lines for the data, and creates a new Customer object with that data. Then have the function return that new Customer object.

>>Can you do that ?

The function to search the actual array for a specific number (accNumber) the user enters is beyond me at the moment. hehe.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium 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
the records are not customer data but transaction data: account number, amount and trans type.

so when reading in Transaction::readRecord you could directly read into the member variables of the Transaction for example

  if (!(myfile >> accountNumber))
     break;
 ...

Sara
the break; should be better error return when you do the above in Transaction::readRecord.

Sara
>> the records are not customer data but transaction data: account number, amount and trans type.

Right. Then simply replace Customer with Transaction in my previous posts ;)