简体   繁体   English

C ++帮助。 我在解决这个问题时遇到了问题

[英]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. 我想做的是让程序显示60英寸以上的儿童人数及其身高。 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. 该程序现在显示60英寸以上儿童的数量,但是我还需要它来显示60英寸以上儿童的身高。 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; 当检测到时也输出超过60的高度; 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. numChildren的输出numChildren的更改只是添加空格,这是一种不错的格式化方式。 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. 一些小建议完全不会影响您的代码性能,但我认为数十年来我从未见过x = x + 1 The C and C++ way of doing this is generally ++x . 使用C和C ++的方法通常是++x

In addition, I tend to prefer \\n to endl in most cases. 另外,在大多数情况下,我倾向于\\n而不是endl The latter (see here ) outputs an end of line and flushes the buffers, which can be inefficient is some circumstances. 后者(请参阅此处 )输出行尾刷新缓冲区,在某些情况下效率低下。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++我在数组/动态内存方面遇到一些问题 - C++ I'm having some problems with arrays/dynamic memory 我是 C++ 的新手,我得到了这个错误帮助。 我很确定这是一个简单的解决方法,但我查找的答案没有意义 - I'm new to C++ and I got this error help. I'm pretty sure its an easy fix but the answers i looked up didn't make sense C ++帮助。 数组不适用于整数 - C++ Help. Arrays not working with integers 请提供任何帮助,我正在尝试将此 C++ 代码转换为 C 代码,但我遇到了 fflush() function 代码的问题 - Any help please, I'm trying to convert this C++ code to C code, but I'm having problem with fflush() function 循环永无止境~​​~需要帮助。 C ++ - loop never end ??~ need help. C++ 循环帮助。 在不应有的情况下终止。 C ++ - for loop help. Terminates when it isnt supposed to. c++ C ++游戏编程帮助。 超简单 - C++ game programming help. Super-Simple 在C ++中将节点附加到树上(参考和指针帮助。) - Attaching nodes to a tree in C++ (Reference and pointer help.) glDrawRangeElements遇到的问题 - Problems I'm having with glDrawRangeElements 我只是运行简单的 c++ 程序只是为了检查我的 mingW 是否工作正常,但我遇到了这个错误。 有人可以帮我解决吗? - I'm just running the simple c++ program just to check my mingW is working fine or not but I'm having this error. can someone help me to resolve it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM