Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中动态添加变量+;_C++_Dynamic Variables - Fatal编程技术网

C++ 在C+中动态添加变量+;

C++ 在C+中动态添加变量+;,c++,dynamic-variables,C++,Dynamic Variables,我正在为学校做一个项目。情况就是这样: 您应该能够为n学生人数输入权重。计算学生的平均体重,并输出体重低于65公斤的学生人数 现在我有C++源的例子: #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int number_of_students; cout << "How many students

我正在为学校做一个项目。情况就是这样:

您应该能够为n学生人数输入权重。计算学生的平均体重,并输出体重低于65公斤的学生人数

<>现在我有C++源的例子:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int number_of_students;

    cout << "How many students would you like to add?: ";
    cin >> number_of_students;
    cout << endl;
    cout << endl;

    cout << "--------------------------------------------" << endl;
    cout << "---------- ENTER STUDENT'S WEIGHT ----------" << endl;
    cout << "--------------------------------------------" << endl;
    cout << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
国际学生人数;
学生人数;
不能使用运算符创建一个整数数组

cin >> number_of_students;
int* x = new int[number_of_students];
您现在有了一个
size=numberofstudent
数组。使用它来存储权重



编辑以这种方式处理并不是最好的(处理内存泄漏等)。请注意注释和其他答案,尤其是使用和基于的解决方案的答案。

您需要将权重存储在某种大小可变的容器中。非常推荐使用标准库中的容器,最典型的选择是
std::vector

#include<vector>
#include<algorithm>  //contains std::accumulate, for calculating the averaging-sum

int main(int argc, char *argv[])
{
    int number_of_students;

    cout << "How many students would you like to add?: ";
    cin >> number_of_students;
    cout << endl;
    cout << endl;

    cout << "--------------------------------------------" << endl;
    cout << "---------- ENTER STUDENT'S WEIGHT ----------" << endl;
    cout << "--------------------------------------------" << endl;
    cout << endl;

    std::vector<float> weights(number_of_students);

    for(int i=0; i<number_of_students; ++i) {
      cin >> weights[i];
    }

    cout << "Average is: " << std::accumulate(weights.begin(), weights.end(), 0.f)
                                      / number_of_students
      << std::endl;

    return EXIT_SUCCESS;
}
#包括
#include//contains std::accumulate,用于计算平均值和
int main(int argc,char*argv[])
{
国际学生人数;
学生人数;

cout您可以在循环中使用一个变量。例如:

for (int i = 0; i < number_of_students; i++) {
    int weight;
    cin >> weight;
    if (weight < 65)
        result++;
}
for(int i=0;i>重量;
如果(重量<65)
结果++;
}

也许你不明白这个问题,但是你可以创建一个特定长度的数组,比如

int* a = new int[number_of_students];

for (int i = 0; i < number_of_students; i++) {
    cin >> a[i];
}
int*a=新的int[学生人数];
对于(int i=0;i<学生人数;i++){
cin>>a[i];
}

希望有帮助,祝你好运…

我来自JavaScript和PHP背景…这就像一个数组?请记住,在使用new[…]的地方,需要删除[]在其他地方确保没有内存泄漏。是的,而且真的…只是不要这样做,@Zlatan。标准容器更加安全、可靠、容易…使用手动分配的阵列在这些日子里几乎没有任何真正的优势,这只会让你更担心一件事。这实际上更高效(+1)与存储所有权重然后计算平均值相比,对于更复杂的问题,它通常更容易出错,更实用的方法的开销通常并不重要。我只是认为,如果这是你的学校作业,而你还没有学习数组,那么你应该在循环中使用局部变量。std::d积累了什么o?@Zlatan什么都知道。@Zlatan:实际上我没有正确使用它,等等……现在修好了。