• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

dates looping error

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
justinY
Asked:
justinY
  • 4
  • 3
1 Solution
 
novitiateCommented:
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
 
justinYAuthor Commented:
I am using VC++.net both on windows xp and windows 2003 OS
0
 
justinYAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
justinYAuthor Commented:
Any experts can give me some thoughts on this issue ? I am waiting online. Thanks
0
 
NovaDenizenCommented:
            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
 
justinYAuthor Commented:
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
 
NovaDenizenCommented:
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
 
NovaDenizenCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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