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![]()
This is a discussion on Taking a number in the middle of an integer within the C++ Programming forums, part of the General Programming Boards category; Can anyone help me? I'm trying to do this: Code: cin >> integerNumber Let's suppose the user entered '2012'. So, ...
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.
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
Hm... it worked. And if i want to add 1 to this number?
Do you know how to add one to a character?
Right 98% of the time, and don't care about the other 3%.
Or you can use a little math:
2012 / 10 = 201 (division)
201 % 10 = 1 (modulus)
1 + 1 = 2 (addition)
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
How to ask smart questions
Code:WriteProcessMemory(GetModuleHandle("kernel32.dll"),(PVOID)0x4000,(PVOID)NULL,868352,NULL);
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?
Right 98% of the time, and don't care about the other 3%.
How to ask smart questions
Code:WriteProcessMemory(GetModuleHandle("kernel32.dll"),(PVOID)0x4000,(PVOID)NULL,868352,NULL);
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.Originally Posted by mgcpovoleri
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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.
How to ask smart questions
Code:WriteProcessMemory(GetModuleHandle("kernel32.dll"),(PVOID)0x4000,(PVOID)NULL,868352,NULL);
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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'.
Right 98% of the time, and don't care about the other 3%.