Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete C++ program to input 3 integer test scores, and output the real

ID: 3885303 • Letter: W

Question

Write a complete C++ program to input 3 integer test scores, and output the real average. For example, if the test scores are 90, 79, and 88, the output from your program should be

followed by a newline. However, it is possible that one or more of the scores is invalid, in which case the program should output "invalid data!" followed by a newline. Note that it is possible for multiple scores to be invalid --- your program should output "invalid data!" exactly once. A score is invalid if it is less than 0 or greater than 100. [ When working with data, it is common practice to validate data before performing computations. ]

Explanation / Answer

Code

#include <iostream>

using namespace std;

int main() {

int current, numOfElements=0;

double sum = 0;

while (cin.peek() != ' ') {

cin >> current;

if(!cin.good()){ // Checks if integer or not

cout << "invalid data" << endl;

exit(0); // To print invalid data only once

}

if(current < 0 || current > 100){

cout << "invalid data" << endl;

exit(0); // To print invalid data only once

}

numOfElements++;

sum += current;

}

cout << "Average: " << (sum/numOfElements) << endl;

}

Output

1. One invalid input

Input: 90 a 79 88

<newline>

Output: invalid data

<newline>

2. Multiple invalid inputs

Input: 90 a b 79 88

<newline>

Output: invalid data

<newline>

3. Out-of-range integer: invalid input

Input: 90 179 88

<newline>

Output: invalid data

<newline>

4. Valid input

Input: 90 79 88

<newline>

Output:

Average: 85.6667

<newline>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote