Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

buffer or what ??

Hi experts,

I have a file

Sale Date: ,,,05052006
transaction, ,Sale Agent, Notes, Sell to, Sell Amt
sell, ,12345,store123,100.00


Here is what I do.

Step 1 - Take the sale date, I have no problem

if (row_elem[0].find("Sale Date") != string::npos)
{
            DateConvert(row_elem[3]);

            cout << row_elem[3] << endl; // here shows 05052006
                                                                        
}

step 2 - I need write to a out put file like this

if (row_elem[0] == "Sell")
{
      fout<<setw(2)            << setiosflags(ios::left) << "AM"
            <<setw(20)      << setiosflags(ios::left) << "12345 "
            <<setw(16)      << setiosflags(ios::left) << " "
            <<setw(8)       << setiosflags(ios::left) << row_elem[3} //sale date here
}

the right output file should be -
AM12345              05052006

But my output file is wrong like this
AM12345              store123//here does not show 05052006

How can I fix this, please help
Do I need buffer to memorize the date and write to the output file ? and How ?

Thanks
0
justinY
Asked:
justinY
1 Solution
 
PaulCaswellCommented:
Hi justinY,

Could you show us the code for DateConvert? I suspect it is freeing memory it shouldnt be.


Paul
0
 
rajeev_devinCommented:
Please do mention the datatype of  row_elem.

>> cout << row_elem[3] << endl; // here shows 05052006
Is this statement also printing the same value ?
Or, the correct value ?
0
 
rstaveleyCommented:
Presumably row_elem[3] is reinitialised with each row that you read from the file. Why not declare a variable outside your processing loop and call it sale_date? Initialise sale_date, when row_elem[0].find("Sale Date") != string::npos and output sale_date (along with the sell data), when row_elem[0] == "Sell".
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
justinYAuthor Commented:
Thanks guys,
1.  DateConvert code.

void DateConvert(string &date)
{
    int day, month, year;
    if ( sscanf ( date.c_str(), "%d/%d/%d", &month, &day, &year ) == 3 )
    {
        char newdate[32];
        sprintf(newdate, "%02d%02d%04d", month, day, year );
        date = newdate;
    }
};

2. Data type of Row_elem
 
vector<string> row_elem;

3. Basically, I want to grab the date 05052006 and write it to output file like this
if (row_elem[0] == "Sell")
{
     fout<<setw(2)          << setiosflags(ios::left) << "AM"
          <<setw(20)     << setiosflags(ios::left) << "12345 "
          <<setw(16)     << setiosflags(ios::left) << " "
          <<setw(8)      << setiosflags(ios::left) << row_elem[3]
}
so the right output file should be -
AM12345              05052006

but my problem is when I write to output file I have
AM12345              store123

Please help, thanks
0
 
rstaveleyCommented:
row_elem[3] is overwritten, when...

   sell, ,12345,store123,100.00

...is parsed.

You need to preserve a copy of the row_elem[3] read from...
   
   Sale Date: ,,,05052006

...and use that instead of the 3rd column from...
 
      sell, ,12345,store123,100.00

...which is indeed store123.
0
 
justinYAuthor Commented:
I agree with you. But how can I preserve a copy of the row_elem[3] ? I try to use memcpy, but I have garbage on my screen.
Can you tell me how ?

Thanks
0
 
rstaveleyCommented:
 string sale_date = "?";

  while ( /* Read line from file as before */ ) {

        /* Load row_elem vector as before */

        if (row_elem[0].find("Sale Date") != string::npos)
        {
                  sale_date = row_elem[3];   // Did you want this before or after conversion?
                  DateConvert(row_elem[3]);
       
                  cout << row_elem[3] << endl; // here shows 05052006
                                                                   
        }


        if (row_elem[0] == "Sell")
        {
             fout<<setw(2)          << setiosflags(ios::left) << "AM"
                  <<setw(20)     << setiosflags(ios::left) << "12345 "
                  <<setw(16)     << setiosflags(ios::left) << " "
                  <<setw(8)      << setiosflags(ios::left) << sale_date << '\n';
        }

  }
0
 
justinYAuthor Commented:
We are almost there, except one thing.

I have multiple lines of records. But my first line shows ? on sale_date. other lines are ok.
like this
AM12345              ?
AM12346              05052006
AM12347              05052006
AM12348              05052006

I think this is because we initilize sale_date ="?" outside of while loop. How can we fix that ? Thanks
0
 
rstaveleyCommented:
That means that your first Sell record is not preceded by a Sale Date record. You don't know what date it is.

You could apply some business logic that you collect the first Sell record(s) with unknown Sale Date (store them in a list) and apply next Sale Date, when it is known, but that makes the assumption that those records were indeed for the same Date. Alternatively, you could apply the business logic that you junk them while the Sale Date is unknown.
0
 
justinYAuthor Commented:
You are right.
Thanks for your help. You are the best
0
 
rstaveleyCommented:
Best wishes
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now