Write a function whose job is to input #\'s, non-# to quit, recover from the inp
ID: 3557677 • Letter: W
Question
Write a function whose job is to input #'s, non-# to quit, recover from the input failure by calling recover(), and tell main some statistics about the numbers read.
. unsigned readA();
Read unsigned #'s, non-# to quit, recover, return the sum of all #'s read.
For example, if main says
cout <<"The total is " <<readA() <<endl;
and if the user types
2 4 6 8 blah
then readA will read the numbers 2, 4, 6, 8, keeping track of the total of the numbers read, readA will then choke on the "blah", readA will then, call recover() so that cin recovers from the input failure and moves past the "blah", readA will then return the 20, then main will resume the cout statement so that it outputs
The total is 20
Explanation / Answer
Program Code:
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
#include <string>
using namespace std;
int main()
{
unsigned int a;
int sum=0;
cout<<"Enter the integers: "<<endl;
do
{
cin>>a;
if(!cin.fail())
sum+=a;
}
while(!cin.fail());
cout<<"Sum is: "<<sum<<endl;
system("pause");
return 0;
}
Sample Output:
Enter the integers:
2
4
6
8
blah
Sum is: 20
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.