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

    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++, 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:
    explicit Date();
        month = 1;
        day = 1;
        year = 1900;
    Here is the function I suspect is causing trouble:
    string getDate() const;
    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:

    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:

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

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

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    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
    neptune whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    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
    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
    The Netherlands
    basic stringstream example:

    #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
    Problem with itoa() perhaps?
    Yes. Problem? Using it. Solution? Don't use it. Buggy. Error-prone. EOF.

    See above posts for alternatives.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /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, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

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