How to view a specific lines of code from a text file

I asked a similar question like this before, but I had another idea in mind. Let's say I had a text file called "account.text" and it contained the following text:

//starting here
Your account number: 1

Your First Name: ftest

Your Last Name: ltest

Your current balance: $10


Your account number: 2

Your First Name: ftest2

Your Last Name: ltest2

Your current balance: $20
//ending here


Let's say I asked the user: "Enter your account number: ", and the user typed in "2". The expected output is:

Your account number: 2

Your First Name: ftest2

Your Last Name: ltest2

Your current balance: $20


How would I go about? This is the following function I have in mind:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void show_record()
{
	int userint;
	cout << "Enter you account number: ";
	cin >> userint;

	ifstream readacct; //needed to read the bank account file

	cout << "**Welcome to your bank account**" << endl;
	
	readacct.open("BankAccount.text"); //opens the bank account file


	readacct.close(); //closes the bank account file
}
Think about it. The number 1, the text "ftest", the text "ltest", and the value $10 all share one property; and the number 2, the text "ftest2", the text "ltest2", and the value $20 all share a different, similar property. So you need to write the code that can read in the text
1
2
3
4
5
6
7
Your account number: 1

Your First Name: ftest

Your Last Name: ltest

Your current balance: $10
and from this assemble a single object that contains the number 1, those two strings, and the value $10. For example, the class for that object might be
1
2
3
4
5
6
class Account{
    int number;
    std::string first_name;
    std::string last_name;
    double balance;
}
Once you have that, you need to read all the accounts in the file. And once you have that array of accounts loaded in memory, you can perform queries with it, such as get the balance for account 2.

PS: You might want to use a different format for your file. The way you've defined it you're making your problem much more difficult than it needs to be. I would start with something simpler:
1
2
3
4
5
6
7
8
1
ftest
ltest
10
2
ftest2
ltest2
20
Last edited on
Once you've opened your file and are ready to read from it -- as you did -- what I would do is:
_while you're not at the end of the file
____use the getline() function and look for the string "Your account number: " once your file stream is before the account number you can read the number and compare it to the user input int userint
____if both numbers are the same
________then read the rest of the account information from the file and display it.
My idea is: find the line that has a specific number and print that out along with the last three lines. For example, the user is prompted with "Enter your account number: ". The user enters "1", and the program looks for the line in the text containing "1". Consider the text file above where the first four lines of the text contained the following:
Your account number: 1
Your First Name: ftest
Your Last Name: ltest
Your current balance: $10
When the program searches for the line that contains a "1", print that line out, and print the next three lines after that. How do I do this? Rather than a class, I used a struct and this is what i have so far:
1
2
3
4
5
6
7
8
struct AccountDetails
{
	int accnum; //account number of user
	string fname; //first name of user
	string lname; //last name of user
	double balance; //balance amount of user
};
AccountDetails ofuser;

So what do I do now?
A struct in place of a class is fine. structs and classes are the same except for the default visibility of members.

helios has already given you suggestions about what you need to do next.

I would be careful about just looking for a match on "1". What if the balance is $1?

I strongly suggest you take helios's advice about the format of the file, or at least reduce the labels to a single word. This will simplify your parsing of the file.

1
2
3
4
5
6
7
8
account: 1
firstname: ftest
lastname: ltest
balance: 10
account: 2
firstname: ftest2
lastname: ltest2
balance: 20


This might help you get started:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
struct AccountDetails
{
    int accnum; //account number of user
    string fname; //first name of user
    string lname; //last name of user
    double balance; //balance amount of user
};

bool acct_read (ifstream & ifs, AccountDetails &acct)
{   std::string kw;

    ifs >> kw >> acct.accnum;
    ifs >> kw >> acct.fname;
    ifs >> kw >> acct.lname;
    ifs >> kw >> acct.balance;
    return ifs.good();
}

void acct_print (const AccountDetails &acct)
{
    cout << "Account: " << acct.accnum << endl;
    cout << "First name: " << acct.fname << endl;
    cout << "Last name: " << acct.lname << endl;
    cout << "Balance: " << acct.balance << endl;
)

void show_record()
{   int userint;
    std::string kw;
    AccountDetails ofuser;

    cout << "Enter you account number: ";
    cin >> userint;

    ifstream readacct; //needed to read the bank account file

    cout << "**Welcome to your bank account**" << endl;

    readacct.open("BankAccount.text"); //opens the bank account file

    while (acct_read (readacct, ofuser)) 
    {   //  Have a good record
        if (userint == ofuser.accnum)
        {
            acct_print(ofuser);
            break;
        }
    }

    readacct.close(); //closes the bank account file
}


Last edited on
This is what I have so far (I deleted some parts (where there are "...") just to not show my project as a whole):
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
struct AccountDetails
{
	int accnum; //account number of user
	string fname; //first name of user
	string lname; //last name of user
	double balance; //balance amount of user
};
AccountDetails ofuser;

void add_record()
{
	cout << "Enter an account number of your choice: ";
	cin >> ofuser.accnum;
	...

	cout << "\n";

	ofstream bnkacct("BankAccount.text", ios::app); //adds user info to the file

	if (!bnkacct) //if we couldn't open the output file stream for writing
	{
		//print an error and exit
		cerr << "BankAccount.text couldn't be opened for writing!" << endl;
		exit(1);
	}
	else
	{
		//add the user information when user opens banking account information
		bnkacct << "Your account number: " << ofuser.accnum;
		bnkacct << '\n';
]              ...
		bnkacct << "Your current balance: $" << ofuser.balance;
		bnkacct << '\n';
		bnkacct << '\n';
	}

	cout << "Thank you for registering a new bank account!" << endl;
}

void show_record()
{
	int userid;
	cout << "Enter your account number: ";
	cin >> userid;

	ifstream Fileshow; //used to read the bank account file

	Fileshow.open("BankAccount.text");
	if (!Fileshow)
	{
		cerr << "BankAccount.text could not be open for reading!" << endl;
		exit(1);
	}
	else
	{
		if(userid == ofuser.accnum)
		{
			
		}
	}
}


I am stuck on this part:
1
2
3
4
if(userid == ofuser.accnum)
{
			
}

If the userid the user typed in matches a line that contains that number, how would i implement the code?
Topic archived. No new replies allowed.