Lowest score drop assistance.

Here's my coding for this, I'm having trouble getting everything declared.

#include <iostream>
using namespace std;

double getScore(double &);
double findLowest(double, double, double, double, double);
double calcAverage(double, double, double, double, double);
bool isValid(double theScore, double maxValue);

int main() {
cout << "Please enter the 5 test scores \n";

double s1, s2, s3, s4, s5;
getScore(s1);
getScore(s2);
getScore(s3);
getScore(s4);
getScore(s5);

cout << "The test average is " << average << "!";

return 0;
}


double getScore(double & d) {
cout << "Enter a test score: ";
cin >> d;
if (result = false){
cout << "Invalid number, please enter a new one.";
cin >> d;

}
}

double findLowest(double s1, double s2, double s3, double s4, double s5){

double lowest = s1;

if (s2 < lowest)
lowest = s2;
else if (s3 < lowest)
lowest = s3;
else if (s4 < lowest)
lowest = s4;
else if (s5 < lowest)
lowest = s5;
}


bool isValid(double theScore, double maxValue){
bool result;
if (theScore >= 0 && theScore <= maxValue)
result = true;
else result = false;
return result;
}

double calcAverage(double s1, double s2, double s3, double s4, double s5){
double calcAverage;
average = (s1 + s2 + s3 + s4 + s5 - lowest )/3;
}






I'm getting the following errors on this.

main.cpp: In function ‘int main()’:
main.cpp:25:36: error: ‘average’ was not declared in this scope
cout << "The test average is " << average << "!";
^
main.cpp: In function ‘double getScore(double&)’:
main.cpp:34:6: error: ‘result’ was not declared in this scope
if (result = false){
^
main.cpp: In function ‘double calcAverage(double, double, double, double, double)’:
main.cpp:66:2: error: ‘average’ was not declared in this scope
average = (s1 + s2 + s3 + s4 + s5 - lowest )/3;
^
main.cpp:66:38: error: ‘lowest’ was not declared in this scope
average = (s1 + s2 + s3 + s4 + s5 - lowest )/3;
main.cpp: In function ‘int main()’:
main.cpp:25:36: error: ‘average’ was not declared in this scope

Look at your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

double getScore(double &);
double findLowest(double, double, double, double, double);
double calcAverage(double, double, double, double, double);
bool isValid(double theScore, double maxValue);

int main() {
  cout << "Please enter the 5 test scores \n";

  double s1, s2, s3, s4, s5;
  getScore(s1);
  getScore(s2);
  getScore(s3);
  getScore(s4);
  getScore(s5);

Does that explain what the "average" is?


The same issue: you use "result" that you have not introduced:
1
2
3
4
5
6
7
8
double getScore(double & d) {
  cout << "Enter a test score: ";
  cin >> d;
  if (result = false) {
    cout << "Invalid number, please enter a new one.";
    cin >> d;
  }
}

Furthermore, you assign "false" to the "result".
That is different from equality comparison: result == false

Last, the function promises to return a double value:
double getScore(double & d)
However, there is no return statement in the body of the function.


Have you had arrays yet?
1
2
3
4
5
6
7
int main() {
  constexpr int Size = 5;
  cout << "Please enter the " << Size << " test scores \n";
  double s[Size] {};
  for ( int i=0; i<Size; ++i ) {
    getScore( s[i] );
  }
We have not done arrays in this class yet
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
#include <iostream>
using namespace std;

double getScore( double maxScore );
double lowest( double, double, double, double, double );
double average( double, double, double, double, double );
bool isValid( double theScore, double maxValue ) ;

int main()
{
    const double MAX_SCORE = 100 ;

    cout << "Please enter the 5 test scores \n";
    const double s1 = getScore(MAX_SCORE) ;
    const double s2 = getScore(MAX_SCORE) ;
    const double s3 = getScore(MAX_SCORE) ;
    const double s4 = getScore(MAX_SCORE) ;
    const double s5 = getScore(MAX_SCORE) ;

    cout << "average of the four higher scores: "
         << average( s1, s2, s3, s4, s5 ) << '\n' ;
}

double getScore( double maxScore )
{
    double score ;
    cout << "Enter a test score: ";
    cin >> score ; // we assume hat the user enters a number

    if( isValid( score, maxScore ) ) return score ;

    cout << "Invalid number, please enter a new one.";
    return getScore(maxScore) ;
}

double lower_of( double a, double b ) { return a<b ? a : b ; }

double lowest( double s1, double s2, double s3, double s4, double s5 )
{ return lower_of( s1, lower_of( s2, lower_of( s3, lower_of(s4,s5) ) ) ) ; }

bool isValid( double theScore, double maxValue )
{ return theScore >= 0 && theScore <= maxValue ; }

double average( double s1, double s2, double s3, double s4, double s5 )
{
    const double lowest_score = lowest(s1,s2,s3,s4,s5) ;
    return ( s1+s2+s3+s4+s5 - lowest_score ) / 4 ;
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <valarray>
using namespace std;

int main()
{
   const int N = 5;
   valarray<double> score( N );
   cout << "Enter " << N << " test scores: ";
   for ( int i = 0; i < N; i++ ) cin >> score[i];
   cout << "Average of the " << N - 1 << " highest is " << ( score.sum() - score.min() ) / ( N - 1);
}
Last edited on
Nice, sure, alas:
We have not done arrays in this class yet

So I suggest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    unsigned int p(0), q(0), r(0), s(0);
    r -= 1;     // 0 - 1 = max if unsigned
    for (p = 1; p <= 5; p++)
    {
        cout << "Enter score " << p << ": ";
        cin >> q;
        s += q;
        if (q < r) r = q;
    }
    cout << "Lowest score droped is " << r << "\n";
    cout << "Average of the remaining scores is " << ( s - r ) / 4.;
}

In this simplification a little semantic glitch, "Lowest score droped" does not show the number but its value.
Topic archived. No new replies allowed.