7 Replies - 572 Views - Last Post: 11 February 2012 - 11:21 AM Rate Topic: -----

#1 krux18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-February 12

[Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 08:56 AM

Hello,

I'm a student working on a project for an assignment,I have searched everywhere on the net and on this site as well, but I couldn't find how to keep adding the stored value that is inside my text file to the new one. So, what I'm trying to say is that I have created a text file and when the program executes each time the value is stored in the text file.When I run the program again the stored value changes to the new value, but I don't want that instead I want to add the previous value to the new value until the program finishes. So, each time the program runs it will keep adding the two values together. I want the program to keep adding the sales of the adult tickets until the program finishes. I hope I made it clear what I'm stuck on. Thank you.

void SoldTickets ()
{
            TotalAdultTicketsSold = AdditonalAdultQty + AdultQty;
            ofstream myfile; 
            myfile.open ("Sold Tickets.txt");
            myfile << "Sold tickets for adult:"<< TotalAdultTicketsSold;
            myfile.close();
         
                  
            fstream file;
            file("Sold Tickets",ios::in);
            file << TotalAdultTicketsSold + TotalAdultTicketsSold;
            file.close();
            
            

         



}



Is This A Good Question/Topic? 0
  • +

Replies To: [Help] How to keep adding the stored data to a new value [Help]

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 5343
  • View blog
  • Posts: 16,675
  • Joined: 25-December 09

Re: [Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 09:16 AM

I think you may want to study the following link: C++ File input/output.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 krux18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-February 12

Re: [Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 09:21 AM

I have already gone through the tutorial, but I really can't figure out how to keep adding the old value with the old value.

Thanks

Kru
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 5,409
  • Joined: 27-December 05

Re: [Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 09:34 AM

Use an ifstream to open the file for input.

Read the value from the file into a variable, and close the ifstream.

Then do whatever you want to do to change the value of that variable.

Then use an ofstream to open the file for output (which will erase its contents at the same time).

Send your output to the file.

Close the ofstream.
Was This Post Helpful? 0
  • +
  • -

#5 krux18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-February 12

Re: [Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 10:30 AM

Hi,

This is what I did and it didn't work. Thanks

void SoldTickets ()
{
            TotalAdultTicketsSold = AdditonalAdultQty + AdultQty;
            ofstream myfile;
            myfile.open ("Sold Tickets.txt");
            myfile << "Sold tickets for adult:"<< TotalAdultTicketsSold;
            myfile.close();
         
            
                  
            fstream indata("Sold Tickets.txt",ios::in);
            
            float num; 
            
            indata >> num;
            TotalNo = num + TotalAdultTicketsSold;
            indata << TotalNo;
            
            indata.close();
            
            

         



}







Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 5343
  • View blog
  • Posts: 16,675
  • Joined: 25-December 09

Re: [Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 10:41 AM

Please show an example of what you are looking for.

           TotalAdultTicketsSold = AdditonalAdultQty + AdultQty;
            ofstream myfile;
            myfile.open ("Sold Tickets.txt");
            myfile << "Sold tickets for adult:"<< TotalAdultTicketsSold;
            myfile.close();

What is the value of TotalAdultTicketsSold in the above snippet? Do you understand that the above snippet erases the file contents when the file is opened?

            fstream indata("Sold Tickets.txt",ios::in);
            
            float num; 
            
            indata >> num;
            TotalNo = num + TotalAdultTicketsSold;
            indata << TotalNo;
            
            indata.close();


Based on the question above what do you think is the value of num? TotalNO?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 krux18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-February 12

Re: [Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 10:51 AM

Hello,

The value of num is AdditonalAdultQty + AdultQty (AdditonalAdultQty and AdultQty will be inputted from the user) Then those two values will be stored in the text file. When the program runs again and a different user will input how many tickets he would like then AdditonalAdultQty + AdultQty will be added together and that value will be added to the stored data in the text file. The purpose of this is to find out how many tickets are sold for the adult category for the day.

Thanks
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon

Reputation: 5343
  • View blog
  • Posts: 16,675
  • Joined: 25-December 09

Re: [Help] How to keep adding the stored data to a new value [Help]

Posted 11 February 2012 - 11:21 AM

I would suggest that you create two functions one function to read the file, and another function to write to the file.

To do what you want it appears that you will need to read the data that the file contains, add the values the user provides to value contained in the file, then write this new total to the file. I think you have your read and writes reversed.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1