### 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:

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include 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
 ``12345678`` ``````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:
 ``1234567`` ``````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):
 ``1234567891011121314`` ``````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<

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.