jgriff
jgriff

Reputation: 1

C++ help. I'm having problems getting this right

Here is the code that I have so far. What I'm trying to do is make the program display the number of children over 60 inches and their height. The program now shows the number of children over 60 inches, but I also need it to display the height of the children over 60 inches. Thanks in advance!

#include <iostream>
using namespace std;
int main ()
{
    double childHeight[10];
    int numChildren = 0;

    for (int x = 0; x < 10; x = x + 1)
    {
        childHeight[x] = 0.0;
    }
    cout << "You will be asked to enter the height of 10 children." << endl;
    for (int x = 0; x < 10; x = x + 1)
    {
        cout << "Enter the height of child: ";
        cin >> childHeight[x];
    }
    cout << "The number of children over 60 inches are: "<< endl;
    for (int x = 0; x < 10; x = x + 1)
    {
        if (childHeight[x] > 60)
        {                
           numChildren = numChildren + 1;                
        }  
    }
    cout << numChildren << endl;
    system("pause"); 
    return 0;
}

Upvotes: 0

Views: 96

Answers (2)

paxdiablo
paxdiablo

Reputation: 882626

That's very close, a good first attempt if it's homework, so I don't mind helping out a bit.

You already have a loop that goes through your array checking the heights so it's a simple matter of adding to that, so that you:

  • also output the heights over 60 when detected; and
  • make slight changes to what's printed so that it makes sense in that order.

Change:

cout << "The number of children over 60 inches are: " << endl;
for (int x = 0; x < 10; x = x + 1)
{
    if (childHeight[x] > 60)
    {                
       numChildren = numChildren + 1;                
    }  
}
cout << numChildren << endl;

to:

cout << "The heights of children over 60 inches are: " << endl;    // ADD
for (int x = 0; x < 10; x = x + 1)
{
    if (childHeight[x] > 60)
    {                
       numChildren = numChildren + 1;                
       cout << "    " << childHeight[x] << endl;                  // ADD
    }  
}
cout << "The number of children over 60 inches are: " << endl;    // MOVE
cout << "    " << numChildren << endl;                            // CHNG

The change to the output of numChildren was simply to add spaces, a nice formatting touch. This should result in output something like:

The heights of children over 60 inches are:
    62
    67
The number of children over 60 inches are:
    2

Some minor advice which doesn't affect your code performance at all, but I don't think I've seen x = x + 1 for decades. The C and C++ way of doing this is generally ++x.

In addition, I tend to prefer \n to endl in most cases. The latter (see here) outputs an end of line and flushes the buffers, which can be inefficient is some circumstances.

Upvotes: 5

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

You just need another for loop like the one that counts the tall children, only instead of counting, in the body you can print the height.

Upvotes: 0

Related Questions