how to pick a specific data in fstream

hi~~~~
i got a problem in the fstream~~~~~

the scenario is like this~~~~~
an user of a bank wishes to access his/her account~~~~~
he/she is required to input their account number~~~~~
and the program will show their account balance according to their account number. (the acc. number with its corresponding acc. balance is stored in a text file)

and i come up to this problem~~~~~
when the user input their acc. number
the program will not output their acc. balances

can someone tells me how to solve this problem~~~~~
how to let the program knows that it need to output the acc. balance according to the responding acc. number~~~~~~
Last edited on
you must organise your data file in a specific manner, like
acc:balances

then you need to read out the file line by line and compare the acc. number with the one that the user provided. if they are same, then you can do some string processing work and extract the desired balances.
.... can i know what is the command line for reading the acc. number line by line~~~~~~
and what is the command line to extract the desired the desired balances~~~~~~

=.=''' sorry~~~i'm new at this things~~~~~~~
for example:
your data file may like:

a:123
b:456
c:789
....

string useracc;
cin >> useracc; // get the user's acc

ifstream fin("in.txt"); // open the file
string line; // store each line of the file

while(getline(fin, str)) { // read a line
string::size_type pos; // record the ':' position
string acc = str.substr(0, pos = str.find(':')); // extract acc
string balance = str.substr(pos+1); // extract balance
if(acc == useracc) { // if they are equal
cout << balance; // report the balance
break; // and exit the loop
}
}

fin.close(); // close the opened file


hope this will help :-)
o~~~~i see~~~~~
i'll give it a try~~~~~~
thanks~~~~~~~~~
hi~~~~can i know what is the "str" in the " while(getline(fin, str)) "
sorry~~~~i get it what you mean already~~~~~
after trying to work out my source code~~~~~~~
i finally know what you mean already~~~~~~jimmyi~~~~~
thanks~~~~~~~~~

this is the sample source code i've written~~~~~
can someone comment this source code~~~~~~
see that is there anything that can be modify to let it look more simplier~~~~~


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
33
34
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    ifstream inData;
    inData.open("asd.txt");

    string accNum;
    string balance;
    size_t pos;

    cout << "input account number: ";
    cin >> accNum;

    string line;

    while( getline(inData, line) )
    {
        ifstream::pos_type size;
        string acc = line.substr(0, pos = line.find(':'));
        balance = line.substr(pos + 1);

        if (acc == accNum)
        {
            cout << balance;
            break;
        }
    }

    inData.close();
}
Topic archived. No new replies allowed.