简体   繁体   中英

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;
}

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.

您只需要另一个for循环,例如用来计数高个子的循环,而无需计数,您可以在体内打印高度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM