C ++函数&数组 - Visual Studio中的错误C4700

时间:2018-05-23 04:43:01

标签: c++ arrays function

我有关于数组和函数的问题。这是与我完成的部分有关的说明:

在学期期间,计算机科学课程的学生需要进行4次测试。为了计算学生的数量 课程成绩,最低的考试成绩被删除。只有前3个考试成绩用于确定字母 使用数组,创建一个程序,使用scores.txt中的数据返回字母等级 每个学生如下:

  1. 创建一个名为dropped_test()的函数 - 确定每个学生的最低考试成绩。
  2. 因此,当我尝试调试它时,我得到错误#C4700说已经使用了未初始化的变量 - "低"在我调用dropped_test函数的行上。谁能帮我?我很迷茫,不知道该怎么做。谢谢。

    {

    #include<iostream>
    #include<fstream>
    #include<iomanip>
    #include<string>
    
    using namespace std;
    
    //Prototypes.
    void dropped_test(int low, int T1[], int T2[], int T3[], int T4[], int i);
    
    int main() {
        //Declares
        ifstream fin;
        ofstream fout;
        string name[100];
        int T1[100], T2[100], T3[100], T4[100], low;
        int i = 0;
        int sumT;   //Sum of the test scores after dropped test
        int percent;
        char grade;
        int sumT1 = 0, sumT2 = 0, sumT3 = 0, sumT4 = 0, sumTotal = 0;   //Running sums of the test scores for each column
    
    
        //Open the files.
        fin.open("scores.txt");
        fout.open("grades.txt");
    
        //Test if files opened.
        if (fin) {
            cout << "Processing..." << endl;
            cout << endl;
        }
        else {
            cout << "ERROR ID107: File not found." << endl;
            cout << endl;
            system("pause");
            return 0;
        }
    
        //Remove the header.
        fin.ignore(100, '\n');
    
        //Input the data.
        while (!fin.eof()) {
            getline(fin, name[i], ',');
            fin >> T1[i];
            fin.ignore(5, ',');
            fin >> T2[i];
            fin.ignore(5, ',');
            fin >> T3[i];
            fin.ignore(5, ',');
            fin >> T4[i];
            fin.ignore();
        }
    
        //Calculate the lowest score.
        /*low = T1[i];
        if (low > T2[i])
            low = T2[i];
        if (low > T3[i])
            low = T3[i];
        if (low > T4[i])
            low = T4[i];*/
    
        //Dropped Test
        dropped_test(low, T1, T2, T3, T4, i);
    
        //Calculate the sum of the test scores.
        sumT = T1[i] + T2[i] + T3[i] + T4[i] - low;
    
        //Calculate the test grade.
        percent = sumT / 3;
        if (percent >= 90)
            grade = 'A';
        else if (percent >= 80)
            grade = 'B';
        else if (percent >= 70)
            grade = 'C';
        else if (percent >= 60)
            grade = 'D';
        else
            grade = 'F';
    
        //Calculate the running sums.
        sumT1 = sumT1 + T1[i];
        sumT2 = sumT2 + T2[i];
        sumT3 = sumT3 + T3[i];
        sumT4 = sumT4 + T4[i];
        sumTotal = sumTotal + sumT;
    
        cout << "Testing." << endl;
    
        //Close files.
        fin.close();
        fout.close();
    
        system("pause");
        return 0;
    }
    
    void dropped_test(int low, int T1[], int T2[], int T3[], int T4[], int i = 0) {
        low = T1[i];
            if (low > T2[i])
        low = T2[i];
            if (low > T3[i])
        low = T3[i];
            if (low > T4[i])
        low = T4[i];
            i++;
    }
    

2 个答案:

答案 0 :(得分:0)

如果要更改函数内的参数值,则需要初始化变量low并通过引用传递参数。下面是更新的代码:

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

using namespace std;

//Prototypes.
void dropped_test(int &low, int T1[], int T2[], int T3[], int T4[], int i);

int main() {
    //Declares
    ifstream fin;
    ofstream fout;
    string name[100];
    int T1[100], T2[100], T3[100], T4[100], low = 0;
    int i = 0;
    int sumT;   //Sum of the test scores after dropped test
    int percent;
    char grade;
    int sumT1 = 0, sumT2 = 0, sumT3 = 0, sumT4 = 0, sumTotal = 0;   //Running sums of the test scores for each column


    //Open the files.
    fin.open("scores.txt");
    fout.open("grades.txt");

    //Test if files opened.
    if (fin) {
        cout << "Processing..." << endl;
        cout << endl;
    }
    else {
        cout << "ERROR ID107: File not found." << endl;
        cout << endl;
        system("pause");
        return 0;
    }

    //Remove the header.
    fin.ignore(100, '\n');

    //Input the data.
    while (!fin.eof()) {
        getline(fin, name[i], ',');
        fin >> T1[i];
        fin.ignore(5, ',');
        fin >> T2[i];
        fin.ignore(5, ',');
        fin >> T3[i];
        fin.ignore(5, ',');
        fin >> T4[i];
        fin.ignore();
    }

    //Calculate the lowest score.
    /*low = T1[i];
    if (low > T2[i])
        low = T2[i];
    if (low > T3[i])
        low = T3[i];
    if (low > T4[i])
        low = T4[i];*/

    //Dropped Test
    dropped_test(low, T1, T2, T3, T4, i);

    //Calculate the sum of the test scores.
    sumT = T1[i] + T2[i] + T3[i] + T4[i] - low;

    //Calculate the test grade.
    percent = sumT / 3;
    if (percent >= 90)
        grade = 'A';
    else if (percent >= 80)
        grade = 'B';
    else if (percent >= 70)
        grade = 'C';
    else if (percent >= 60)
        grade = 'D';
    else
        grade = 'F';

    //Calculate the running sums.
    sumT1 = sumT1 + T1[i];
    sumT2 = sumT2 + T2[i];
    sumT3 = sumT3 + T3[i];
    sumT4 = sumT4 + T4[i];
    sumTotal = sumTotal + sumT;

    cout << "Testing." << endl;

    //Close files.
    fin.close();
    fout.close();

    system("pause");
    return 0;
}

void dropped_test(int &low, int T1[], int T2[], int T3[], int T4[], int i = 0) {
    low = T1[i];
        if (low > T2[i])
    low = T2[i];
        if (low > T3[i])
    low = T3[i];
        if (low > T4[i])
    low = T4[i];
        i++;
}

答案 1 :(得分:0)

似乎low是输出变量,因此请使用int& low代替int low