Can anyone help me? I'm trying to do this:
Let's suppose the user entered '2012'. So, I want to output the third number in this int, in this case, the '1'. How I do that?Code:cin >> integerNumber
Thank you :)
Printable View
Can anyone help me? I'm trying to do this:
Let's suppose the user entered '2012'. So, I want to output the third number in this int, in this case, the '1'. How I do that?Code:cin >> integerNumber
Thank you :)
Take the input as a std::string, and print out the third character with the [ ] operator.
Hm... it worked. And if i want to add 1 to this number?
Do you know how to add one to a character?
Or you can use a little math:
2012 / 10 = 201 (division)
201 % 10 = 1 (modulus)
1 + 1 = 2 (addition)
That's not actually what mgcpovoleri asked, Rodaxoleaux. The question was about incrementing a character, not appending a character to a string. Even more elementary than you thought, eh?
Hint to mgcpovoleri: char is an integral type (albeit the character '1' is not numerically equal to 1). How would you add 1 to an integral value?
Let's ask again: I've a string with value "1", and I've a integer with value '2'. How I add 1 + 2 and get the number 3?
You could just convert string to int and then add them :)
Convert the numeric string to its corresponding integer value then add 2 to that result.Quote:
Originally Posted by mgcpovoleri
I never thought I'd view a thread on a computer programming forum where the members would actually have to teach someone how to add 1+1.
I'll give a hint. Given a character that is a digit ('0', '1', '2', .... '9') subtracting '0' converts to the value (i.e. '1' - '0' = 1, '2' - '0' = 2, etc). The reverse also applies: adding '0' to 1 gives '1'.