Like Tree1Likes
  • 1 Post By iMalc

Taking a number in the middle of an integer

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, ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Take the input as a std::string, and print out the third character with the [ ] operator.
    Manasij Mukherjee | gcc-4.8.2 @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 !



  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,438
    Do you know how to add one to a character?
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,790
    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
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    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,&dwBytesWritten);

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

  9. #9
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    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,&dwBytesWritten);

  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
    21,939
    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.
    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

  13. #13
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    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,&dwBytesWritten);

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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.
    Rodaxoleaux likes this.
    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,438
    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%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 12: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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21