Output for Array Not Displaying Correctly

My program works but I can't figure out how to construct the array to display the output correctly.

Here's the INPUT:
Heights:
62 61 59 64 60
54 57 53 60 55

This is the expected OUTPUT
:
Heights of students taller than 60 inches: 62 61 64
Number of students taller than 60 inches: 3


My code displays the following output:
Heights of students taller than 60 inches: 62
Number of students taller than 60 inches: 3
Heights of students taller than 60 inches: 61
Number of students taller than 60 inches: 3
Heights of students taller than 60 inches: 64
Number of students taller than 60 inches: 3


I've tried to correct the code but I just keep making it worse, so any help would be most appreciated.

Here's the 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
#include <iostream>

using namespace std;

int main()
{
   	double height[10];
    int htAbove60 = 0;
    
	for (int x = 0; x < 10; x = x + 1)
	{
		height[x] = 0.0;
	}
	    cout << "You are asked to enter heights (in inches) of 10 students. " << endl << endl;
	
    for (int x = 0; x < 10; x = x + 1)
	{
        cout << "Enter the height of a student: ";
		cin >> height[x];
	}      
	for (int x = 0; x < 10; x = x + 1)
	{
        if (height[x] > 60)
        {
        htAbove60 = htAbove60 + 1;
	    }
     }
     	 for (int x = 0; x < 10; x = x + 1)
	{
        if (height[x] > 60)
{
	    cout << "Heights of students taller than 60 inches: " << height[x] << endl;
	    cout << "Number of students taller than 60 inches: " << htAbove60 << endl;
     }   
}
	system("pause"); 
	return 0;
}
Last edited on
It's doing that because of
1
2
3
4
5
6
7
8
for (int x = 0; x < 10; x = x + 1)
	{
        if (height[x] > 60)
{
	    cout << "Heights of students taller than 60 inches: " << height[x] << endl;
	    cout << "Number of students taller than 60 inches: " << htAbove60 << endl;
     }   
}

That's displaying the cout's then starting over and displaying them again. To get it to display the way you want could be a little tricky. One way to do it my be after this point in the code:
1
2
3
4
5
6
7
for (int x = 0; x < 10; x = x + 1)
	{
        if (height[x] > 60)
        {
        htAbove60 = htAbove60 + 1;
	    }
     }

add something like this (but with a better varable name):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int heights_above_60[htAbove60];
int y = 0;
for (int x = 0; x < 10; x++)
{
       if (height[x] > 60){
                heights_above_60[y] = x;
                y++;
                }
}
cout << "Heights of students taller than 60 inches: ";
for (int x = 0;x < htAbove60; x++) {
cout<<height[heights_above_60[x]]<<" ";
}
cout <<endl<< "Number of students taller than 60 inches: " << htAbove60 << endl;


Use that and tell me if it works (honestly, I don't know if it will). Also, there may be some errors in what I wrote because I didn't test it and I wrote it on this website without typing it in my IDE first to see if I made some small errors. Tell me how it turns out.
Awesome! I made a few changes to the code you suggested and it worked perfectly! Thank you so much! I never would have figured it out had it not been for you. :-)
Topic archived. No new replies allowed.