hi all, i need help in extracting data out of a txt file and then modify it.

this is the problem

i have a txt file (customer.txt) storing some data

account no. money
123 3000
293 2300
874 3400

and this is the output i have to show

Customer Purchase:
Enter account number of customer: 123

Customer 123
Balance: $3000.00
Please enter sale charge: 200.00
<<<Sale approved:>>>
New Balance for customer 102: $2800.00

can anyone help me with this problem im stuck with

1) extracting the customer account number and balance
2) splitting the balance from the string (since account number and balance is in 1 line)
2) modify the txt file with the new balance

this is a little part of the whole program

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main (void) 
{
	string acct_no;
	cout<<"enter account: ";
	cin>>acct_no;

	ifstream myfile ("example.txt");
	if (myfile.is_open())
	{
	// stuck at here
    }
    myfile.close();
  }
}

it will be best if any sample source code or syntax is given so i can do the rest on my own. tyvm

Comments
Thanks for using code tags correctly

assume customer is a string and balance is an integer then just loop through the file until the customer is found. The loop will stop after all the file has been read.

while( myfile >> customer >> balance)
{
   // blabla
}

isit possible that i can find the 1st 3 digits of the acct_no and then by using seekg to cout the balance?

if so, how can i match the 1st 3 digits

>>how to declare customer as a string and balance as an integer
Doesn't matter because ifstream will convert what's in the file to an integer.

>>while( myfile >> acct_no>>balance)
You can NOT use acct_no in that line because ifstream will destroy the value you type from the keyboard. That's why you need to use a different variable as I suggested before. Then in that loop you have to compare acct_no to customer (or whatever variable name you want to use) to see if they are the same. If they are the same then perform the operations you stated in your original post.

ok thx it works for me now.
so the bottomline is dont use the same object name
and the working code is

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main (void) 
{
	string acc_no, customer, balance;
	cout<<"enter account: ";
	cin>>acc_no;

	ifstream myfile ("customer.txt");
	if (myfile.is_open())
	{
		while( myfile >>customer>>balance)
		{
			if(acc_no==customer)
			{
				cout<<customer<<endl;
				cout<<balance;
			}
		}
    }
    myfile.close();
}

thx once again, finally the problem i have being trying for umpten hours has been solved

ermm one more question

how do i convert the string 'balance' into a int/double which can do algorithms and vice versa?

when i try declaring 'balance' to an int, it doesnt cout anything when i run the program anymore.

what is the correct approach to this problem?

so is the same thing when u have input some int/double and u want to outfile it. what i have learn is by using strings and characters so i need help from you guys =)

it didnt work but thx anyway

sorry my code was wrong
try this

string A;
int B;
B=atoi(A.c_str());

ermm one more question

how do i convert the string 'balance' into a int/double which can do algorithms and vice versa?

Why are you using balance as a string anyway. It should be declared as an int and then ifstream will do all the work for you.

ok. one more question.

how do i modify a specific line?

for eg inside a text file contain

11 gg
22 bb
33 cc

i want to change '22 bb' to '22 aa'

how can i do that without losing the space?

imo, do i have to search for the 1st 2 letters '22' using ofstream and not ifstream then edit the whole line replacing '22 bb' with '22 aa' again?

if it is right, what is the syntax? if im wrong, please correct me. ty

This article has been dead for over six months. Start a new discussion instead.