Need help with calling average from function

thanks
Last edited on
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
#include <fstream>

const int NUM_SCORES = 5 ;
const int MIN_SCORE = 0 ;
const int MAX_SCORE = 100 ;

std::string get_name() // return the name entered by the user
{
    std::string name ;

    std::cout << "name: " ;

    while( name.empty() ) // skip over any empty input lines
        std::getline( std::cin, name ) ;

    return name ;
}

int get_score() // return the score entered by the user
{
    int score ;
    std::cout << "enter score [" << MIN_SCORE << ',' << MAX_SCORE << "]: " ;

    if( std::cin >> score ) // if the user entered an integer
    {
        // if it is in valid range, return it
        if( score >= MIN_SCORE && score <= MAX_SCORE ) return score ;

        else std::cout << "out of range. try again\n" ;
    }

    else // the user did not enter a number
    {
        std::cout << "error: invalid input. try again\n" ;

        // handle the input error

        // https://en.cppreference.com/w/cpp/io/basic_ios/clear
        std::cin.clear() ; // clear the error state of stdin

        // https://en.cppreference.com/w/cpp/io/basic_istream/ignore
        std::cin.ignore( 1000, '\n' ) ; // throw the bad input line away
    }

    return get_score() ; // try again
}

// calculate and return the average
// invariant: array holds NUM_SCORES valid scores
double calc_average( const int scores[] ) // note: const
{
    // static_assert( NUM_SCORES > 0 ) ; // sanity check

    // add up the scores
    double total = 0 ;
    for( int i = 0 ; i < NUM_SCORES ; ++i ) total += scores[i] ;

    // compute the average and return it
    return total / NUM_SCORES ;
}

int main()
{
    // declare arrays of names and scores
    std::string names[NUM_SCORES] ;
    int scores[NUM_SCORES] {} ; // , initialised to all zeroes

    //set names and score from user input
    for( int i = 0 ; i < NUM_SCORES ; ++i )
    {
        names[i] = get_name() ;
        scores[i] = get_score() ;
    }

    // open and write the scores and average to a file

    const char file_name[] = "average.txt" ;
    std::ofstream file(file_name) ; // open the file for output

    if( file.is_open() ) // if the file was successfully opened
    {
        // write the names and scores
        for( int i = 0 ; i < NUM_SCORES ; ++i )
            file << "name: " << names[i] << "   score: " << scores[i] << '\n' ;

        //calculate and  write the average
        const double average = calc_average(scores) ;
        file << "\n--------------\naverage score: " << average << '\n' ;
    }

    else std::cout << "error: failed to open output file\n" ;
}
Is there a way to keep the functions as "void calcAverage" and "void getScore" and then call the function in main?
Last edited on
You have to pass information between the functions.

1
2
3
4
double calc_average( const int scores[] );

const double average = calc_average( scores );
file << average;

This version is very simple: input data goes into function, function computes average, and returns the result.

1
2
3
4
5
void calc_average( const int scores[], double& result );

const double average {};
calc_average( scores, average );
file << average;

This version is almost the same: input data goes into function, function computes average, and result is returned via by reference parameter.

1
2
3
void calc_and_write_average( const int scores[], std::ostream& out );

calc_and_write_average( scores, file );

This does not give the average to the main(). The main calls the function with array and stream.
The function performs two tasks:
1. Computes average.
2. Writes the result to a stream.
Topic archived. No new replies allowed.