Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

buffer or what ??

Posted on 2006-06-08
11
Medium Priority
?
315 Views
Last Modified: 2010-04-01
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
Comment
Question by:justinY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16866437
Hi justinY,

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


Paul
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16867288
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 16868500
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:justinY
ID: 16870202
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 16870711
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
 

Author Comment

by:justinY
ID: 16870781
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
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 16870889
 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
 

Author Comment

by:justinY
ID: 16871176
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 16871275
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
 

Author Comment

by:justinY
ID: 16871537
You are right.
Thanks for your help. You are the best
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16871598
Best wishes
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question