Function help for a begginner

Started by
19 comments, last by Auron 19 years, 6 months ago
Ok,here's my question...

How can I add 3 integers if only 1 variable reads them?

This is probably so simple, I'll go d'oh! wont I?

edit:

WAIT A MINUTE! D'OH! INDEED... this could be a callByReferance couldnt it??

easiest. program. ever.

btw, am i going on the right track?
Advertisement
You could use the += operator.

For instance,

x = 1;

x+=2; // x now equals 3

x+= 5; // x now equals 8

Edit:

Hrm... you would still need to use 2 variables in this case if you plan on getting input from the user.
new code...

#include <iostream>using std::cout;using std::cin;int main(){	int num;	int count;	int plus;		int sumByReferance( int &);	cout << "Please input three integers: ";	cin >> num; 	for( count = 1; count < 3; count++)	{		cin >> num;		plus = num;		sumByReferance( plus );	}cout << "\nThe sum of the three integers is " << sumByReferance( plus ) ;return 0; }


The output is 56?

discuss ;p
Quote:Original post by Clover_Maximus
new code...

*** Source Snippet Removed ***

The output is 56?

discuss ;p

you just said it was 28 did you change your code?
maybe its random?[smile]
-----------------------------------Panic and anxiety Disorder HQ
ok....heres the new code again....

#include <iostream>using std::cout;using std::cin;int main(){	int num;	int count;	int plus;		int sum( int );	cout << "Please input three integers: ";	cin >> num; 	for( count = 1; count < 3; count++)	{		cin >> num;		plus = num;		sum( plus );	}cout << "\nThe sum of the three integers is \n" << sum( plus ) ;return 0; }


My input is 13, 27, 14. If you input in this ( 13, 14, 27) exact order you get 54. Any other order and it becomes wierd. Wierd as in not right.
[/source]
Forgive my bouncing around everywhere. Im trying new things as I recall them.

thanks ontheheap for the operator idea. duh is all i can say about myself.

hothead. thanks for your input. wasnt what i was looking for, but at least you helped. thanks dude.

Thanks again guys. This is a great help
For those of you keeping score, I got it to work. OH FREAKIN YEAH!!!

Here it is

#include <iostream>using std::cout;using std::cin;int main(){	int num;	int count;	int plus;		int sum( int, int );	cout << "Please input three integers: ";	cin >> num; 	plus = num;	for( count = 1; count < 3; count++)	{		cin >> num;				plus = sum( plus, num );	}cout << "\nThe sum of the three integers is " << plus ;return 0; }


Thanks to all of you guys that helped. It may sound like serious ass kissing (which it is by the way) but seriously for those that helped, much thanks. One fucntion down, 4 more to go. :):):):)

#include iostreamusing namespace std;int main(){ int sum = 0; int num = 0; cout << "Please enter three integers: "; for (int i = 0; i < 3; i++) {  cin >> num;  sum += num; } cout << "\nYour sum is: " << sum << "\n"; return 0;}


that to me just seemed much easier then everything else...
yeah,

x = sum(x, y);

is, by definition, the same as:

x += y;

for a quick reminder, look at the following expressions - they all have the same result (IN THIS CASE), but 3 of them are exactly equivelent, while the other is not.

1. x = x + 1
2. x += 1
3. ++x
4. x++

1, 2, and 3 are exactly the same - each increases the value in x by 1, AND returns the new value of x.

4 increases the value of x by 1, but returns the OLD value of x.

in your domain:

x = sum(x, y);
x = x + y;
x += y;

are all identical
ah jeez, i knew it was simple. Then there really isnt a need for a function in that case....


What if I wanted to take the average of those three numbers?

The way I have it doesnt seem to work....more at eleven

This topic is closed to new replies.

Advertisement