...

View Full Version : need help with c++ CODE PLEASE



edbtzy
11-05-2008, 08:00 PM
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.

tomws
11-05-2008, 08:47 PM
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.

edbtzy
11-05-2008, 08:54 PM
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???

tomws
11-05-2008, 09:08 PM
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.

edbtzy
11-05-2008, 09:17 PM
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.

tomws
11-05-2008, 09:34 PM
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.

oracleguy
11-05-2008, 11:35 PM
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.)

BWiz
11-06-2008, 12:48 AM
Try applying this little program:



#include <iostream>

int main() {
int Num = 34;
int Out = 34 % 10;

cout << Out;
// Output's 4.
}

edbtzy
11-06-2008, 12:50 AM
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

oracleguy
11-06-2008, 12:54 AM
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?

BWiz
11-06-2008, 06:48 AM
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.

oracleguy
11-06-2008, 07:08 AM
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.

jmac555
11-06-2008, 08:13 AM
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


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 =)

edbtzy
11-06-2008, 09:45 PM
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
11-07-2008, 01:56 AM
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??

oracleguy
11-07-2008, 03:40 AM
The reason your output is unexpected with the number: 12345678987654321 is that 12345678987654321 is too big to be stored in a 32-bit signed integer so the variable is overflowing.

edbtzy
11-07-2008, 06:51 AM
ooo i get it. Thats good to know. I thought i did something wrong in the program but so far everything is running good with that code. But now im just trying to find out a way to output the numbers in the order they were inputted without using a function/arrays.

jmac555
11-13-2008, 07:11 AM
This isn't an entire fix, but may i suggest that the problem may be that 12345678987654321 is outside that allowed value range for an int

(-32768 to 32767) so that may be the reason it doesn't work

(<edit> sorry didn't read previous, already been answered >.<)

primefalcon
11-13-2008, 09:26 AM
not sure how to delete so just exiting it out, nvm got my languages mixed up



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum