Back to functions

Hi, another newbie here. I thought I had a basic grasp on functions and was attempting to move on to arrays when I ran across an issue I can't figure out.
Here is the source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Learning Arrays

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

double input1,input2,input3,input4,input5;
double addfunc ()
    {

        return input1 + input2 + input3 + input4 + input5;
    }
int main ()
{
    
    double myArray [] = {input1,input2,input3,input4,input5};
    for(double i = 0.0; i < 5; i++)
    {
            cout << " Enter a number: ";
            double input;
            cin >> input;
            
            
    }
    
    
    cout << "The sum of these numbers is: ";
    double sumcall = addfunc ();
    cout << sumcall << endl;
    
    
    system("PAUSE");
    return 0;
}


I can't figure out why addfunc() is returning 0. Any help would be appreciated. Any other problems you can see here feel free to point out my mistakes. Thanks for your time.
double myArray [] = {input1,input2,input3,input4,input5};

These are all uninitalized when you do this.

1
2
3
4
5
6
7
8
for(double i = 0.0; i < 5; i++)
    {
            cout << " Enter a number: ";
            double input;
            cin >> input;
            
            
    }


Not sure what you're trying to do here. I'm assuming you're trying to add these into your array? If so, you're doing that wrong. All this is doing is taking some user input, and storing it into the variable, input, every time. So each iteration you're changing this variable. Also, using a double as your loop counter is odd and not recommended. Should change that to an int.
closed account (zb0S216C)
A few things to remember:

1) Global objects (variables) are considered bad practice. Instead, pass each object by value to the addfunc() function.

2) Your global objects are redundant. The array you declared in main() would suffice.
3) In your for loop, i should be an integral type, such as int. Why? Because of floating-point rounding issues.
4) Your for loop is useless. It requests input and does nothing with it.
5) sumcall is redundant. You can simply invoke addfunc() in this fashion: std::cout << addfunc();

Wazzak
Okay. Thanks for the feedback and advice, it's all very helpful. Hopefully someday I'll be able to return the favor to another beginner. Thanks again.
Topic archived. No new replies allowed.