How to read a from a txt.file

#include <cstdlib>
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>


using namespace std;


int main(int argc, char *argv[])

{
int menu;
int offset;
int Status = 0;
int record=0;
int dep;
int withd;
string AccountNo;
string line;
string AccountData [3];


cout << "\t\tFarmers Bank\n"
<< "\t\t************\n\n"
<< "\t\t1. Deposit\n"
<< "\t\t2. Withdrawal\n"
<< "\t\t3. Bank Statememt\n"
<< "\t\t4. Account Balance\n\n\n";

cout << "Please Choose Option: ";
cin >> menu; cout << endl;

if (menu == 1 || menu == 2 || menu == 3 || menu == 4)

{
cout << "Enter Account Number: ";cin >> AccountData[0];
cout <<endl <<endl;

if (menu != 3 && menu !=4)

{
cout << "Enter Date: ";cin >> AccountData[1];
cout << "Enter Amount: $";cin >> AccountData[2];
}


ifstream myfile ("accounts.txt");
if (myfile.is_open())
{
if (menu==3)

{
cout << "Account Number: " << AccountData[0] << "\n\n";
}
else if (menu==4)
{
AccountData[2] = dep - withd;

cout << "Account Number: " <<AccountData[0] <<"\n\n";
cout << "\nYour remaining balance is $" << AccountData[2] <<"\n\n";
}


while ( myfile.good() )
{
getline (myfile,line);

if ((offset = line.find(AccountData[0], 0)) != string::npos)
{

if (menu==3)
{
cout << line << "\n";
Status=0;
record=1;
}

Status=1;

}

}


myfile.close();

if (Status==1)
{
ofstream writefile;
writefile.open ("accounts.txt",ios::app);
if (writefile.is_open())
{

if (menu==1)


{
writefile << "\n" << AccountData[0] << " " << AccountData[1] << " " << AccountData[2] << " " << "dep";
cout << "\nDeposit Successfully Saved! \n";
}

else if (menu==2)
{
writefile << "\n" << AccountData[0] << " " << AccountData[1] << " " << AccountData[2] << " " << "withd";
cout << "\nWithdrawal Successfully Done! \n";
}

Status=0;
}
}

else
{
cout << "Account Number not found! \n";
string input;
cin>>input;
}

if (menu==3 && record==0)

{
cout << "Account Number not found! \n";
}


}

else cout << "Unable to Open File";
string a;
cin>>a;
}

}

*********************************************************
menu==4
My Account Balance does not appear!! $.....
can you help me out...
Use [code][/code] tags.
See: http://cplusplus.com/doc/tutorial/files/
If you need more information, you'll need to ask a more specific question.
56789765 21-may-2009 1400.00 dep
56789763 21-may-2009 1200.00 dep
56789766 21-may-2009 1200.00 dep
56789765 22-may-2009 18.00 dep
56789765 23-may-2009 17.00 withd
56789763 23-may-2009 32.00 dep
56789765 24-may-2009 452.00 withd
56789765 24-may-2009 121.00 dep
56789766 25-may-2009 16.00 withd
56789765 26-may-2009 122.00 withd
56789765 27-may-2009 125.00 dep
56789765 28-may-2009 42.00 with

above is the txt file where it should read from (account.txt)

To show the balance I need to add all deposits and subtract all of the withdrawals this is where I`m facing difficulties in...

the C++ programme above on the menu 4 the cout <<"your remaining balance is $" //this is where there was no output on the screen...NO AMOUNT on the remaining balance was shown..
Like Zhuge said: Use [code]Your code[/code]

The problem is this: AccountData[2] = dep - withd; dep and withd are numric. you need to covert the result to string
Topic archived. No new replies allowed.