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?
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?
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
To be exact, I want to store the string in a variable, as such:
I'm not sure if that syntax is correct, but I can't experiment if the code can't be compiled.Code:str = num, ch;
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.
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.
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:
Yeah, itsme86 is right. You want to create a string from them, not parse a string.Originally Posted by 843
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
My bad for using the wrong term. Thanks for the help.