Мой текстовый файл не отображается правильно в выводе - PullRequest
0 голосов
/ 01 мая 2020

В настоящее время я занимаюсь чтением данных из сохраненного файла в программу на C ++. Текстовый файл содержит массив чисел, которые должны представлять оценки учеников для определенных заданий. Предполагается, что желаемый вывод отображает гистограмму, подсчитывающую каждого учащегося в диапазоне оценок, и напоминает этот пример:


A (90 - 100) *********

B (80 - 89) *****

C (70 - 79) ****

D (60 - 69) *******

F (00 - 59) *****

САМЫЙ НИЗКИЙ СЧЕТ В КЛАССЕ: 99

НАИБОЛЬШИЙ СЧЕТ В КЛАССЕ: 999

КОЛИЧЕСТВО СТУДЕНТОВ : 999

EXAM01 СРЕДНЕГО: 999,99

EXAM02 СРЕДНЕГО: 999,99

EXAM03 СРЕДНЕГО: 999,99

EXAM04 СРЕДНЕГО: 999,99

ФИНАЛЬНОЕ СРЕДНЕГО : 999,99

LABS AVERAGE: 999,99

Тесты AVERAGE: 999,99

( Эти результаты имитируются )

Когда я компилирую и запустите программу, каждая категория заполнена нулями и гистограмма никогда не отображается. Я предполагаю, что файл не вставлен в программу, но я не уверен, где проблема может l ie.

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

#define ARRAY_SIZE 10
#define CLASS_SIZE 30
#define GPA_SIZE 30
#define LAB_SIZE CLASS_SIZE * 5
using namespace std;

//Funtion Prototypes
void completesign(int);
int reports(int, int, int, int, int, int, int, int, double, double,
    double, double, double, double, double);
int Bsort(int[], int size);
double Average1(double[], int);


