please help: there is a problem with my do while loop...

my do while loop is right ...but it ends without me entering all the values in for output!!! as soon as i enter two inputs it ends!! so please help me find whats missing??


1. Write a program that reads 10 numbers. The program should report the total number of positive numbers entered. The average value of the positive numbers and the same for negative numbers. The program should also indicate which of the two categories (positive or negative) has the highest count. (Use do-while loop)
Sample run:
Please input 10 numbers:
3
-6
-12
-4
13
-12
-8
-2
133
1
There were 4 positive numbers and their average was 37.5
There were 6 negative numbers and their average was -7.33
There were more negative numbers than positive numbers



(((((this is my code>>>>>

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// MAIN_CPP

#include <iostream>

using namespace std;

#include <iomanip>

using std::setprecision;

int main()

{
	int		counter_pos	= 0,
			counter_neg		= 0,
			input			= 0,
			sum_pos		= 0,
			sum_neg			= 0;

	double	average_pos	= 0,
			average_neg		= 0;


cout << "Please input 10 numbers : " << endl;
	cin >> input;

	do

	 {
		 if (input>= 0)
		{

			counter_pos++;
			sum_pos += input;
		}

		else if (input< 0)
		{

			counter_neg++;
			sum_neg += input;
		}
	
			cin >> input;
	 }
	
	

	
	

	 while (input >= 10);
	 
	 {
		if (counter_pos != 0)
		average_pos = static_cast<double>(sum_pos) / counter_pos;

	else average_pos = 0;

	if (counter_neg != 0)
		average_neg = static_cast<double>(sum_neg) / counter_neg;

	else
		average_neg = 0;
	
		
		}
	

	cout << "There were " << counter_pos << " positive number(s), and their average was " << setprecision(2) << fixed << average_pos << "!" << endl;
	cout << "There were " << counter_neg << " negative number(s), and their average was " << setprecision(2) << fixed << average_neg << "!" << endl;

	 
	if (counter_pos > counter_neg)
		cout << "There were more positive numbers than negative numbers." << endl;

	else if (counter_pos < counter_neg)
		cout << "There were more negative numbers than positive numbers." << endl;

	else
		cout << "There was the same amount of positive and negative numbers." << endl;

	if (average_pos > average_neg)
		cout << "positive numbers average is higher." << endl;

	else if (average_pos < average_neg)
		cout << "negative numbers average is higher" << endl;

	else
		cout << "The average of the positive numbers is as high as of the negative ones." << endl;

	 
return 0;

}
Last edited on
please help asap >>> thanks to whom helps me:`(
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
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int counter_pos = 0,
        counter_neg = 0,
        input = 0,
        sum_pos = 0,
        sum_neg = 0;

    double average_pos = 0,
           average_neg = 0;

    cout << "Please input 10 numbers : " << endl;
    cin >> input;
    do
    {
        if (input>= 0)
        {
            counter_pos++;
            sum_pos += input;
        }
        else if (input< 0)
        {
            counter_neg++;
            sum_neg += input;
        }
        cin >> input;
    } while (input >= 10);

    {
        if (counter_pos != 0)
            average_pos = static_cast<double>(sum_pos) / counter_pos;
        else 
            average_pos = 0;

        if (counter_neg != 0)
            average_neg = static_cast<double>(sum_neg) / counter_neg;
        else
            average_neg = 0;
    }

    cout << "There were " << counter_pos << " positive number(s), and their average was " << setprecision(2) << fixed << average_pos << "!" << endl;
    cout << "There were " << counter_neg << " negative number(s), and their average was " << setprecision(2) << fixed << average_neg << "!" << endl;

    if (counter_pos > counter_neg)
        cout << "There were more positive numbers than negative numbers." << endl;
    else if (counter_pos < counter_neg)
        cout << "There were more negative numbers than positive numbers." << endl;
    else
        cout << "There was the same amount of positive and negative numbers." << endl;

    if (average_pos > average_neg)
        cout << "positive numbers average is higher." << endl;
    else if (average_pos < average_neg)
        cout << "negative numbers average is higher" << endl;
    else
        cout << "The average of the positive numbers is as high as of the negative ones." << endl;

    return 0;
}
Your formatting was brutal, so I updated it. I didn't change the function of your code, just the way it looks (by cutting out superfluous whitespace). Check out the part where you read in input. The first read will always happen. Then you go into a do-while loop. A do-while will always execute once. Then, at the end you evaluate the condition which in this case is input >= 10. That means the loop will keep going as long as the number you input is greater than or equal to 10. This is not what you want. You don't care what the input is, you just want to get 10 inputs. I would suggest a for-loop.
1
2
3
4
5
6
7
8
9
10
//...
int numTermsIWant = 10;
for(int i = 0; int < numTermsIWant; ++i)
{
    cout << "Please input a number : " << endl;
    cin >> input;

    //update counters and sums accordingly
}
//... 

EDIT: The curly braces on line 34 and 44 are unnecessary. I think given the placement of the while in your code it may have confused you as to what that while actually belonged to. It belongs to the block that starts with do right above it.
Last edited on
i cant use a for loop!! as it asked for a while loop??

and if putting a while loop inside or outside a while loop i`ll create an infinite loop!!!


please if ayone could help???
What booradley60 was saying is that you need to use a separate variable to use as a counter to count the total numbers that have been input by the user so far regardless of what type of loop you use.

1
2
3
4
5
6
int counter = 0;
do {
    //get number from user
    //update counters and sums
    ++counter; //counts the current input
} while ( counter < 10);


The problem with your current code deals with the logic of it. Rather than checking to see if the user has entered less than ten numbers, it checks to see if the number the user has input is greater than ten.
thanks so much both of you :)

i`m just not that good in english ~~:s

but i did it :)

and the code turned out like this:


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int counter_p = 0,
        counter_n = 0,
        input = 0, i=0 ,
        sum_p = 0,
        sum_n = 0;

    double avg_p = 0,
           avg_n = 0;

    cout << "Please input 10 numbers : " << endl;
   

    do
    { cin >> input;
		
        if (input >= 0)
        {
            counter_p++;
            sum_p += input;
        }
        if (input < 0)
        {
            counter_n++;
            sum_n += input;
        }
i++;
	}
	
   
	while (i < 10);
	{
        if (counter_p != 0)
            avg_p = static_cast<double>(sum_p) / counter_p;
        else 
            avg_p = 0;

      
		if (counter_n != 0)
            avg_n = static_cast<double>(sum_n) / counter_n;
        else
            avg_n = 0;
	
    }

    cout << "There were " << counter_p << " positive number(s), and their average was " << setprecision(2) << fixed << avg_p << "!" << endl;
    cout << "There were " << counter_n << " negative number(s), and their average was " << setprecision(2) << fixed << avg_n << "!" << endl;

    if (counter_p > counter_n)
        cout << "There were more positive numbers than negative numbers." << endl;
    else if (counter_p < counter_n)
        cout << "There were more negative numbers than positive numbers." << endl;
    else
        cout << "There was the same amount of positive and negative numbers." << endl;
	
    return 0;
}




/*
Please input 10 numbers :
3
-6
-12
-4
13
-12
-8
-2
133
1
There were 4 positive number(s), and their average was 37.50!
There were 6 negative number(s), and their average was -7.33!
There were more negative numbers than positive numbers.
Press any key to continue
*/



i hope other people get benefit :)
Topic archived. No new replies allowed.