Stupid easy noob question.

This is a discussion on Stupid easy noob question. within the C++ Programming forums, part of the General Programming Boards category; Hi, i've tried look about for this but cant find it . Basically all i want to do its break ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Angry Stupid easy noob question.

    Hi, i've tried look about for this but cant find it .

    Basically all i want to do its break a number up into single ints.

    I.E

    id have

    int a
    int b
    int c

    and a number, say 189. I want break 189 into 1,8 and 9. So a=1 b=8 and c=9.

    thanks for any help.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use math, specifically division and modulus. What's (189 / 100) % 10 in integer math? It's 1. What's (189 / 10) % 10? It's 8. What's (189 / 1) % 10? It's 9.
    Last edited by Daved; 04-10-2008 at 03:12 PM. Reason: Thanks Todd

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Daved View Post
    What's 189 % 100? It's 9.
    89..
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    oh, so i cant just break it up you can in java?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If "189" is a string, then yes you can parse it out. You implied 189 was an integer by calling it a number, (and because you didn't put double quotes around it), thus you got a math solution.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,747
    Of course you can, and Daved explained how.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    yeah it is an int i will be reading in.

    so there is no classes to do this sort of thing? sorry im a noob at c++

    in java i would just use the scanner and nextInt()

    thanks for the replys.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, you wouldn't do that in Java, as nextInt() only works with strings (you said it's an int) and would grab the entire number.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Or the std::stringstream class would work.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> yeah it is an int i will be reading in
    Or you might be able to read it in as a string and get each digit that way. To convert a single character to an int just subtract the zero character:
    Code:
    string num("189");
    int second_digit = num[1] - '0'; // second_digit is 8.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Real Noob question
    By Dark Greek in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2007, 07:49 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. Noob here...a simple question.
    By The_Mayor in forum C Programming
    Replies: 4
    Last Post: 06-08-2005, 01:48 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Easy Question
    By Bryan in forum C++ Programming
    Replies: 15
    Last Post: 10-17-2002, 05:25 PM

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