int main()
{
    int studentID[CLASS_SIZE] = { 0 };
    int LSIC[CLASS_SIZE] = { 0 };
    int HSIC[CLASS_SIZE] = { 0 };
    int grades[ARRAY_SIZE] = { 0 };

    double EXAM01[CLASS_SIZE] = { 0 };
    double EXAM02[CLASS_SIZE] = { 0 };
    double EXAM03[CLASS_SIZE] = { 0 };
    double EXAM04[CLASS_SIZE] = { 0 };
    double FINAL[CLASS_SIZE] = { 0 };

    double LABS[CLASS_SIZE] = { 0 };
    double QUIZZES[CLASS_SIZE] = { 0 };
    double gpa[GPA_SIZE] = { 0 };

    ofstream outputFile;
    ifstream inputFile1;

    int i = 0;
    int j = 0;
    int k = 0;

    int numA = 0;
    int numB = 0;
    int numC = 0;
    int numD = 0;
    int numF = 0;

    int LS = 0;
    int HS = 0;
    int NUM = 0;

    double Exam01Avg = 0.00;
    double Exam02Avg = 0.00;
    double Exam03Avg = 0.00;
    double Exam04Avg = 0.00;
    double FinalAvg = 0.00;
    double LabAvg = 0.00;
    double QuizAvg = 0.00;

    // Open file as output
    inputFile1.open("student_in", ios::out);
    while (inputFile1 >> studentID[j])
    {

        // Identical to: cout << studentID[j] << " ";
        for (i = 0; i < ARRAY_SIZE; ++i)
        {
            inputFile1 >> grades[i];
            // Identical to: cout << grades[i] << " ";

            // Total of the letter grades
            if (grades[i] >= 90 && grades[i] <= 100)
            {
                numA++;
            }
            else if (grades[i] >= 80 && grades[i] <= 90)
            {
                numB++;
            }
            else if (grades[i] >= 70 && grades[i] <= 80)
            {
                numC++;
            }
            else if (grades[i] >= 60 && grades[i] <= 70)
            {
                numD++;
            }
            else
            {
                numF++;
            }
            switch (i)
            {
            case 0:         // Exam 1 for the Array
                EXAM01[j] = grades[i];
                break;
            case 1:         // Exam 2 for the Array
                EXAM02[j] = grades[i];
                break;
            case 2:         // Exam 3 for the Array
                EXAM03[j] = grades[i];
                break;
            case 3:         // Exam 4 for the Array
                EXAM04[j] = grades[i];
                break;
            case 4:         // Final for the Array
                FINAL[j] = grades[i];
                break;
            case 5:         // Lab 01 for the Array
                LABS[j] += grades[i];
                break;
            case 6:         // Lab 02 for the Array
                LABS[j] += grades[i];
                break;
            case 7:         // Lab 03 for the Array
                LABS[j] += grades[i];
                break;
            case 8:         // Lab 04 for the Array
                LABS[j] += grades[i];
                break;
            case 9:         // Lab 05 for the Array
                LABS[j] += grades[i];
                break;
            }
        }

        // Finding the Average of Student Labs
        LABS[j] = LABS[j] / 5;

        // Average of Student Quizzes
        inputFile1 >> QUIZZES[j];
        // Indentical to cout << QUIZZES[j] << endl;

        // Finding the total letter grade
        if (QUIZZES[j] >= 90 && QUIZZES[j] <= 100)
        {
            numA++;
        }
        else if (QUIZZES[j] >= 80 && QUIZZES[j] <= 90)
        {
            numB++;
        }
        else if (QUIZZES[j] >= 70 && QUIZZES[j] <= 80)
        {
            numC++;
        }
        else if (QUIZZES[j] >= 60 && QUIZZES[j] <= 70)
        {
            numD++;
        }
        else
        {
            numF++;
        }


        // Initiate the Bubble Sort Algorithm
        Bsort(grades, ARRAY_SIZE);

        LSIC[j] = grades[0];
        HSIC[j] = grades[0];

        /*
        cout << "____________________________________________________________________________________________" << endl;
        cout << "Student (" << j << ") Low/High grades are: " << LSIC[j] << ":" << HSIC[j] << endl;
        cout << "____________________________________________________________________________________________' << endl;
        */   // Identical output design to "outputfile" and "inputFile" displays

        // Output desgin for output
        outputFile << "_____________________________________________" << endl;
        outputFile << "Student ID: " << studentID[j] << endl;
        for(i = 0; i < ARRAY_SIZE; i++)
        {
            switch(i)
            {
            case 0:
                outputFile << "Exam 1: " << grades[i] << endl;
                break;
            case 1:
                outputFile << "Exam 2: " << grades[i] << endl;
                break;
            case 2:
                outputFile << "Exam 3: " << grades[i] << endl;
                break;
            case 3:
                outputFile << "Exam 4: " << grades[i] << endl;
                break;
            case 4:
                outputFile << "Final: " << grades[i] << endl;
                break;
            case 5:
                outputFile << "Lab 1: " << grades[i] << endl;
                break;
            case 6:
                outputFile << "Lab 2: " << grades[i] << endl;
                break;
            case 7:
                outputFile << "Lab 3: " << grades[i] << endl;
                break;
            case 8:
                outputFile << "Lab 4: " << grades[i] << endl;
                break;
            case 9:
                outputFile << "Lab 5: " << grades[i] << endl;
            }
        }
        outputFile << "Average of Quizzes: " << QUIZZES[j] << endl;

        j++;

    }
    // Identical to: cout << "the file read" << i << "many times \n" << endl;
    inputFile1.close();

    // Bubble sort of High and Low class scores
    Bsort(LSIC, CLASS_SIZE);
    Bsort(HSIC, CLASS_SIZE);

    LS = LSIC[0];
    HS = HSIC[j - 1];
    NUM = j;

    Exam01Avg = Average1(EXAM01, CLASS_SIZE);
    Exam02Avg = Average1(EXAM02, CLASS_SIZE);
    Exam03Avg = Average1(EXAM03, CLASS_SIZE);
    Exam04Avg = Average1(EXAM04, CLASS_SIZE);
    FinalAvg = Average1(FINAL, CLASS_SIZE);
    LabAvg = Average1(LABS, CLASS_SIZE);
    QuizAvg = Average1(QUIZZES, CLASS_SIZE);
    // This is where all of the output info will come from
    reports(numA, numB, numC, numD, numF, LS, HS, NUM, Exam01Avg,
        Exam02Avg, Exam03Avg, Exam04Avg, FinalAvg, LabAvg, QuizAvg);
    return 0;
}

// Bubble sort Algorithm
int Bsort(int b[], int size)
{
    int retVal = 0;
    int i = 0;
    int number1 = 0;
    int j = 0;
    int temp = 0;

    // Sorting of the Array

    for (j = 0; j < size - 1; j++)
    {
        for (i = 0; i < size - 1; i++)
        {
            if (b[i] > b[i + 1])
            {
                temp = b[i + 1];
                b[i + 1] = b[i];
                b[i] = temp;
            }
        }
    }

    // ***SORTED ARRAY **

    return retVal;
}

int LetterGradeNumber(double grade, char C)
{
    int retVal = 0;
    int i = 0;

    return retVal;
}

// When the program fufills the requirements
void completesign(int z)
{
    int i = 0;

    for (i = 0; i < z; i++)
    {
        cout << "*";
    }
}

// Averages of grades for each side of graph
double Average1(double d[], int size)
{
    double totalgrades = 0;

    for (int i = 0; i < size; i++)
    {
        totalgrades += d[i];
    }
    return(totalgrades / size);
}

Вот «кусок» кода, в котором, как мне кажется, проблема заключается.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...