Problem with itoa() perhaps?

This is a discussion on Problem with itoa() perhaps? within the C++ Programming forums, part of the General Programming Boards category; Okay, I've tried everything I can think of including couts all over the place to see what my values looks ...

  1. #1
    Not just a squid...
    Join Date
    Sep 2004
    Posts
    25

    Problem with itoa() perhaps?

    Okay, I've tried everything I can think of including couts all over the place to see what my values looks like and I'm out of ideas, so I'll see if anyone else can figure it out.
    For starters, I'm using Dev-C++ 4.9.9.2, though I doubt it matters for this.

    I have a simple Date class in Date.h and Date.cpp. I'm including <string> and <cstdlib>.

    Here is my constructor:
    Code:
    explicit Date();
    Code:
    Date::Date()
    {
        month = 1;
        day = 1;
        year = 1900;
    }
    Here is the function I suspect is causing trouble:
    Code:
    string getDate() const;
    Code:
    string Date::getDate() const
    {
        char m[2], d[2], y[4];
        string date;
        
        // Convert the date fields to strings.
       itoa( month, m, 10 );
       itoa( day, d, 10 );
       itoa( year, y, 10 );
        
        // Assemble the date fields.
        if( month < 10 )
            date += "0";
        date += m;
        date += "/";
        if( day < 10 )
            date += "0";
        date += d;
        date += "/";
        date += y;
       
        return date;
    }
    Currently, this is the only code left in main, I have commented everything else out:

    Code:
    static Date currentDate;
    
    cout << currentDate.getMonth() << endl;
    cout << currentDate.getDay() << endl;
    cout << currentDate.getYear() << endl;
    cout << currentDate.getDate();
    And this is the output I'm getting:
    1
    1
    1900
    1/0/1900

    also If I change the day in the default constructor such as month = 10; day = 10;
    the output becomes:
    10
    10
    1900
    //1900

    If anyone knows whats up let me know please.
    Last edited by TheSquid; 05-06-2006 at 08:40 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    char m[2], d[2], y[4];
    m: enough space for 1 character and a null
    d: enough space for 1 character and a null
    y: enough space for 3 characters and a null

    Afraid of sprintf?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    Use of itoa is completely unnecessary, I think numbers can be inserted into a string on their own.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    It would probably be better to use a stringstream or sprintf, since those are standard (itoa is not) and they work directly with the C++ string class.

    >> I think numbers can be inserted into a string on their own
    No, you cannot simply insert a number into a string.

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    basic stringstream example:

    Code:
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        stringstream ss;
        int day = 2;
        int month = 3;
        string target;
    
        ss << day << month;
        target = ss.str();
    
        return 0;
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Problem with itoa() perhaps?
    Yes. Problem? Using it. Solution? Don't use it. Buggy. Error-prone. EOF.

    See above posts for alternatives.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21