Thread: date

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    12

    date

    Hello everyone

    can anyone help me to find what is the error in my code, I built a function that get the date from te first column in the csv file, and then call this function in the main program.

    the problem is getting many error and not getting the date from the first coulmn.

    please help me.

    Code:
    string GetdateValue(int column,string line)
     {
        stringstream sstr(line);
         string temp[9];
     
         for( int i = 0; i < column; ++i )
            getline(sstr,temp,',');
     
          return itoa(i,temp,9);
    }
     
     int main()
     {
         ifstream input_file("L_05.csv");// is a scv file have many coulmn first column of it is the date
         string line;
         int count;
     
    
        string field[] = {1};
     	const int date_SIZE = sizeof date / sizeof *date;
     	string date[date_SIZE];
     	
      if ( input_file.fail ())
         { 
             cout << "Error opening file.";
         }
         else
        {
             for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
                 getline(input_file, line);
             count = 8;                  // We are in the eighth line
             while(count <= 1447) // We have to add until 1447 lines..
             {
                 getline(input_file,line);
                 // number of elements in the above array
                 for(int cold = 0; cold < date_SIZE; cold++)
                 {
                     date[cold] = GetdateValue(field[cold],line);
                                   
                 }
                 count++;        // Increment the count, to go to the next line
             }
             count -= 8;
     
     	
     	for(i = 0; i < date_SIZE; i++)
             {
     	cout << " date   " << field [i] << "is  "<< date[i] << endl;
     
             }
     
     
         }
     
        return 0;
     }
    thank you for all your effort .

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Your code is missing the header files.

    What compiler did you use aswell?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    Hi
    I did not paste the heading when I post the code,

    MVC++6, win32 app

    Code:
    error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__cdecl std::getline(class std::basic_istream<char,struct std::char_traits<char> > &,class std::basic_string<char,struct std::char_traits<char>,class
     std::allocator<char> > &,const char)' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [9]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocato
    r<char> > &'
            A reference that is not to 'const' cannot be bound to a non-lvalue
    C:\date\date.cpp(23) : error C2664: 'itoa' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [9]' to 'char *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\date\date.cpp(33) : error C2440: 'initializing' : cannot convert from 'const int' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
            No constructor could take the source type, or constructor overload resolution was ambiguous
    C:\date\date.cpp(33) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Error executing cl.exe.
    heading
    Code:
    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <time.h>
    #include <numeric>
    #include <limits>  
    using namespace std;
    I am not sure what is these error are.
    Last edited by hi5; 02-01-2006 at 06:43 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Just a few
    Code:
        string field[] = {1};  // you cannot initialize a string with an int
        const int date_SIZE = sizeof date / sizeof *date; // date is not defined yet so you cannot get it's size
        string date[date_SIZE];
    Code:
        date[cold] = GetdateValue(field[cold],line);
        // GetdateValue is defined as GetdateValue( int, string ) you call it with string, string.
    the function GetdateValue() looks very wrong. Hard to tell how to improve it because I don't know the format of the date
    Kurt

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    thank you for your information, it help me to realise some of my mistake.

    my date format the csv file is dd/mm/yyyy


    the idea of my function is to send the column no and get the value of that column in a string, I suppose i did mistake.

    can you suggest a better code to do it.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    For a start you don't need the cstdio header it is for c style input/output, it can be used, but you are using iostream which is C++ input/output.

    The GetdateValue() function should go below main() with a prototype at the top.

    The indentation is messed up.

    Also string temp[9];
    This isn't a string of 9 characters length, this is an array of 9 strings.

    With the help ZuK gave and the information in this post it is a start to change the code around a bit, and make it better.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    Hi

    If I was not sure that I have mistake I would not post this thread, but the problem is I am not sure how to fix it, or to apply a better function to get the date.

    cstdio is require because the code i post is part of a program, I finished all the requirement but this is the only one still.

    the intend is fine in my orginal code, so thanks for the advise.

    but the most important part is the date.
    I made some changes now the code only bring the dd from the column and not all dd/mm/yyy
    Code:
    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <time.h>
    #include <numeric>
    #include <limits> 
    #include <math.h> 
    using namespace std;
    
    float GetValue(int column,string line)
    {
        stringstream sstr(line);
        string temp;
    
        for( int i = 0; i < column; ++i )
            getline(sstr,temp,',');
    
        return atof(temp.c_str());
    }
    
    
    int main()
    {
        ifstream input_file("L_05.csv");
        string line;
        int count;
    	int array[]={1};
        const int SIZE = sizeof array / sizeof *array;
        float date[ SIZE ] = {0.0}, value;
    	
     if ( input_file.fail ())
        { 
            cout << "Error opening file.";
        }
        else
        {
            for(int i = 0; i < 7; i++)    // Scan the first 7 lines, which is not required
                getline(input_file, line);
            count = 8;                  // We are in the eighth line
            while(count <= 1447) // We have to add until 1447 lines..
            {
                getline(input_file,line);
                // number of elements in the above array
    		for(int colm = 0; colm < SIZE; colm++)
                {
                    value = GetValue(array[colm],line);
                              }
    	
                count++;        // Increment the count, to go to the next line
    
            }
            count -= 8;
    		
    
    		for(i = 0; i < SIZE; i++)
            {
               
                cout << endl << "The date of all values of column " << array[i]
                     << " is : "<<value <<endl; 
             }
    	
        }
    
        return 0;
    }
    thank you
    Last edited by hi5; 02-01-2006 at 08:24 AM.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Please can you post a link to an example .csv file which is formatted the same as the .csv file you are using.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    here it is, mine is .csv but I changed it because it can not be upload niether zip file nor csv
    you can just save it as L_05.csv

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Code:
    int array[]={1};
    const int SIZE = sizeof array / sizeof *array;
    When those 2 lines of code finish executing SIZE only equals 1.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    sorry but it seems it did not click yet in my mind, when I inialise the array with 1 to go for the first column, and get me the value of that column.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    So what are you saying, does that help at all or is that not the problem?

    If it is not the problem, but it is still _a_ problem, fix that problem and post the new code.

    Also have you tried debugging it? What were your findings?

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    yeah, I did debug it it goes to the first column ( date) in my csv file but it only get me the day (dd)(10 in my file) not giving me the rest of the date.
    so what I saying is that it goes to the right coulmn but not all the date just part of it, so how do I set that format, I do not know

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    12

    any ideas

    any ideas for my post please

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    When I run the code all I get is

    The date of all values of column 1 is : 0
    That is because the code here
    Code:
    int array[]={1};
    const int SIZE = sizeof array / sizeof *array;
    Sets SIZE to 1, like I said above.

    So the code below only executes once.

    Code:
    for(i = 0; i < SIZE; i++)
            {
               
                cout << endl << "The date of all values of column " << array[i]
                     << " is : "<<value <<endl; 
             }
    So like I said in my last post, I am waiting for you to fix that piece of code and repost it.

    Judging from your post here
    http://www.daniweb.com/techtalkforums/thread37770.html

    It seems you want to put the data from the other columns aswell, that's why you need to fix those 2 lines of code, so that SIZE is more than 1, so that the other code will loop more than once. And then from there I can try and find what other problems are happening.

    From this post
    http://www.programmersheaven.com/c/M...ing=A9999F0001
    It says you want to check these specific columns.
    {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,46,50 ,51,52,61,71,81,91,101,111,121,131,141,151,161,171 ,181,191 }

    If you are having trouble doing this, just say and we can try to help.

    But you need to fix that part of the code I said before I can help you anymore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM