C++ 数组在while循环中未正确初始化

C++ 数组在while循环中未正确初始化,c++,arrays,loops,for-loop,while-loop,C++,Arrays,Loops,For Loop,While Loop,我的问题是,我已经设置了一个数组来存储根据从文件读取的值计算的总数。然后将这些存储的总数相加,得出总体平均值 此问题源于程序开始时的“cin”,用户输入一个数字,该数字应通过设置程序循环次数和阵列中的模块数量来驱动程序。无论我怎么尝试,阵列似乎都无法正常工作 #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std

我的问题是,我已经设置了一个数组来存储根据从文件读取的值计算的总数。然后将这些存储的总数相加,得出总体平均值

此问题源于程序开始时的“cin”,用户输入一个数字,该数字应通过设置程序循环次数和阵列中的模块数量来驱动程序。无论我怎么尝试,阵列似乎都无法正常工作

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string StudentGrades;
    int studentID;
    double quiz1;
    double quiz2;
    double quiz3;
    double quiz4;
int total = 0;
double choice;
ofstream outFile;
double numStud=1;

cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
outFile.open("StudentGrades.txt");
cout << "How many students would you like to enter?" << endl;
cin >> numStud;

for (int x = 0; x < numStud; x++)
{
    cout << "Enter student ID: ";
    cin >> studentID;
    cout << "Enter quiz grade 1: ";
    cin >> quiz1;
    //cout << quiz1;
    cout << "Enter quiz grade 2: ";
    cin >> quiz2;
    //cout << quiz2;
    cout << "Enter quiz grade 3: ";
    cin >> quiz3;
    //cout << quiz3;
    cout << "Enter quiz grade 4: ";
    cin >> quiz4;
    //cout << quiz4;
    cout << endl;
    //outFile.open("StudentGrades.txt");
    if (outFile.is_open())
    {
        cout << "inside if/else outFile" << endl;
        //outFile << "File successfully open";

        outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;

    }
    else
    {
        cout << "Error opening file";
    }
    outFile.close();
    /*cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
    cin >> choice;
    if (choice == 1)
        continue;
    if (choice == 0)
    {
        outFile.close();
        break;
    }*/

}



ifstream inFile;
inFile.open("StudentGrades.txt");
int sTotal;
int total[numStud];
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
    //cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
    total = (quiz1 + quiz2 + quiz3 + quiz4);
    sTotal = total[numStud];

    double avg = total / 4;


}


system("pause");
return 0;
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
学生成绩;
国际学生;
双quiz1;
双quiz2;
双quiz3;
双quiz4;
int-total=0;
双重选择;
出流孔的直径;
双numStud=1;
cout-quiz1;
//库特奎兹2;
//库特奎兹3;
//库特奎兹4;

//cOUT

<代码> int [NUTSUD];是C++中的标准,不是标准的。如果你需要一个数组,你不知道它的大小,那么你应该使用A。矢量几乎可以像数组一样使用。例如,你可以变成:

int total;
std::vector<int> studentTotal;
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
    //cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
    studentTotal.push_back(quiz1 + quiz2 + quiz3 + quiz4); // insert into the vector at the end
    total += studentTotal.back();  // get last inserted element
}
int-total;
std::向量studentTotal;
而(填充>>studentID>>quiz1>>quiz2>>quiz3>>quiz4)
{

//cout
int总计[numStud];
不是标准的编译器C++你能给我一个例子说明它的用途吗?@ConnorLance我编辑了我的答案,给你一个小例子。它的库名是什么,因为它正在为它创建错误me@ConnorLance您需要
#包括
。如果您没有注意到,我有一个指向文档的链接,其中包含此信息如果你在我的回答中点击上面写着
std::vector
的地方,它会带你去cppreference.com上的vectors。有没有办法通过“studentTotal”进入“cout”屏幕