Using a 'while loop'

Hi there! I'm supposed to work in this

while (score < 0 || score > 100)
{
...
}


in the function void getTestScore(int& score) but I'm not quite sure how to do it. If while loop returns false then its suppose to cout "Invalid Entry" under the prompt then ask again for an entry. This is supposed to continue until it receives a valid entry - a number between 0 and 100 - and when it finally gets three valid entries it completes the run. Everything else is fine it's just this part really.


void getTestScore(int& score) {
cout << CALL;
cin >> score;


Still, just in case I have included the rest of my code as a reference. Again, just give me hints on what to do please, if possible don't directly give me the answer. Thanks for the help!

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
//Pre-processor Directive
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//Constant Variables
const int NUM_TESTS = 2;
const string LINE = "----------------------------------------------------------------\n";
const string CALL = "Enter a test score between 0 and 100: ";
//Function prototypes
void getTestScore(int&);
void calcAvgAndDisplayResult(int, int, int); //5b
int findAndReturnLowest(int, int, int);//5ci

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int main()
{
	//Variables
	int score1, score2, score3;

	//Function Calls

	getTestScore(score1);//5ai
	getTestScore(score2);//5ai
	getTestScore(score3);//5ai
	cout << LINE;
	calcAvgAndDisplayResult(score1, score2, score3); //5bi
	cout << LINE;

	system("pause");
	return 0;
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void getTestScore(int& score) {
	cout << CALL;
	cin >> score;
}
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void calcAvgAndDisplayResult(int score1, int score2, int score3)
{
	//Get lowest score.
	int lowest = findAndReturnLowest(score1, score2, score3);//5cii
	cout << "Lowest test score: "<< lowest <<endl;
	float average = static_cast <float>(score1 + score2 + score3 - lowest) / NUM_TESTS;
    cout << "Average test score (after dropping the lowest score) = " << average<<endl;
    

}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
The findAndReturnLowest function finds and returns
the lowest of the three scores passed to it
*/
int findAndReturnLowest(int score1, int score2, int score3)
{
	int lowest = score1;

	//Determine lowest score
	if (score2 <= score1 && score2 <= score3)
	    lowest = score2;

	if (score3 <= score1 && score3 <= score2)
         lowest = score3;
         
    return lowest;
	


}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
#include <iostream>

int getTestScore( int min_value, int max_value )
{
    std::cout << "Enter a test score between "
              << min_value << " and " << max_value << ": " ;

    int score ;
    if( std::cin >> score ) // if the user entered an integer
    {
        if( score >= min_value && score <= max_value ) // if it is within the valid range
            return score ; // return it

        std::cout << "the value " << score << " is out of range.\n" ;
    }

    else // invalid input: not an integer eg. the user entered 'abcd'
    {
        std::cout << "please enter an integer value for the score\n" ;
        std::cin.clear() ; // clear the failed state of the stream
        std::cin.ignore( 1000, '\n' ) ; // and throw this bad input line away
    }

    return getTestScore( min_value, max_value ) ; // try again
}

int main()
{
    const int min_score = 0 ;
    const int max_score = 100 ;

    const int score = getTestScore( min_score, max_score ) ;
    std::cout << "the score is " << score << '\n' ;
}
Thanks for that man but our teacher wants us to specifically use the while loop. Trust me, if I could use an If/else loop I would. Thanks though.
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
#include <iostream>

int getTestScore( int min_value, int max_value )
{
    int score = 0 ;
    bool got_a_valid_score = false ;

    while( !got_a_valid_score ) // keep looping till we get a valid score
    {
        std::cout << "Enter a test score between "
                  << min_value << " and " << max_value << ": " ;

        if( std::cin >> score ) // if the user entered an integer
        {
            if( score >= min_value && score <= max_value ) // if it is within the valid range
                got_a_valid_score = true ; // we have got a valid score

            else std::cout << "the value " << score << " is out of range.\n" ;
        }

        else // invalid input: not an integer eg. the user entered 'abcd'
        {
            std::cout << "please enter an integer value for the score\n" ;
            std::cin.clear() ; // clear the failed state of the stream
            std::cin.ignore( 1000, '\n' ) ; // and throw this bad input line away
        }
    }

    return score ; // we have a valid score when we come out of the loop, return it
}

int main()
{
    const int min_score = 0 ;
    const int max_score = 100 ;

    const int score = getTestScore( min_score, max_score ) ;
    std::cout << "the score is " << score << '\n' ;
}
If I could type in a thumbs up I would. Honestly, man, you are the best, the greatest. Honestly, speaking from heart, you are the bee's knees! Thanks, @JLBorges
@Bluegirl64, a tigthter implementing of your findAndReturnLowest() function could be:
1
2
3
4
5
6
7
8
9
10
11
12
int findAndReturnLowest(int score1, int score2, int score3)
{
        int lowest = score1;

        if (score2 < lowest)
                lowest = score2;

        if (score3 < lowest)
                lowest = score3;
         
        return lowest;
}
Topic archived. No new replies allowed.