Need advice for my program

May 12, 2013 at 10:14pm
Hello all, I am trying to write a program that displays the user's input. Then, for it to calculate the input of how many positive and negative integers, the sum of those integers and the average of all integers. The problem is, i cant seem to figure out how to put a limit of up to 20 integers entered and how to display what the user entered in a cout statement.
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
 #include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	float avg;
	int input, pos=0, neg=0, sumPos=0, sumNeg=0;
	cout << "Please enter up to 20 non-zero intergers. Please press 0 when completed: " << endl;
	cin >> input;		
	while (input!=0)
	{
	
		if (input>=0)
		{
		pos++;
		sumPos+=input;
		}
		else
		{
		neg++;
		sumNeg+=input;
		}
		cin >> input;
	}
	cin.ignore(15,'\n');
cout << "Here are the numbers you have entered:\n" << endl;
	cout << input <<"\n";
	
avg=(sumPos+sumNeg)/(pos+neg);
cout << "Number of positive input: " << pos << "\t";
cout << "Sum of positive input: " << sumPos << endl;
cout << "Number of negative input: " << neg << "\t";
cout << "Sum of negative input: " << sumNeg << endl;
cout << "Average input value: " << avg << endl;
cin.get();
return 0;
}
May 12, 2013 at 10:57pm
use
1
2
3
4
for(int i = 0; i < 20; i++)
{
// asks for user input and stores them.
}

and could use an array to store numbers
 
int number[20];

and then cout each element in the array to display them.
May 13, 2013 at 4:28am
i figured as much to use an array, wasn't sure how to input. thanks so much for the tip :)
May 13, 2013 at 5:09am
input to an array like this
1
2
3
4
5
6
7
8
9
10
11
int array[ SIZE ];

for( int i = 0; i < SIZE; i++ )
{
     std::cin >> array[ i ];
}

for( int i = 0; i < SIZE; i++ )
{
     std::cout << array[ i ] << std::endl;
}


Actually I would use a do/while loop since you want to input atleast one time ( do ) and you are doing "while" a certian thing has or has not happened ex it does not equal 0 or there are less than 20 entries
Last edited on May 13, 2013 at 5:11am
Topic archived. No new replies allowed.