473,412 Members | 2,003 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,412 software developers and data experts.

int in C++ (newbie)

Help i try to built a function which is a sum of two integers
The result must be an integer !
Check the result...
Any help it 's welcome.
/************************************************** ********/
#include <iostream>

#include "math.h"
using namespace std;

int add_number(int a, int b, int c)
{
cout << "enter a" << endl;
cin >>a;
cout << "enter b "<< endl;
cin >> b;
c = a + b;
return (a, b, c);
}

int main ()
{
signed int a, b, c;

add_number(a, b, c);
cout << " the sum is :" << c << endl;
}
Oct 10 '06 #1
4 2157
First of all, when you're creating this function, since you're going to ask the user for the inputs, you don't need to include them in your function.

Also, you don't need to return all three values, only the resulting summation (c in your case)

i.e.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int addNumber()
  3. {
  4.    int a, b, c;
  5.  
  6.    cout << "enter a" << endl;
  7.    cin >>a;
  8.    cout << "enter b "<< endl;
  9.    cin >> b;
  10.    c = a + b;
  11.    return c;
  12. }
  13.  
  14. int main ()
  15. {
  16.    int sum;
  17.    sum = addNumber();
  18.  
  19.    cout << " the sum is :" << sum << endl;
  20. }
  21.  
  22.  

That should work - let me know if you have any questions. Thanks.
Oct 10 '06 #2
Many thanks. It' s fine!
I made some changes for me.


int addNumber(int a, int b)
{
int c;

cout << "enter a" << endl;
cin >>a;
cout << "enter b "<< endl;
cin >> b;
c = a + b;
return c;
}

int main ()
{
int x, y, sum;
sum = addNumber(x, y);

cout << " the sum is :" << sum << endl;
Oct 10 '06 #3
Many thanks. It' s fine!
I made some changes for me.


int addNumber(int a, int b)
{
int c;

cout << "enter a" << endl;
cin >>a;
cout << "enter b "<< endl;
cin >> b;
c = a + b;
return c;
}

int main ()
{
int x, y, sum;
sum = addNumber(x, y);

cout << " the sum is :" << sum << endl;
Glad that it worked for you.

FYI, however

Expand|Select|Wrap|Line Numbers
  1. nt  x, y, sum;
  2.    sum = addNumber(x, y);
  3.  
You don't need to declare x or y, or pass them to the function, as you are obtaining the values for those two numbers within the function itself. Declaring x and y, and then passing them to the function, doesn't actually do anything useful for the program.

Just a side note, glad I could help!
Oct 10 '06 #4
You can used this way:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. #include "math.h"
  4. using namespace std;
  5.  
  6. void add_number(int &a, int &b, int &c)
  7. {
  8.     cout << "enter a" << endl;
  9.     cin >>a;
  10.  
  11.     cout << "enter b "<< endl;
  12.     cin >> b;
  13.  
  14.     c = a + b;
  15.     // return (a, b, c);
  16. }
  17.  
  18. void main ()
  19. {
  20.     int a, b, c;
  21.  
  22.     add_number(a, b, c);
  23.  
  24.     cout << " the sum is :" << c << endl;
  25. }
  26.  
Oct 12 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Philippe C. Martin | last post by:
Hi, Is there a program out there that would generate the C code to instantiate objects and call them: ex: miracle.exe -i mymodule.py -o module_internface.c ? I seem to recall a _yes_ to...
0
by: Twayne | last post by:
Twayne wrote: That's the verification I was looking for I think. There are times that doesn't seem to be so, but perhaps I'll looking at the wrong side of the tree in the wrong forest ... I...
5
by: Banibrata Dutta | last post by:
Hi, I've gone through the list of "language differences" between 2.3 / 2.4 & 2.5 of CPython. I've spend around 2 weeks now, learning v2.5 of CPython, and I consider myself still very very...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
10
by: Peter Michaux | last post by:
On May 14, 8:55 pm, Prisoner at War <prisoner_at_...@yahoo.comwrote: Get it from the library. I cannot imagine needing to own an HTML book. There are plenty of good references on the web....
2
by: r_ahimsa_m | last post by:
Could you recommend me some free JavaScript validator? I was using JSlint but it reports nonsense errors. Please help. Thanks. /RAM/
6
by: raylopez99 | last post by:
Will ASP.NET 3.0 work under Visual Studio 2005? And what is a good newbie ASP.NET book? Subject: Will ASP.NET 3.0 work under Visual Studio 2005? And what is a good newbie ASP.NET book? My...
3
Lokean
by: Lokean | last post by:
Sorry for this newbie question, this is not my realm of expertese. I have searched google, tried several applications that claim they can do this, such as Mapforce, which I found confusing, to...
5
by: Dave | last post by:
I am new to Visual Web Developer 2005 Expres. I am using absolute positioning and every time I add a button control to my web form its width extends all the way to the edge of the page. IOW I...
5
by: SharkD | last post by:
Hi! I'm a total newbie when it comes to ASP (or any type of server-side programming). I want to start a project that does the following: 1. query Wikipedia for information regarding...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.