In: Computer Science
Write a C++ program that continuously requests a donation to be entered. If the donation is less than 0 or greater than 10, your program should print an appropriate message informing the user that an invalid donation has been entered, else the donation should be added to a total. When a donation of -1 is entered, the program should exit the repetition loop and compute and display the average of the valid donations entered.
#include <iostream>
using namespace std;
int main(){
double donation, counter = 0, total = 0,
average;
while(true){
cout<<"Please enter your
donation or (-1 to exit): ";
cin>>donation;
while((donation<0 ||
donation>10) && donation!=-1) {
cout<<"Invalid donation! Please enter donation between 0 and
10."<<endl;
cout<<"Please enter your donation or (-1 to exit): ";
cin>>donation;
}
if(donation == -1)
break;
total = total + donation;
counter++;
}
average = (double)total/counter;
cout<<"\nThe average of the valid donations is
"<<average;
return 0;
}
Screenshot of the program
Output 1:
Output 2:
NOTE: If you want to change something , please let me know through comments; I will surely revert back to you.
Please give a up vote .....
Thank you...