Is it possible to increment/decrement a number if the number has been initialized as a character?

To be a more specific, I am trying to decrement the character every time a
condition takes place. i.e. from 10 to 9 to 8...etc. There is a function which
only takes a constant string literal or a pointer to a character and the
character in question is a number. Is it possible to perform arithmetic
operations if the character is a number? I have done it by surrounding the
character with single quotes ' ' but.... when I declare the character as a
pointer, the compiler demands that I surround the number with double quotes " "
and I can't seem to find out how to output anything with that.
A string literal is not a character it is an array of characters. So it is not clear what you are doing.
As for the type char then it is an integral type and you may apply all operations with a value of type char that are acceptable for integral types.
Hopefully this will make it a little more clear.

This Function

TTF_RenderText_Solid( font, "10" , textColor );//creates a surface that takes font, text, and text color respectively


works if I just insert a number in the second parameter. The problem is I want to decrement that second
parameter. The documentation for this function says it takes a pointer to a character so I declared one

char* x;

TTF_RenderText_Solid( font, x , textColor );


The problem is I just don't know how to perform arithmetic operations on x [second parameter]
Probably best to give out the relevant code, or at least more information. This is too vague for any really concrete help. Should've refreshed.
Last edited on
Also I have searched around a lot for any detailed explanations on nuances with strings, chars as its been hellish for me to understand both and the subtle differences between both. Do you know of any good documentation on them? Thanks
@Moeljbcp
As for relevant code...that's it.

I believe it is just the basics of working with chars I am having a problem with. Even without that function, I don't know how to perform operations on char pointers.
I'm sorry, I should've refreshed before I replied :( My bad. As for decrementing a character, well, that's kind of messy. Why does it take a pointer to char* if it's accepting a number as input? That seems really strange.

If you really need to perform integer arithmetic on a char* I'd recommend looking at the atoi and itoa functions, which convert from char* to int and int to char* respectively.
Last edited on
It is totally unclear how you are going to decrement the second parameter. I do not see where you are decrementing anything. You showed only two function calls. In the first function call you specified a character literal that has type const char[3] and thus can not be changed.
@ vlad from moscow

That is the problem. My mistake for even putting the first function call. I just put it up to give an
example of what would work....but the second function call is what I plan to use because it seems to be the only way I can "potentially" do any operations on it x.

here's a quick synopsis of what I am aiming at. game starts off with ten turns...

- player has 10 turns left
- user presses enter
//some operation happens that will decrement x

- player has 9 turns left

...and so on

@Moeljbcp

Thanks! Will check them out now
Last edited on
Why did you declared the second parameter as char array (or pointer to char)? Why would not use an integral type instead?
closed account (DSLq5Di1)
Use itoa() as Moeljbcp suggested or a stringstream, to convert your value to a string for output.

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
http://www.cplusplus.com/reference/iostream/stringstream/

dtaqee88 wrote:
Also I have searched around a lot for any detailed explanations on nuances with strings..

http://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/
http://www.lysator.liu.se/c/c-faq/c-2.html
@ vlad from moscow

I wish I could just use an integral type as it would be so much simpler but the function in question demands that the second parameter be a const char pointer.

@ sloppy9

Thanks. Will check them out now
Last edited on
If the function requires that the second parameter will be a const char pointer then you shall not do any changes of its value (I mean the char array pointed to by the pointer) in the function body. Otherwise the function is defined incorrectly. You can convert it to an int value either with atoi or stoi functions and deal with the converted value.
Last edited on
Assuming that you know you are outputting integers:
1
2
3
4
5
int turns = 10;
while (turns >= 0) {
    TTF_RenderText_Solid(font, char(turns + '0') , textColor);
    --turns;
}


Edit:
Actually, this is pretty bad =/. I knew it was bad, but then I realized the limitation of characters is '0' through '9'. Soooo bad.

What I have actually done in my own code is made a function that accepts an std::string for the text to output, two ints representing pixel positions on the screen, and an optional switch to center the text horizontally. The std::string has to then be converted to a C string, but I use them because they are so easy to work with. The function also interprets color codes imbedded within the text. I also keep track of a "cursor" to output paragraphs easily.

With this setup I just use:
1
2
3
4
5
6
template <typename T>
std::string itoa (T num) {
    std::ostringstream ss;
    ss << num;
    return ss.str();
}

To convert numbers to std::strings.

The result of all this is much more flexibility:
1
2
int n = 5;
outputText(10, 10, "Number: " + itoa(n));


I actually would recommend doing something like this that works the way you want it as it is much more flexible and more easy to use. Not very difficult to set up either. =]
Last edited on
@monad


Well, and what will be the result of the expression char( 10 + '0' )?! :)
Heh, I was typing my edit as you posted that. Yeah, terrible suggestion it was.
The atoi and itoa functions worked like a charm.

@ sloppy9

Thanks for those links. Very informational.
Topic archived. No new replies allowed.