Why won't this piece of my code write to file

Posted by user2934783 on Stack Overflow See other posts from Stack Overflow or by user2934783
Published on 2013-10-31T03:49:41Z Indexed on 2013/10/31 3:53 UTC
Read the original article Hit count: 128

Filed under:
|
|
|
|

I am developing a c++ banking system. I am able to get the float, newbal, values correctly and when I try to write to file, there is no data in the file.

            else
            {
                file>>firstname>>lastname;
                cout<<endl<<firstname<<" "<<lastname<<endl; 
                cout<<"-----------------------------------\n";
                string line;
                    while (getline(file, line))
                    {

                        //stringstream the getline for line string in file
                        istringstream iss(line);

                        if (iss >> date >> amount)
                        {
                            cout<<date<<"\t\t$"<<showpoint<<fixed<<setprecision(2)<<amount<<endl;
                            famount+=amount;
                         }

                    }
                cout<<"Your balance is $"<<famount<<endl;   
                cout<<"How much would you like to deposit today: $";
                cin>>amountinput;

                float newbal=0;
                newbal=(famount+=amountinput);


                cout<<"\nYour new balance is: $"<<newbal<<".\n";
                file<<date<<"\t\t"<<newbal;  //***This should be writing to file
                                                but it doesn't.

                file.close();

The text file looks like this:

Tony Gaddis

05/24/12 100

05/30/12 300

07/01/12 -300

//Console Output looks like this

Tony Gaddis

05/24/12 100

05/30/12 300

07/01/12 -300

Your balance is: #1

How much wuld you like to deposit: #2

Your new balance is: #1 + #2

write to file

close file.

//exits to main loop:::: How can I make it write to file and save it, and why is this happening.

I tried doing it with ostringstream as well considering how I used istringstream for the input. But it didn't work either :\

float newbal=0;
                newbal=(famount+=amountinput);

                ostringstream oss(newbal);
                oss<<date<<"\t\t"<<newbal;

I am trying to self teach c++ so any relevant information would be kindly appreciated.

© Stack Overflow or respective owner

Related posts about c++

Related posts about string