Searching for, and printing values in C++ from a .txt file

So I'm doing a kind of banking system. Atm I've got the user able to create an account and set the balance of it, which is written to a .txt file. However, I would like the user to be able to enter their account no. and the program to cout the account info on the screen. I will be adding more to the program later, but until I work out how to search for values in a .txt file, and print out values on that line I'm a bit stuck.

Any help would be greatly appreciated.

Tom (BTW I'm still a bit of a noob)

Code -

Main.cpp -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "create_account.h"
#include <fstream>
using namespace std;


int menuin;


int main()
{

    create_account creataccObj;


   cout << "Welcome to the Bank" << endl;
   cout << "-------------------" << endl;
   cout << "Please choose from one of the following options: " << endl;
   cout << "1) Add Account" << endl;
   cin >> menuin;

        if (menuin == 1)
            creataccObj.createaccount();
        else
            cout << "Invalid Entry" << endl;
}


createaccount.h -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CREATE_ACCOUNT_H
#define CREATE_ACCOUNT_H


class create_account
{
    public:
        create_account();
        void createaccount();
        int accountno;
        double balance;
    protected:
    private:
};

#endif // CREATE_ACCOUNT_H 


createaccount.cpp -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include "create_account.h"
using namespace std;

create_account::create_account()
{

}

void create_account::createaccount()

{
    cout << "Please enter in the Number of the account you wish to create" << endl;
    cout << "Account numbers should be four digits, not begining with zero (0)" << endl;
    cin >> accountno;

    ofstream openaccount( "accounts.txt", ios::app );

    if(openaccount.is_open()) {
        cout << "Writing data..." << endl << "-------" << endl;
    }else{
        cout << "ERROR opening file." << endl;
    }

    openaccount << accountno << ' ';
    cout << "Enter in current balance" << endl;
    cin >> balance;
    openaccount << balance << ' ' << endl;
    openaccount.close();
    int main();
}


Sample .txt file -

1
2
3
4
5
6

// Account_no - Balance //

1001 140500 
1002 150600 
1003 18600 
Last edited on
Bump (Sorry)
To search a text file you want to open it and read values from it until you find the values you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool create_account::find_acount (int acctnum, double & bal)
{  int ifstream  acctt;
    int acct1;
    double bal1;

  accts.open ("accounts.txt", ios:in);
  if (! accts)
  {  cout << "Couldn't open accounts file" << endl;
      return false;
  }
  while (accts >> acct1 >> bal1)
  {  if (acctnum == acct1)
      {  bal = bal1;
          accts.close();
          return true;   // found account
      }
  }  
  accts.close();
  return false;  // account not found
}


Thank you!

Getting quite a few errors when I compile, could you just take a quick look to make sure I've implemented it right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void find_account(){

bool create_account::find_acount (int acctnum, double & bal)
 int ifstream  acctt;
    int acct1;
    double bal1;

  accts.open ("accounts.txt", ios::in);
  if (! accts)
    cout << "Couldn't open accounts file" << endl;
      return false;

  while (accts >> acct1 >> bal1)
    if (acctnum == acct1)
        bal = bal1;
          accts.close();
          return true;   // found account


  accts.close();
  return false;  // account not found


}


Header file -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CREATE_ACCOUNT_H
#define CREATE_ACCOUNT_H


class create_account
{
    public:
        create_account();
        void createaccount();
        int accountno;
        double balance;
        void find_account();
        int acctnum;
        double bal1;
    protected:
    private:
};

#endif // CREATE_ACCOUNT_H 
Please copy the errors and post them here.
Line 37 in errors = line 4 in post.

I am mainly concentrating on the first one, so once thats sorted I'll look at the others :)

|In function 'void find_account()':|
|37|error: expected initializer before 'int'|
|41|error: 'accts' was not declared in this scope|
|44|error: return-statement with a value, in function returning 'void' [-fpermissive]|
|47|error: 'acctnum' was not declared in this scope|
|48|error: 'bal' was not declared in this scope|
|50|error: return-statement with a value, in function returning 'void' [-fpermissive]|
54|error: return-statement with a value, in function returning 'void' [-fpermissive]|
||=== Build finished: 7 errors, 0 warnings (0 minutes, 0 seconds) ===|


Thank You!

Tom
Lines 1 & 2 in the first snippet are extraneous.

Line 3 does not match the declaration in the .h file (line 12).

Line 4 missing {

Line 4. accts is misspelled.

If statements at lines 9 and 14 missing {}.

Still getting same errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void find_account(){

bool create_account::find_account (int acctnum, double & bal)
int ifstream  accts;
    int acct1;
    double bal1;

  accts.open ("accounts.txt", ios::in);
  if (! accts){
    cout << "Couldn't open accounts file" << endl;
      return false;}

  while (accts >> acct1 >> bal1)
    if (acctnum == acct1){
        bal = bal1;
          accts.close();
          return true; }  // found account


  accts.close();
  return false;  // account not found




}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CREATE_ACCOUNT_H
#define CREATE_ACCOUNT_H


class create_account
{
    public:
        create_account();
        void createaccount();
        int accountno;
        double balance;
        void find_account();
        int acctnum;
        double bal1;
        double bal;
        int acct;
        int acct1;
    protected:
    private:
};

#endif // CREATE_ACCOUNT_H 



Any help would still be appreciated, getting a little frustrated at this point :P

Tom
Try fixing the errors I pointed out in my previous post.
Topic archived. No new replies allowed.