Hey guys, so I'm pretty sure I know the issue here but i'm not sure how to fix it. The question I have is:
Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Stop reading input when user enters 0. Display the average as a double.
I need to find a way to turn it into a string so that it can read all the numbers when inputed together but I'm not sure how.
This is what I have so far:
#include <iostream>
using namespace std;
int main(){
int possitive=0, negative=0, count=0;
double total=0, average;
int number;
cout<<"Please enter a series of numbers followed by 0 when you are done: ";
cin>>number;
while(number!=0){
total+=number;
count++;
if(number>=0){
possitive++;
}
else if (number<0){
negative++;
}
average=total/count;
cout<<"The number of possitives is: "<<possitive<<endl;
cout<<"The number of negatives is: "<<negative<<endl;
cout<<"The total is: "<<total<<endl;
cout<<"The average is: "<<average<<endl;
#include <iostream>
usingnamespace std;
int main()
{
int a, total_positive = 0, count_positive = 0, total_negative = 0, count_negative = 0;
while(true) // Continue indefinitely
{
cout<<"Enter a number: ";
cin>>a;
if(a == 0) // Check if the number is 0.
{
cout<<"You chose to exit\n"; //Exit if the number is 0
break;
}
elseif(a > 0) // check if the number is positive
{
total_positive = total_positive + a;
count_positive++;
}
else // If the number is neither positive nor 0 then it is negative
{
total_negative = total_negative + a;
count_negative++;
}
}
if(count_positive == 0) // Assure that the number of positive integers is greater than 0, else averaging is not possible
{
cout<<"You Did not enter any positive integer\n";
}
else
{
cout<<"You Entered "<<count_positive<<" positive integers\t";
cout<<"Sum = "<<total_positive<<"\t Average = "<<(float(total_positive)/float(count_positive))<<endl;
}
if(count_negative == 0) // Assure that the number of negative integers is greater than 0, else averaging is not possible
{
cout<<"You Did not enter any negative integer\n";
}
else
{
cout<<"You Entered "<<count_negative<<" negative integers\t";
cout<<"Sum = "<<total_negative<<"\t Average = "<<(float(total_negative)/float(count_negative))<<endl;
}
return 0;
}