Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2008, 07:00 PM   PM User | #1
edbtzy
New Coder

 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
edbtzy is an unknown quantity at this point
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.
edbtzy is offline   Reply With Quote
Old 11-05-2008, 07:47 PM   PM User | #2
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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.
tomws is offline   Reply With Quote
Old 11-05-2008, 07:54 PM   PM User | #3
edbtzy
New Coder

 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
edbtzy is an unknown quantity at this point
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???
edbtzy is offline   Reply With Quote
Old 11-05-2008, 08:08 PM   PM User | #4
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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.
tomws is offline   Reply With Quote
Old 11-05-2008, 08:17 PM   PM User | #5
edbtzy
New Coder

 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
edbtzy is an unknown quantity at this point
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.
edbtzy is offline   Reply With Quote
Old 11-05-2008, 08:34 PM   PM User | #6
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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.
tomws is offline   Reply With Quote
Old 11-05-2008, 10:35 PM   PM User | #7
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,042
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
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
oracleguy is offline   Reply With Quote
Old 11-05-2008, 11:48 PM   PM User | #8
BWiz
Regular Coder

 
BWiz's Avatar
 
Join Date: Mar 2006
Location: Sol System
Posts: 471
Thanks: 7
Thanked 21 Times in 21 Posts
BWiz is an unknown quantity at this point
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.
BWiz is offline   Reply With Quote
Old 11-05-2008, 11:50 PM   PM User | #9
edbtzy
New Coder

 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
edbtzy is an unknown quantity at this point
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
edbtzy is offline   Reply With Quote
Old 11-05-2008, 11:54 PM   PM User | #10
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,042
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
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
oracleguy is offline   Reply With Quote
Old 11-06-2008, 05:48 AM   PM User | #11
BWiz
Regular Coder

 
BWiz's Avatar
 
Join Date: Mar 2006
Location: Sol System
Posts: 471
Thanks: 7
Thanked 21 Times in 21 Posts
BWiz is an unknown quantity at this point
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.
BWiz is offline   Reply With Quote
Old 11-06-2008, 06:08 AM   PM User | #12
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,042
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
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
oracleguy is offline   Reply With Quote
Old 11-06-2008, 07:13 AM   PM User | #13
jmac555
New Coder

 
Join Date: Nov 2008
Location: Australia
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
jmac555 is an unknown quantity at this point
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 =)
jmac555 is offline   Reply With Quote
Old 11-06-2008, 08:45 PM   PM User | #14
edbtzy
New Coder

 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
edbtzy is an unknown quantity at this point
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
edbtzy is offline   Reply With Quote
Old 11-07-2008, 12:56 AM   PM User | #15
edbtzy
New Coder

 
Join Date: Nov 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
edbtzy is an unknown quantity at this point
This is the code that i came up with:

Quote:

#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??
edbtzy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:08 AM.


Advertisement
Log in to turn off these ads.