Thread: I'm new to C++ and need basic help please

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    Talking I'm new to C++ and need basic help please

    I'm new to C++ and one of the basic programming exercise questions I have is:
    Write a program that prompts the user to input a four-digit positive integer. The program then outputs the digits of the number, one digit per line. For example, if the input is 3245, the output is:
    3
    2
    4
    5


    That is the entire question. I need to be pointed in the right direction on how to get the program to print the different numbers from the integer separately. This is only the second chapter in the book, so the most advanced thing we have is probably a string....no arrays yet or anything like that.....I have gone over the chapter again and again, but I can't find out what I need to be doing. This doesn't have to be turned in until Monday, but I would like to understand what I'm doing and try it with some different programs that we wrote earlier, before Monday. I don't want everyone thinking as I saw from another posting that I am on here to have others do my work...I am really just very eager to learn.
    Thanks in advance!!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No need for an array or string, just use math. Specifically integer division.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you know the input required will be a 4-digit positive integer, then i have a quick method.. but ill try and just give you a hint so you can get the rest.

    say we have an integer 25. how can we get the first digit, 2? say we use division. 25/x = 2. what number must we divide 25 by to get 2? remember integer division results in an integer. example, 25/6 = 4, not 4.16666... (also note, the x or divisor wouldnt change if we had any other number > 9 and < 100, say, 30, or 90, instead of 25. that is, dont overcomplicate it!).

    now we have the first digit, how do we isolate the 5 in 25? hint: check out the mod/modulus/&#37; operator on a calculator. (you could use the 'x' value from the previous step!)

    this method i just thought of know, so im sure there are many ways to do it, and other more efficient ways. also i hope im not being too ambiguous or vague.
    Last edited by nadroj; 09-06-2007 at 07:16 PM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    11

    Unhappy Ok

    Ok, so before when I said I needed a little help....really I guess I should have said give me the answer lol lol!! because I don't get what you guys have given me so far. I really do appreciate you taking the time to answer me, I just don't understand yet.

    How would you know what to have it divide by if you don't know what number the user will be inputting?

    Thanks again!!

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you do just want the answer and not to put any work into learning the language, drop the course.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i thought i might have been to ambiguous.

    if we have 25, and we want 2, we need to divide 25 by some number x. note in my previous i said that that x wasnt 'specific' to 25. that is, if we had a different integer, say, 43 and we wanted the first digit, we could divide 43 by that same number x to get 4. again, remember this is integer division, so 1/2 = 0 not 0.5, 1/3 = 0, not 0.333.., 3/2 is 1 not 1.5. only the 'integer' part of the quotient is kept.

    so we need an x such that (with INTEGER division): 25/x = 2; 43/x = 4, 15/x = 1, etc. in general, our x satisfies this, which should give away the answer for what x would be: 10/x = 1.

    now that you have your x value, use the mod operator. 25 mod x = 5, the last digit. also, 43 mod x will return 3; 15 mod x will return 5.

    so in both cases (getting the 1st digit then the 2nd digit of our 2-digit number) we divded the original number by x to get the first digit then used the mod operator on the original number with x to get the second digit. as long as our original number is an integer > 9 and < 100 (that is, as long as its 2-digits), the x will be the same for both operators and yield the correct digits.

    the only time x changes is when the placeholder changes. example: 345/x = 3; 345 mod x = 45; 45/x = 4; 45 mod x = 5. do you see a pattern for the value of x?

    hope it helps.. and im sure some one else here has a better method, but ive got to get back to my calculus homework. good luck
    Last edited by nadroj; 09-06-2007 at 07:42 PM.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    MacGyver
    U were the exact person I had in mind when I ever so carefully typed my original question. Clearly I want help or I wouldn't have taken the time to search this website for an assignment that is worth about 1 point. I haven't been rude or gotten smart with anyone in my postings. I am not asking for someone to just do my work. What I was saying as I said before was that with the answers I have gotten thus far I do not understand. I appreciate what I have been given and have even gone through my book to try and find information to help me using the integer division info.....still don't get it. If you don't want to help me that is def your right, however please refrain from making assumptions and rude comments. I'm not a slacker and I do not simply cheat or copy the work of others, so I can understand why you wrote what you did.

    Thank you to those that have taken the time to help me I really do appreciate it. I really am not trying to just sponge off of you. Thank you soo much!

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    This is soooo much clearer!! I get that x is 10 in our first examples and then u add a 0 depending on how long the first integer entered is....I didn't get the whole mod thing....I see it on my calculator and I can type it in and get the answer just fine, but I didn't know what that actually was!!! I will be fine now! Really again thank you soooo much!!

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    More generally, write a program that inputs a string and then prints it out, one character at a time. In your special case it does what you want.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    11
    that's what I was originally thinking because I used a string in a previous question for the same chapter, but I don't know how to make the output divide the string up....

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you can use a stringstream object and write the integer directly to it i believe.
    i imagine doing some math trick would be more efficient than the stringstream, though. just takes some sitting down with paper and pencil and thinking.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string s = "strings";
    
        std::cout << s[0] << s[2] << s[1] << s[5] << '\n';
    
        return 0;
    }
    Last edited by robwhit; 09-06-2007 at 08:12 PM.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It's safer and more general to check the number of characters in s and then print them all out using a loop. It still does the right thing in the OP's special case.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, let's clarify one thing that the original poster didn't quite understand. In C there are different types, integer numbers and floating point numbers. Integer numbers are "whole numbers", such as 25, 16, 4711, 19233412, etc. Floating point is "any number", e.g. 4.1, 7, 19.231272414, 3.1415926, etc. They aren't necessarily PRECISELY the number you want (just like on a calculator, at least a few years back, if you divide 1 by 3, you get 0.33333... etc, but if you multiply by 3 again, you get 0.9999...).

    If you use division on integers, the answer will be an integer. 25/4 becomes 6 exactly, not 6.25. You can find out how much is "left" (remainder of sort) with the modulo operator %, e.g. 25%4 is 1.

    So it's perfectly possible to divide the number by suitable numbers and then print the result. For example 3256 / 1000 = 3.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    39
    If you want to break a integer then you use "/" and "%" (div and mod) operator

    int i = 3456;
    int j = i / 1000; // j have 3
    int k= i%1000;// k have 456 (remender)

    Code:
    #include <iostream>
    
    int main(void){
    int i = 1234;
    cout<<(i/1000)<<endl<<((i%1000)/(100))<<endl<<((i%100)/(10))<<endl<<(i%10);
    return 0;
    }

Popular pages Recent additions subscribe to a feed