Thread: concatination of string problem

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    concatination of string problem

    in my c++ course we have started classes. I created a class Date which has a default constructer
    Code:
    Date::date(int, int,int) that takes mm-dd-yyyy.
    The private data is
    Code:
     int intmonth, day, year;
    The function

    Code:
    string Date::toString() const
    is to convert the int date to a string date. This is obviously simple, we only have to change the mm to a string, using an enumertion class
    Code:
    Month{January=1, ....}
    Month month;
    Now.. this functon must return a string. Therefore, the string must consist of
    Code:
    string Returnstring;
    Returnstring =  month(intmonth) + " " + day + "st/nd/rd/th" + ", " + year;
    return  Returnstring;
    My problem: the concationation of this string. I cannot just output to the string, my teacher asked for the function to return a string. In the driver it has
    Code:
    cout << d1.toString();
    I have read about the itoa function.. but i get the same prototype error as everyone else, and i have tried every hint online that have yeildd no results. I have heard of a function in class ostream in standard library? Does anyone know a good way to convert these integers to strings so i can do the concatination.

    I use pico, and the sun compiler if it helps..

    Thank you
    Last edited by Salem; 10-25-2006 at 05:47 AM. Reason: snip email address from the spambots

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What you want is a stringstream. Create an ostringstream variable, then pretend it is cout and do the same thing you would do to print your date to cout. In fact, a good start is to use cout and test that you have that working, then replace it with the ostringstream variable. Once you've "output" your date to the ostringstream, call the str() function to return an actual string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM