Add numbers

T

Tommy Lang

Lets say you have a read (using cin >> ) a couple of numbers that the
user have entered ie. 1234. And you want to add these numbers together
like this, 1+2+3+4=10 How do you do that?

Thanks,

Tommy
 
K

Kevin Goodsell

Tommy said:
Lets say you have a read (using cin >> ) a couple of numbers that the
user have entered ie. 1234. And you want to add these numbers together
like this, 1+2+3+4=10 How do you do that?

Thanks,

Tommy

So, you are at this point:

#include <iostream>

int main()
{
int a, b;
std::cin >> a >> b;

return 0;
}

And now you want to add 'a' and 'b'? It's quite simple:

#include <iostream>

int main()
{
int a, b;
std::cin >> a >> b;

a + b; // add a and b

return 0;
}

Of course, the next logical step would be to *do something* with the
result. But you didn't say anything about that, so I don't know what you
want to do.

A few hints:

1) Be specific when you ask a question here. We're not good at guessing
what you actually wanted to ask.

2) This is about the most basic question I've ever seen on this group.
You really should not ask these kinds of questions here, for 3 main reasons:

* You can get an answer much more quickly from your C++ book.

* If you attempt to learn C++ by asking questions here, it will take you
approximately 2593 years (I did the math[1]).

* We can't be bothered to answer hundreds of basic questions that you
could have answered yourself in seconds just by picking up a book. It is
a waste of our time.

-Kevin

[1] I did not actually do any math.
 
H

Howard

Tommy Lang said:
Lets say you have a read (using cin >> ) a couple of numbers that the
user have entered ie. 1234. And you want to add these numbers together
like this, 1+2+3+4=10 How do you do that?

Thanks,

Tommy

Tommy,

that's a rather vague question. What exactly did you read that
information INTO? A string, as in "1234"? Four different integers, as in
1,2,3 and 4? Or one integer, as in 1234? You say they entered "a couple of
numbers", so it's not clear what you meant by 1234.

To add separate numberic variables, just add them. To add the digits of
a string, loop through the string, convert each character to an unsigned
char (i.e, convert "1" to the value 1), and add each converted value to a
running total. To add the "digits" of an integer value, first convert it to
a string and then do like I just described for a string.

-Howard
 
S

Snake

Hey Tommy,
I understood what you want.And its not a stupid question as others
said(assuming that what I undersood was what u want).You gonna use little
math to solve it.Look,lets rephrase the question again: the user enters a
number (without spaces) in a one command line and u wanna add the digits of
that number.In other words (cin>>b) where b was entered as 1234.
so here is the code u use to add them:

int add_digits(void){
int i=1,sum=0,digit=0,number_reduced,b;
cin>>b;
number_reduced=(b / i);
while (number_reduced !=0 ){
digit=number_reduced%10; // to put this digit in temp2
sum = sum + digit; //u could use the += too,I just always confuse += with =+
i*=10;
number_reduced=(b / i);// to keep the digit that we want in the last
place(notice that temp1 is INT)
}
cout <<sum;
getch();
}

This will basically solve the problem.if u have a question about the
code,let me know. but u can go to alt.comp.learn.c-c++ if u are beginner
where u can find answers to easy questions.

Have a good day
Bye
 
M

Mike Wahler

Tommy Lang said:
Lets say you have a read (using cin >> ) a couple of numbers that the
user have entered ie. 1234. And you want to add these numbers together
like this, 1+2+3+4=10 How do you do that?

While it's the the specific solution to your question,
I think the below illustrates what you need to know:

#include <iostream>
#include <string>

int main()
{
std::string s("12");
std::cout << s[0] - '0' + s[1] - '0' << '\n';
return 0;
}

Output:

3

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top