Thread: Store combined data types into string?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Store combined data types into string?

    I want to parse two variables, an integer and a character into a string (e.g. 20d, 1e, 72a, etc.). How should I do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That depends on what is the exact format. Is there really only supposed to be one character at the end? If so, you can access that last character and store it in a char variable, then replace that character with a null character, then use strtol (or more simply but with no error checking, atoi) to convert the string into an integer.
    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
    Oct 2010
    Posts
    135
    To be exact, I want to store the string in a variable, as such:

    Code:
    str = num, ch;
    I'm not sure if that syntax is correct, but I can't experiment if the code can't be compiled.

    I have got the values of num and ch in their respective data types, but I can't figure out how to parse them together.

    I would like to convert them into a string, not the other way around, so atoi wouldn't work.
    Last edited by 843; 12-01-2010 at 11:50 AM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Parsing is usually the reverse process. Taking a string like "20d" and turning it into an integer (20) and a character ('d'). If you already have the separate variables then you can just use sprintf() to join them in a string. sprintf(stringbuffer, "%d%c", myint, mychar);
    If you understand what you're doing, you're not learning anything.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, what I did not say is that you should first read into a string, and then parse that string as I described (assuming I guessed the format correctly).

    EDIT:
    Oh wait, this did not register:
    Quote Originally Posted by 843
    I would like to convert them into a string, not the other way around, so atoi wouldn't work.
    Yeah, itsme86 is right. You want to create a string from them, not parse a string.
    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

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    My bad for using the wrong term. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM