Thread: typecasting an int into a string

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    typecasting an int into a string

    I'm trying to write a function that does the following (in pseudocode)

    string1 = "Item ";
    int Number = 5;
    string2 = (char) Number;

    finalstring = strcat(string1 + string 2);

    I've tried a variety of options, but none is working. How do you do something like this?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the integer is just one digit, add '0' to it and then cast to char. If not, use say, sprintf() or snprintf().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Not one digit and the number changes.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pollypocket4eva
    Not one digit and the number changes.
    In that case use sprintf() or snprintf(). The latter is safer because the maximum size of the string is specified.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Can you provide a sample? I'm not sure how to declare lengths etc

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    In this example,
    Code:
    int main ()
    {
      char buffer [50];
      int n, a=5, b=3;
      n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
      printf ("[%s] is a %d char long string\n",buffer,n);
      return 0;
    }
    Why do you need variable n?

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Oh, i got it! thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM