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.


Ricky NguyenITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
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 ?
0
ambienceCommented:
Yes please post the rest of the code AND also outline file format (if that's already given).
0
Ricky NguyenITAuthor Commented:
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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Infinity08Commented:
>> 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 ?
0
Ricky NguyenITAuthor Commented:
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.
0
Infinity08Commented:
>> 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 ?
0
Ricky NguyenITAuthor Commented:
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.

0
Ricky NguyenITAuthor Commented:
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.
0
Infinity08Commented:
>> 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 ?
0
Ricky NguyenITAuthor Commented:

>>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.
0
Infinity08Commented:
>> The function to search the actual array for a specific number (accNumber) the user enters is beyond me at the moment. hehe.

That's not what I'm suggesting ;)

Keep the searching for later.

Just focus on reading one record (the next one) from the file now.

Once that is done, this function can be used to read records repeatedly, and do the search part.

But you first need your basic building blocks ;)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
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
0
sarabandeCommented:
the break; should be better error return when you do the above in Transaction::readRecord.

Sara
0
Infinity08Commented:
>> 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 ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.