Solved

buffer or what ??

Posted on 2006-06-08
11
294 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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 125 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

820 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