?
Solved

dates looping error

Posted on 2005-02-25
11
Medium Priority
?
208 Views
Last Modified: 2008-02-01
Hi experts,

My program reads the dates and prices. and it will write one price for 7 days (weekly).

input file is like this:
AAAAAAAAA,aaaaaaaa,3/31/2005,7654.33
BBBBBBBBB,bbbbbbbb,3/31/2005,1243.54
CCCCCCCCC,cccccccc,3/31/2005,3112,3

my desired output file should be:
AAAAAAAAA03312005765433
AAAAAAAAA04012005765433
AAAAAAAAA04022005765433
AAAAAAAAA04032005765433
AAAAAAAAA04042005765433
AAAAAAAAA04052005765433
AAAAAAAAA04062005765433
BBBBBBBBB03312005124354
BBBBBBBBB04012005124354
BBBBBBBBB04022005124354
BBBBBBBBB04032005124354
BBBBBBBBB04042005124354
BBBBBBBBB04052005124354
BBBBBBBBB04062005124354
......

BUT my current output file is like this:
AAAAAAAAA03312005765433
AAAAAAAAA04012005765433
AAAAAAAAA04022005765433
AAAAAAAAA04032005765433
AAAAAAAAA04042005765433
AAAAAAAAA04052005765433
AAAAAAAAA04062005765433
BBBBBBBBB03302005124354 // this is wrong date
BBBBBBBBB03312005124354
BBBBBBBBB0401005124354
BBBBBBBBB04022005124354
BBBBBBBBB04032005124354
BBBBBBBBB04042005124354
BBBBBBBBB04052005124354
......

Anyone can help me to see what's wrong with my code ? Thansks

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <cstdio>
#include <vector>
#include <sstream>
//#include <algorithm>
#include <vector>
#include <time.h>
//#include <math.h>

using namespace std;

const int MaxSize=1024;

int GetFields(std::string &aStr, std::vector<string> &aVec, char
aDelim)
{
      std::istringstream ss(aStr);
      std::string field;
      while (std::getline(ss, field, aDelim))
      {
            aVec.push_back(field);
      }
      return aVec.size();
};


///////////////////////////////////////////////////////
// Convert the date format from mm/dd/yyyy to mmddyyy
//////////////////////////////////////////////////////
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;
    }
};

/////////////////////////////////////////
// function to do substr
/////////////////////////////////////////
void substr(const char* src, int start, int len, char ** dest)
{
      while ( *src && --start > 0)
      src++;

      while ( *src && len > 0)
      {
            **dest = *src;
            *dest++;
            src++;
            len--;
      }
            **dest = 0x00;
};

// Leap Year Test
bool LeapYear ( int Year )
{
      bool LeapYear= false;
      if ((Year%4==0 ) && ( Year %100 != 0) || ( Year %400 ==0 ))
      LeapYear = true;
                  return LeapYear;
}

// Days per Month
int DaysPerMonth(int Year, int m)
{
      if (m == 2)
      {
      if (LeapYear(Year)) return 29;
      return 28;
      }
      if (m >= 8) m--;
      return ((m & 1)?31:30);
}

// Last day of the month test
bool is_last_day_in_month(int year, int month, int day)
{
      return day == DaysPerMonth(year, month);
}


main(int arc, char *arv[])
{
      ifstream fin("Priceweekly.csv");
      ofstream fout("Priceweeklyforimport.txt");
          string row_read;

      int isFirstLine = 0;
        
    while( std::getline(fin, row_read) )
    {
            
            // skip first title line
            if (isFirstLine ==0 )
            {
                  isFirstLine = 1;
                  continue;
            }
            // end of skip

        vector<string> row_elem;
        GetFields(row_read, row_elem, ',');
   
            DateConvert(row_elem[2]);
            //cout << row_elem[2] << endl;
            
            // print the PRICE decimal 9.999999999 to 0009999999999
            
            char elem1[100];
            sprintf(elem1,"%08.0f",atof(row_elem[3].c_str())*100);
            std::stringstream oss8;
            oss8 <<setw(15) << setfill('0') << std::string(elem1);
            //////// starting test of dates /////////

                  int m = 0;
                  int d = 0;
                  int Y = 0;
                  
                  struct tm date;
                  time_t ntt;
                  struct tm* pNextDate;

                  char szDate[10];      // weekly
                  stringstream oss;      // weekly
                  
                  m = atoi(row_elem[2].substr(0, 2).c_str()); // month
                  d = atoi(row_elem[2].substr(2, 2).c_str()); // date
                  Y = atoi(row_elem[2].substr(4, 4).c_str()); // year
                  //struct tm date = { 0 };
                  //date = { 0 };
                  date.tm_mday = d - 1;      
                  date.tm_mon = m - 1;
                  date.tm_year = Y - 1900;


///////////////////////
// Output file format
///////////////////////
// weekly
                  for (int dloop=0; dloop<7; dloop++)
                  {
            
                        date.tm_hour =0;
                        date.tm_min = 0;
                        date.tm_sec = 0;

                        date.tm_mday++;  
                        ntt = mktime(&date);
                        pNextDate = localtime(&ntt);
                        strftime(szDate,10,"%m%d%Y",pNextDate);
                        oss << szDate;
                              date = *pNextDate;
                        //cout << oss.rdbuf() << endl;
                              
                  fout<<setw(9)      << setiosflags(ios::left) << row_elem[0]
                        <<setw(1)      << setiosflags(ios::left) << "P"            
                        <<setw(15)      << setiosflags(ios::left) << oss8.str().c_str()
                        <<setw(8)      << setiosflags(ios::left) << oss.rdbuf()
                        <<setw(1)      << setiosflags(ios::left) << "A"
                        <<setw(3)      << setiosflags(ios::left) << " "
                        <<endl;
                        
                  } // end of for
// end of weekly
    } //end of while
system("pause");
return 0;
}
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
  • 4
  • 3
