assigning ints into a string

This is a discussion on assigning ints into a string within the C++ Programming forums, part of the General Programming Boards category; I have to assign int values from arrays to a string so I can use the string to display output. ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    44

    assigning ints into a string

    I have to assign int values from arrays to a string so I can use the string to display output.

    Code:
    string Polygon::ShowPolygon()
    {
       string displayPoly;
    
       for (int h = 0;h < num_points;h += 2)
       { 
          displayPoly += "(" + points[h] + "," + points[h + 1] + ")";
          if (h < num_points)
             displayPoly += ",";
       }
       return displayPoly;
    }
    Im getting errors, and I cant find any information in my book or on the web how to do this. If you can just provide me with a tutorial link on how to do this Id be grateful.

  2. #2
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    It helps if you post your errors.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    printf() outputs to the standard output.
    _snprintf() is another useful function
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    44
    Quote Originally Posted by Nor View Post
    Nor- here is the entire program with error output on the bottom
    http://codepad.org/TLWDe1ey


    Quote Originally Posted by Nor View Post
    printf() outputs to the standard output.
    _snprintf() is another useful function
    Which would be nice if I was using C, but Im using C++, and the little I know about C string functions only makes me wonder how the most used procedural language in the world does not have safe/fault tolerant IO by default.
    Last edited by shintaro; 11-20-2008 at 09:49 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Use a std::stringstream in place of the sprintf family of functions:
    Code:
    #include <sstream>
    
    ...
    
    string Polygon::ShowPolygon()
    {
       std::stringstream displayPoly;
    
       for (int h = 0;h < num_points;h += 2)
       { 
          displayPoly << "(" << points[h] << "," << points[h + 1] << ")";
          if (h < num_points)
             displayPoly << ",";
       }
       return displayPoly.str();
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    44
    hk_mp5kpdw- I appreciate the help, but there has got to be an easy(er) way to do this without resorting to exotic functions that my teacher has never heard of. Time for me to crack more books and see if I can find an instance of ints being assigned to a string.

    Edit: I think the problem is that I cant concat an int with strings when assigning to a string. Im taking a Java and C++ class at the same time, I think Im getting stuff mixed up between these languages.

    sry for not explaining clearly what I wanted
    Last edited by shintaro; 11-20-2008 at 11:26 PM.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    std::stringstream is part of the C++ standard. it is not exotic. It is the easiest way to do what you want.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    stringstream is what you want.

    Another possibility is that ShowPolygon is supposed to output directly to cout instead of creating a string. In that case you don't have to convert an int to a string because you can just send the ints to the output stream directly.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    44
    Raigne, Daved- Yes youre right, after some looking I came across info on converting numbers to strings. So I guess this is what I will have to put in my code:


    Code:
       double c;
       c = -3.4;
       ostringstream cnvrt;
       cnvrt << c;
       s = cnvrt.str();
    Daved- the function has to return a string, so I cant do output inside the function.

    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. getting a string from ints
    By fbplayr78 in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2003, 04:03 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. convert ints to string
    By Zughiaq in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 12:37 PM

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