473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Add numbers

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
Jul 19 '05 #1
4 6812
Tommy Lang wrote:
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.
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2

"Tommy Lang" <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
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

Jul 19 '05 #3
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

"Tommy Lang" <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
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

Jul 19 '05 #4

Tommy Lang <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
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

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
0
by: August1 | last post by:
This is a follow up to using these functions to produce lottery numbers to an outfile, then read the numbers from the file to the screen. Although other methods are certainly available. ...
0
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.