Script broke.

Any one help me fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//The following calculates the average of three numbers.

#include <iostream>
#include <cstdio>
#include <cstdlib>

float val1, val2, val3, valSum;

int calculate ()
{
    valSum = val1 * val2 * val3 / 3;
}

int main ()
{
    std::cout << "Please enter your tree numbers: ";
    cin >> val1 >> val2 >> val3;
    
    switch (calculate);
    
    cout << "The average is: " << valSum;
    
    return 0;
}
Your switch statement doesn't do anything, and that is NOT how you calculate an average.
Read about how to properly make and call a function here http://www.cplusplus.com/doc/tutorial/functions/
Also your use of global variables breaks any encapsulation that the function would otherwise provide, which by the way should be returning an int based on your calculate function's signature.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

//The following calculates the average of three numbers.
float calculate (float v1, float v2, float v3)
{
    float average = ( v1 + v2 + v3 ) / 3;
    return average;
}

int main ()
{
    float val1, val2, val3, avg;
    
    std::cout << "Please enter your three numbers: ";
    std::cin >> val1 >> val2 >> val3;
    
    avg = calculate( val1, val2, val3 );
    
    std::cout << "The average is: " << avg;
    
    return 0;
}
Topic archived. No new replies allowed.