Help with C++ prorgram

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;

return 0;

}
}


Any Ideas?
Last edited on
The teacher is telling you to use a loop to get numbers until the user enters a zero to end the input, not to enter a string with 0 at the end.

Read the directions and follow...

PS. your return 0 needs to be moved down.

To do what your asking change
int number;
to
string number;

You can fix your code just by moving things around. I made 3 changes which I'll show you and just moved things around until it worked.

If you do this you can move the cin inside the while loop
int number=1;

typing 0 to exit is counted in the "count". I didn't think it should be included.
average=total/(count-1);

typing 0 to exit is counted as a positive number. I didn't think it should be included.
cout<<"The number of possitives is: "<<possitive-1<<endl;
Last edited on
As Samuel said, you are not asked to read the integers from string but to read an unspecified number of integers.

A simple program could be


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace 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;
                }
                else if(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;

}
Last edited on
Topic archived. No new replies allowed.