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.
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.
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???
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.
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.
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.
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.)
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
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?
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.
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.
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 =)
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
#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??