#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    7

    C++, seperating the digits of an intger


    In a book I have been reading about C++, one of the exercises has asked me to separate a 5 digit integer into single integers separated by 3 spaces using the / and % operators.

    for example
    12345 --> 1 2 3 4 5

    I have no idea how to do this and I feel I can't move on until I know how to do this. I am only a beginner at c++ and have not come across for or while statements yet so if you could try to give me pseudo code that has for or while statements.

    I only want something like a formula on how to do this.

    Thanks in advance
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    Convert the integer to a string (man sprintf), then read off the digits from the string.

    Comments on this post

    • almo2001 agrees

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    7
    Originally Posted by mitakeet
    Convert the integer to a string (man sprintf), then read off the digits from the string.
    I have no idea how to do that
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    7
    its fixed now, thanks for the help
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2227
    Originally Posted by mitakeet
    Convert the integer to a string (man sprintf), then read off the digits from the string.
    The complication here is that the problem is from a C++ book which, if it remains true to form, is absolutely dead-set against its readers ever discovering the existence of the *printf family of functions, but rather wants to lock its readers into iostreams.

    As such, the book would be looking for using either the stringstream class, or the strstream class if it pre-dates the new 1998 standard.

    The OP indicates that he has solved the problem, but doesn't say how.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    7
    Originally Posted by dwise1_aol
    The complication here is that the problem is from a C++ book which, if it remains true to form, is absolutely dead-set against its readers ever discovering the existence of the *printf family of functions, but rather wants to lock its readers into iostreams.

    As such, the book would be looking for using either the stringstream class, or the strstream class if it pre-dates the new 1998 standard.

    The OP indicates that he has solved the problem, but doesn't say how.
    sorry I should have posted the code

    number1 = (number / 10000);
    number2 = (number / 1000) % 10;
    number3 = (((number / 100) % 100) % 10);
    number4 = ((number % 100) / 10);
    number5 = ((number % 1000) % 10);

    numberX being the individual numbers and number being the 5 digit integer the user entered
  12. #7
  13. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,974
    Rep Power
    510
    Next try to generalize your good solution to handle larger numbers.
    (loops and arrays will help)
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    7
    Originally Posted by b49P23TIvg
    Next try to generalize your good solution to handle larger numbers.
    (loops and arrays will help)
    I am only a beginner so please cut me some slack
  16. #9
  17. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,974
    Rep Power
    510
    Sorry. Having accomplished this difficult chore, you really ought to reward yourself with a two week vacation on a private tropical island.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    7
    I wish :P
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2227
    If you had ever seen James Edward Olmos playing math teacher Jaime Escalante in the movie, Stand and Deliver (1988 -- outstanding movie; watch it), you will know that that is not the answer:
    Originally Posted by Stand and Deliver
    In the intensive summer school calculus class, Pancho is working a problem on the board and gets stuck:
    Jaime Escalante: Do you want me to do it for you?
    Pancho: Yes.
    Jaime Escalante: You're supposed to say no.
    The key in his class was for the students to have ganas ... the desire to succeed. Pancho's answer showed he had lost the ganas.

    It's one thing to write a program to solve one specific problem, but that's not the idea of programming. You use an editor to write your code. Somebody had to write that editor. If you have another text file to write, do you expect somebody to have to write an entirely new editor for that specific file? And yet another editor for a third specific file? Or would you expect that editor to be able to handle any kind of text file? As in, one program to solve any of a particular kind of problem.

    You wrote code to split up the digits of a 5-digit number. What about a 6-digit number? Now you have to write a separate program to handle that case. What about a 4-digit number? Yet again, you have to write yet another separate program.

    Rather, what you really need to think about is how to write a program that can work on any size of number. Because that's what programming is really about. If you have the ganas.

    Consider using an array to hold the digits. When you print them out in the end, you can start at the end of the array and work towards the beginning just as easily as you can start at the beginning and work towards the end. By the same token, when you tear the number apart into its individual digits, you can start at either end. And think about this: you don't know how big the number is at first, but you do know that it will have a right-most digit there in the 1's place. So which end of the number do you think you should start at?

    Remember, Ese: ¡ganas!
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    7
    Originally Posted by dwise1_aol
    If you had ever seen James Edward Olmos playing math teacher Jaime Escalante in the movie, Stand and Deliver (1988 -- outstanding movie; watch it), you will know that that is not the answer:

    The key in his class was for the students to have ganas ... the desire to succeed. Pancho's answer showed he had lost the ganas.

    It's one thing to write a program to solve one specific problem, but that's not the idea of programming. You use an editor to write your code. Somebody had to write that editor. If you have another text file to write, do you expect somebody to have to write an entirely new editor for that specific file? And yet another editor for a third specific file? Or would you expect that editor to be able to handle any kind of text file? As in, one program to solve any of a particular kind of problem.

    You wrote code to split up the digits of a 5-digit number. What about a 6-digit number? Now you have to write a separate program to handle that case. What about a 4-digit number? Yet again, you have to write yet another separate program.

    Rather, what you really need to think about is how to write a program that can work on any size of number. Because that's what programming is really about. If you have the ganas.

    Consider using an array to hold the digits. When you print them out in the end, you can start at the end of the array and work towards the beginning just as easily as you can start at the beginning and work towards the end. By the same token, when you tear the number apart into its individual digits, you can start at either end. And think about this: you don't know how big the number is at first, but you do know that it will have a right-most digit there in the 1's place. So which end of the number do you think you should start at?

    Remember, Ese: ¡ganas!
    now here is the better version (or so I think)
    Code:
    C++ Code:
     
    #include <iostream>
    #include <string>
    using namespace std;
     
    int main()
    {
        string number;
        cout << "Please enter a digit of any size: ";
        cin >> number;
     
        for (int i = 0; i <= number.length(); ++i)
        {
            string digit = number.substr(i, 1);
            cout << digit << "   " ;
        }
        return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo