Public Group

# Function help for a begginner

This topic is 5455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm writing a program that will find the sum of three integers. I need to use a function. But instead of intializing three differant variables, I want to use a counter and intialize only one variable. I hope this makes sense. So far I have... int main() { int number; int num; int plus; int count; int sum( int x, int y, int z); cout << "Please input three integers: "; cin >> number; for( count = 1; count < 3; count++) { cin >> num; plus = sum( plus, plus, plus); return 0; } cout << "\nThe sum of the three integers is " << sum; int sum( int plus1, int plus2, int plus3) { return plus1 + plus2 + plus3 } So of course I get an error. "Local function definition is illegal" I have 3 questions. 1. How can I use only one variable to add three numbers? 2. Can you point me in the right direction to fix my "illegal definition"? 3. Is there a better way to post my code so it doesnt take up half the screen? Thanks in advance!

##### Share on other sites
oh yeah, by the way.

Yes this is my homework.

and I have it done using 3 variables.

Any input would be appreciated.

Thanks again.

##### Share on other sites
3. use [ source ] [ /source ] tags (without the added spaces).

2. move int sum() declaration outside of main

1. You could use an array

##### Share on other sites
try it like this
#include<iostream>    using namespace std;int main(){    int num,num2,num3,oput;    cout<<"enter a number\n";    cin>>num;    cout<<"another\n";    cin>>num2;    cout<<"another\n";    cin>>num3;    cout<<num<<"+"<<num<<"+"<<num3<<"="<<num+num2+num3<<endl;    cin>>oput;    return 0;}

##### Share on other sites
Holy cow, I thought i fixed it (thanks ontheap, btw not allowed to use arrays yet;P) but i get the following output

PLEASE INPUT THREE INTEGERS: 13
27
14

THE SUM OF THE THREE INTEGERS IS 004012DFplease press any key

That messes with my head...heres my new code...(thanks again heap)

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

(i think that worked)

##### Share on other sites
I don't think you meant to output "sum".

##### Share on other sites
There's quite a bit wrong with your code there.

Some of the errors I see are in your for loop (really no need to use one in this case, and the call to plus() is definitely not needed).

A function is simply another chunk of code in memory that the program jumps to when it encounters a function call (well, this isn't the case with inline functions, but you've probably not learned about this yet).

The point of your function is to return the sum of three ints passed to it. You need to take a look at your code and try and see why it doesn't work. If you get stuck just ask.

##### Share on other sites
Quote:
 Original post by ontheheapThere's quite a bit wrong with your code there.Some of the errors I see are in your for loop (really no need to use one in this case, and the call to plus() is definitely not needed).

I soooo thought I went crazy and forgot how to use C++ until you posted that. It seems that you have quite a bit to go over with this code...are you coming from a different language?

EDIT: also, you should try hothead's method. It's messy, but if I was going to do this, that's probably how I'd do it.

##### Share on other sites
Quote:
Original post by Boku San
Quote:
 Original post by ontheheapThere's quite a bit wrong with your code there.Some of the errors I see are in your for loop (really no need to use one in this case, and the call to plus() is definitely not needed).

I soooo thought I went crazy and forgot how to use C++ until you posted that. It seems that you have quite a bit to go over with this code...are you coming from a different language?

EDIT: also, you should try hothead's method. It's messy, but if I was going to do this, that's probably how I'd do it.

I did a double take when I saw it at first, lol.

##### Share on other sites
Well let me explain a little more of what the program will do. I need to find the smallest, largest, sum, product, and average of the 3 integers. I did this program before but I had a ton of code and im trying to shorten it down.

that is why Im using a the for loop. I declared a ton of variables the first time. I thought maybe I could streamline it by using a loop.

sneftl: i didnt notice that, thanks dude

update: I fixed the output (plus instead of sum) i know get a very large number...Wierd....

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 13
• 14
• 45