Brand new to here, asking for help/advice

So as the title says, I am new so hello to all. I am in a programming class and would like some help. I am having to do a project about using functions, but I am not understanding a few things.


The program will need to calculate the avg of a group of 5 test scores, where the lowest score will be dropped. Will need to include a function named "void getScore()" which will ask the user for a test score, store it in a reference parameter variable, and validate that it is not lower than 0 or higher than 100. The function will be called in for each of the 5 scores. The second function will be called "void calcAverage()" and should calculate then display the average of the 4 highest scores. The final function will be called "int findLowest()" and should find and return the lowest of the 5 scores passed to it. It should be called by calcAverage, which uses the function to determine which one of the 5 scores to drop.

I am completely lost on this to be honest. Any help will be appreciated. The code below is what I have, and I am stuck with an error. Using Visual Studios 2013 ultimate with patch 5.

#include <iostream>
using namespace std;

//Prototypes
void getScore(int);
void calcAverage(int);
int findLowest(int);

int main()
{
int score1, score2, score3, score4, score5;

score1 = getScore();

system("pause");
return 0;
}

void getScore(int score)
{
cout << "Enter test score." << endl;
cin >> score;

/* if ((score < 0) || (score > 100))
{
cout << "That is not a valid score." << endl;
}*/

}
Last edited on
Why are you completely lost? Are you paying for this programming class?
As a start you might look at storing your five scores in an array.
Completely lost as score1 = getScore() ends up giving me an error that I am trying to change a void function to int. Also, we are required to do it per instructions, so I can't use arrays (though we haven't gotten to those yet). Yes I am paying for it? It's a college course, but I was out sick during class, and haven't been able to get ahold of the professor.
Completely lost as score1 = getScore() ends up giving me an error that I am trying to change a void function to int.


That's not the error you're getting.

getScore has a void return type, which means it returns nothing. Since the function returns nothing, it doesn't make sense to assign its (nonexistent) return value to score1.

I would imagine that getScore should either return a value (in which case it's parameter list should probably be empty) or take an argument by reference. Reading the OP more carefully, I see that the second tact should be taken.
If you can't use arrays you would still have to loop 5 times to get each value:
1
2
3
4
5
6
7
for (int i=0; i<5; i++)
{
    score = getScore();
    sum = sum + score;   // keep running total to work out average
    if (score < min)
        min = score;  // save lowest value
}


otherwise you would have to repeat it five times.
1
2
3
    score1 = getScore();
    score2 = getScore();
    ...


Makes no sense not to use a list of numbers.
When you learn arrays it would be more like 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

//Prototypes
void getScores();
float calcAverage();
int findLowest();
void showScores();

int scores[5];  //global variable to all functions

int main()
{

    getScores();
    showScores();

    //system("pause");
    return 0;
}

void getScores()
{
    for (int i=0; i<5; i++)
    {
        cout <<  endl << "Enter next test score: ";
        cin >> scores[i];
        while ( (scores[i] <0) || (scores[i] > 100))
        {
             cout << endl << "invalid value, try again 0 to 100" << endl;
             cin >> scores[i];
        }
    }
    cout << endl;
}

void showScores()
{
    for (int i=0; i<5; i++)
    {
        cout << "scores[" << i << "] =" << scores[i] << endl;
    }
}


I'm trying to really understand the OP right now, but I highly, highly doubt that any professor or any programmer worth his two cents would make a "get" function void type. I'm going to assume a few things here @OP, since I'm not seeing it in the code he provided us.

1) You're not allowed to use loop controls, meaning no try/catch, no do/while, no while, and no for. Only if,then,else

2) You're not allowed to use vectors or arrays or any other data type that contains multiple objects within itself (i.e. string)

I provided you with a working answer, but by no means should you actually try to use my code letter for letter; instead try and think about why things work. Also my solution is really ugly so for the rest of you, please don't yell at me :3

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
#include<iostream>

using namespace std;

int getScore( int &scores );
void calcAverage( float &total );
int findLowest( int &score1, int &score2, int &score3, int &score4, int &score5 );

const unsigned int NUMBER_OF_HIGH_SCORES = 4; // keyword unsigned just means the number is guaranteed to not be negative;

int main( void )
{
    int score1, score2, score3, score4, score5;
    
    cout << "Enter 5 scores, each not less than 0 and not greater than 100:" << endl;
    float scoreTotal = getScore( score1 ) + getScore( score2 ) + getScore( score3 ) + getScore( score4 ) + getScore( score5 );
    scoreTotal -= findLowest( score1, score2, score3, score4, score5 );
    cout << "The lowest Score is " << findLowest( score1, score2, score3, score4, score5 ) << endl;
    calcAverage( scoreTotal );
    
    // pause the console however you want here. however I would not recommend system("pause"). 
    // basically system("pause") is like using a flamethrower to try and kill one mosquito
    
    return 0;
}

int getScore( int &scores )
{ 
    cin >> scores;
    if ( scores < 0 || scores > 100 )
    {
        cout << "Error: scores not within bounds. Try again:" << endl;
        int faultyInput = scores;
        scores -= faultyInput;
        cin >> scores;
    }
    return scores;
}

void calcAverage( float &total )
{
    float average = total / NUMBER_OF_HIGH_SCORES;
    cout << "Average is " << average << endl;
}

int findLowest( int &score1, int &score2 ,int &score3 ,int &score4 ,int &score5 )
{
    int lowestScore = score1;
    if ( lowestScore > score2 && score2 < score3 && score2 < score4 && score2 < score5 )
        lowestScore = score2;
    else if ( lowestScore > score3 && score3 < score2 && score3 < score4 && score3 < score5 )
        lowestScore = score3;
    else if ( lowestScore > score4 && score4 < score2 && score4 < score3 && score4 < score5 )
        lowestScore = score4;
    else if ( lowestScore > score5 && score5 < score2 && score5 < score3 && score5 < score4 )
        lowestScore = score5;
    
    return lowestScore;
}


FYI use code tags next time. [code][/code]
Last edited on
Okay, so thank you to all. I'm sorry if it wasn't maybe worded the best. What was messing me up earlier was trying to assign the wrong properties to an int. Cire was correct, as I was supposed to be passing by parameter. Again, I had been sick and missed class, and I didn't have full access to notes. Almost the final version, just need to add the int findLowest function:

#include <iostream>
using namespace std;


//Function prototypes
void getScore(int &);
void calcAverage(int &, int &, int &, int &, int &);
int findLowest(int, int, int, int, int);


int main()
{
int score1, score2, score3, score4, score5;

getScore(score1);
getScore(score2);
getScore(score3);
getScore(score4);
getScore(score5);

calcAverage(score1, score2, score3, score4, score5);


system("pause");
return 0;
}

void getScore(int &score)
{
cout << "Enter test score." << endl;
cin >> score;

if ((score >= 0) || (score <= 100))
{
cout << "You entered " << score << "." << endl;
}
if ((score < 0) || (score > 100))
{
cout << "You entered an invalid score." << endl;
cin >> score;
}

}

void calcAverage(int &score1, int &score2, int &score3, int &score4, int &score5)
{
int average, lowest;

average = (score1 + score2 + score3 + score4 + score5);

lowest = findLowest(score1, score2, score3, score4, score5);

average = (average - lowest) / 4;

cout << "Your average is " << average << "." << endl;
}
Topic archived. No new replies allowed.