In: Computer Science
Program in C++ with comments using a do while loop with break statement fo negative integer.
#include <iostream>
using namespace std;
int main() {
//variables to count initiated to 0
int count=0, countNumbers =0,total =0,value;
//loop to read integers till break is executed
do{
cout<<"\nEnter a number: ";
cin>>value;
//once value is read increase value read count
count++;
//if value is negative display error and break from the loop
if(value<0){
cout<<"\nError!!Negative value entered!Program will
terminate";
break;
}
//if value is 0 ignore it
else if(value==0){
//add count of positive numbers
countNumbers++;
}
//if value is positive increase count of numbers and add it to
total
else {
countNumbers++;
total +=value;
}
}while(true);
//display all the values counted
cout << "\nNumber of values read is "<<count;
cout << "\nNumber of values totaled is
"<<countNumbers;
cout << "\nTotal of values is "<<total;
}
Sample output :