Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with c++ CODE PLEASE

    I am new to the C++ programming and i have an assignment due but not sure where to start and how to actually set the program up to do what is needed...

    it says to write a program that reads integers that the user inputs (ex: 3475) then outputs each individual integer (ex: 3 4 7 5 ). THEN gives the sum of the numbers...can anybody help me out please ???

    also i just started learning about functions and i believe we have to use IF, ELSE, DO statements in order to do this.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Do you know cin, cout, and the integer data type? If so, the first step might be to up a little program to read an integer and then write it. Then you can figure out the logic necessary in between there to do what you want with the integer. One suggestion for that part: think about how you might get at each digit if you were doing it on a calculator that uses integer division.

  • #3
    New Coder
    Join Date
    Nov 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i know all about cin and cout and integer data types...what im trying to figure out now is how to get the program to output each individual number that was entered (no matter how long the number is). therefore i was thinking i would need some type of loop (while/if ect) knowing that, what code can i use for this to work...

    also, i need to have the program add up all the integers that was inputed aka another loop???

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    I understood the question the first time: what you're asking is for someone to do your homework for you. I won't, but I'll offer the same suggestion as the first time: think about how you might get at each digit if you were doing it on a calculator that uses integer division. If your instructor has discussed mathematical operations (integer division in particular), you already have the required fundamentals. The only thing left is solving the problem and my hint gives a good push in the right direction.

  • #5
    New Coder
    Join Date
    Nov 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i appreciate your help but i wasn't looking for you or anyone else to do my homework for me. What i was asking for was possibly an example of a simple 3 line code that illustrates how to grab each individual integer and i could build on that.

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That is the assignment. If someone gives that, there's nothing to build upon. Your exercise is done. If you haven't pulled out your calculator yet, you're burning time on this exercise when you could be gaming or chasing girls (or guys, whatever your preference).

    I'm not trying to be a jerk. Figuring out the way to achieve the solution is always the hardest part of the job. Monkeys can write the code once that part is done. That's why these problems are assigned - to determine if the student can think his way through a problem. If not, he changes majors to marketing or something.

  • #7
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Read up on how to use the modulus operator you can use that to achieve the objective of that program. (There are multiple ways of doing it however, that's just one.)
    OracleGuy

  • #8
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    Try applying this little program:

    Code:
    #include <iostream>
    
    int main() {
      int Num = 34;
      int Out = 34 % 10;
      
      cout << Out;
      // Output's 4.
    }
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #9
    New Coder
    Join Date
    Nov 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i know what that is, but how exactly could that help me output all individual integers then add them up? Im not trying to find out if they are even...im kind of lost at this point

  • #10
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by edbtzy View Post
    i know what that is, but how exactly could that help me output all individual integers then add them up? Im not trying to find out if they are even...im kind of lost at this point
    Determining if an number is even or odd is just the most common use of the modulus operator, it can do much more than that as BWiz demonstrated. Perhaps adding some sort of loop using that code might do what you want?
    OracleGuy

  • #11
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    I'm pretty new to C++ programming, but you could declare different variables, then by using the mod operator, assign each variable to that specific value. IE, the variable int TensPos would correspond to the tenth unit in the number, which with the mod tool actually is %100.

    Just an idea. If anybody has any better ideas, I'd be glad to hear them.
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #12
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by BWiz View Post
    I'm pretty new to C++ programming, but you could declare different variables, then by using the mod operator, assign each variable to that specific value. IE, the variable int TensPos would correspond to the tenth unit in the number, which with the mod tool actually is %100.

    Just an idea. If anybody has any better ideas, I'd be glad to hear them.
    To figure out the sum, you would only need one variable.
    OracleGuy

  • #13
    New Coder
    Join Date
    Nov 2008
    Location
    Australia
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    if you want a more 'cheap' method of what Bwiz was saying you could try:
    with int a being 37 for example (note this is semi-bad coding but if it is an assignment it should do you just fine
    Code:
    int a== int a/10
    std::cout << a;
    output is 3.
    Just in case you are wondering, the following works because, unless you explicitly tell it to do so, C++ does not reckognise decimal number. For example it reads 3.7 as being the same number as 3. So if you were to apply this to each of the columns (1's, 10's etc) and then have a function to add each integer together, that should yield the result you seek.
    Hope I was of any assistence (btw if you need any other help Msg me, i have done something similar to this in the past)

    Jmac

    p.s To all the experienced coders out there, I know I have used some very poor coding in the above example, but considering that it is a school project, I'm sure it will not matter too much =)

  • #14
    New Coder
    Join Date
    Nov 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I finally came up with a code that will do what is needed..I'll post the code shortly once I get back to my house then we could work from it...I created a while loop with a module operator to extract the digits then a short code to sum them up...thanks for all help and suggestions btw

  • #15
    New Coder
    Join Date
    Nov 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is the code that i came up with:


    #include "stdafx.h"
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    int nextdigit;
    int value;
    int sum=0;

    cout<<"Enter Integer: ";
    cin>>value;
    cout<<endl;

    while (value != 0)
    {
    nextdigit=value%10;
    sum=sum+nextdigit;
    cout<<nextdigit<<" ";

    value=value/10;

    }

    cout<<"Sum= "<<sum<<endl;

    return 0;
    }
    when i run it, everything works fine...but when i input an integer like 12345678987654321, the program outputs 0 -6 -4 -3 and so on...but other then that if i insert an integer like 1234 it all works well....
    can somebody look over the code and tell me the reason why im getting something different when i input an integer like how i showed before??


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •