Thread: Taking a number in the middle of an integer

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    Taking a number in the middle of an integer

    Can anyone help me? I'm trying to do this:

    Code:
    cin >> integerNumber
    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?

    Thank you

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Take the input as a std::string, and print out the third character with the [ ] operator.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Hm... it worked. And if i want to add 1 to this number?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Do you know how to add one to a character?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or you can use a little math:
    2012 / 10 = 201 (division)
    201 % 10 = 1 (modulus)
    1 + 1 = 2 (addition)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Quote Originally Posted by grumpy View Post
    Do you know how to add one to a character?
    No .-.

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by mgcpovoleri View Post
    No .-.
    If you take input as as std::string, you're saying you don't know how to APPEND+=! a character to it? Oh sorry, my keyboard went all funky after trying to APPEND an exclamation point.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by grumpy View Post
    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?
    Oh. Oh wow, okay.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    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?

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You could just convert string to int and then add them

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mgcpovoleri
    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?
    Convert the numeric string to its corresponding integer value then add 2 to that result.
    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

  13. #13
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    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:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Rodaxoleaux View Post
    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.
    Nah nothing so mundane, it's '1' + 1 in this case.
    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"

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  2. round a number to superior integer
    By nevrax in forum C Programming
    Replies: 5
    Last Post: 03-30-2007, 02:57 AM
  3. add one integer with is own number
    By nevrax in forum C Programming
    Replies: 5
    Last Post: 03-30-2007, 02:24 AM
  4. how to generate a random integer number between 0 and 9
    By dongkhoi in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 01:49 PM
  5. Program that finds the middle number
    By Zerohero11 in forum C++ Programming
    Replies: 17
    Last Post: 10-13-2005, 07:20 PM