11 Comments
 
LVL 8

Expert Comment

by:novitiate
ID: 13403266
Which platform and compiler are you using.

Windows with VC++, I didn't get the out-put that you mentione.

Here is the output what I got.

AAAAAAAAAP00000000076543303312005A  
AAAAAAAAAP00000000076543304012005A  
AAAAAAAAAP00000000076543304022005A  
AAAAAAAAAP00000000076543304032005A  
AAAAAAAAAP00000000076543304042005A  
AAAAAAAAAP00000000076543304052005A  
AAAAAAAAAP00000000076543304062005A  
BBBBBBBBBP00000000012435403312005A  
BBBBBBBBBP00000000012435404012005A  
BBBBBBBBBP00000000012435404022005A  
BBBBBBBBBP00000000012435404032005A  
BBBBBBBBBP00000000012435404042005A  
BBBBBBBBBP00000000012435404052005A  
BBBBBBBBBP00000000012435404062005A  
CCCCCCCCCP00000000031120003312005A  
CCCCCCCCCP00000000031120004012005A  
CCCCCCCCCP00000000031120004022005A  
CCCCCCCCCP00000000031120004032005A  
CCCCCCCCCP00000000031120004042005A  
CCCCCCCCCP00000000031120004052005A  
CCCCCCCCCP00000000031120004062005A

_novi_
0
 

Author Comment

by:justinY
ID: 13403341
I am using VC++.net both on windows xp and windows 2003 OS
0
 

Author Comment

by:justinY
ID: 13403423
please, Look at my new output file:
AAAAAAAAAP01233333000000003312005A  
AAAAAAAAAP01233333000000004012005A  
AAAAAAAAAP01233333000000004022005A  
AAAAAAAAAP01233333000000004032005A  
AAAAAAAAAP01233333000000004042005A  
AAAAAAAAAP01233333000000004052005A  
AAAAAAAAAP01233333000000004062005A  
BBBBBBBBBP00033399000000003302005A  
BBBBBBBBBP00033399000000003312005A  
BBBBBBBBBP00033399000000004012005A  
BBBBBBBBBP00033399000000004022005A  
BBBBBBBBBP00033399000000004032005A  
BBBBBBBBBP00033399000000004042005A  
BBBBBBBBBP00033399000000004052005A  
CCCCCCCCCP03456790000000003302005A  
CCCCCCCCCP03456790000000003312005A  
CCCCCCCCCP03456790000000004012005A  
CCCCCCCCCP03456790000000004022005A  
CCCCCCCCCP03456790000000004032005A  
CCCCCCCCCP03456790000000004042005A  
CCCCCCCCCP03456790000000004052005A
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: 13404939
Any experts can give me some thoughts on this issue ? I am waiting online. Thanks
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13406092
            date.tm_mday = d - 1;    
               date.tm_mon = m - 1;
               date.tm_year = Y - 1900;

Here, the tm_mon and tm_year adjustments are necessary because struct tm uses 0-11 for months, and year-1900 for the year.  However, tm_mday uses the conventional 1-31 values.  I would change this line to:
             date.tm_mday = d;    

Also, your oss8 thing is just way too complex.  Plus, it looks like your zero-padding is not working correctly.  Throw it out. :)

char elem1[100];
double pennyprice_d = atof(row_elem[3].c_str()) * 100;
int pennyprice_i = (int) floor(pennyprice_d + 0.5); // for proper rounding
sprintf(elem1, "%015d", pennyprice_i);

Then at the end:
              fout<< setw(9)     << setiosflags(ios::left) << row_elem[0]
                    << "P"          // fixed length of 1
                    << elem1     // fixed length of 15
                    <<setw(8)     << setiosflags(ios::left) << oss.rdbuf()
                    << "A"
                    <<setw(3)     << setiosflags(ios::left) << " "
                    <<endl;
 
0
 

Author Comment

by:justinY
ID: 13406345
thank you for pointing out my rounding problem.
regarding the date (date.tm_mday = d), I used to have date.tm_mday = d, but still have the same probelm.
The reason I use date.tm_mday = d - 1, I want to display 7 days including the input file price date.
for example, input file I have AAAAAAAAA,aaaaaaaa,3/31/2005,7654.33, I want to display 03312005 plus 6 more days.

If I use date.tm_mday = d, then I skip 03312005, display 04012005, 04022005, 04032005, 04042005,... Any other thoughts ?
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13407614
Ok, now I see you're using d - 1 to make up for the way the loop is structured.

ntt = mktime(&date);
pNextDate = localtime(&ntt);
strftime(szDate,10,"%m%d%Y",pNextDate);

This is somewhat redundant.  mktime() takes the struct tm date, assumes it's in the local timezone, normalizes the struct tm date, then converts it to time_t.  localtime() takes a time_t and converts it to a struct tm in the local timezone, which is exactly the opposite action taken by mktime().

This could be simplified to:
mktime(&date);
strftime(szDate, 10, "%m%d%Y", &date);

The only thing I can think of is that your timezone isn't set properly, but I still don't think that would have anything to do with it because any errors introduced in the mktime and localtime calls should cancel each other out.

Also, I would set the tm_hour to 12 (noon) instead of 0 (midnight).  There might be some kind of ambiguity about which day midnight lies on.
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 500 total points
ID: 13407652
Also, set your date.tm_isdst to -1 when you set the mday, mon, and year.  That currently is not initialized, and it could be causing your problem.  The uninitialized isdst field might have a different value between the first and subsequent loops.  Actually, I think that is your problem.
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

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

741 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