Question
1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately terminating the list reading when the sentinel value (lets
1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately terminating the list reading when the sentinel value (lets use -9999) is entered. The program should then correctly report the number of non-negative scores entered and the arithmetic mean (average) of those scores
2. Demonstrate your programs behavior in response to errors on input (that is, show that it faithfully rejects improper input such as alphabetic characters, decimal points, and commas). Here are some specific examples demonstrating the requirements of your program.
3. Pseudo-code:
Initialize a Sum and Counter to zero
Initialize a loop Done variable to false, or zero
Provide a brief console output to describe what the program will do
While not Done
Attempt to read the next Input, checking for an integer read failure
If this attempt to read an integer failed then
Clear the input stream status, then ignore the current character on the stream
Repeat the attempt to read an integer until successful
If the Input integer is the sentinel value
Set the loop Done variable to true, or nonzero
Else if the Input integer is non-negative then
Add one to the Counter
Add the Input to the Sum
Repeat until Done
Compute and display the number of inputs and the average (report 0 average for 0 inputs)
Example Output:
Ill compute the average of your inputs:
30 45 -9 one junk 100.0 more -1000.8 -9999
Results: 5 non-negative integers entered, average 36.6
// Comment: the non-negative integers read were (30, 45, 100, 0, 8)
Please work off of the code I have provided below.
#include
int main() { int a(123456789); const int NUM(4);
cout << " Test the stream, the clear() and ignore() "; cout << "Enter " << NUM << " integers: "; for (int i = 1; i <= NUM; i++) { while (!(cin >> a)) // While we have an error reading... { cin.clear(); // Remove the error state in stream cin.ignore(); // Read but ignore the next character } cout << "\t integer(" << i << ") was: " << a << endl; } system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started