为什么VS 2015在下面的代码末尾给我一个例外?

时间:2017-09-22 16:45:38

标签: visual-c++

我对c ++有些新意,我们的课程只是简单地调试了一下。这可能是我的第10次做,我已经花了一周时间,已经对网络进行了大量的研究,我只是不了解调试,以了解如何修复我的代码。该程序应该是这样的文件:

TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF

当它读取文件时,它应该输出学生ID,他们在每个问题上回答的内容以及每个学生的成绩。我的问题是,我对调试知之甚少,VS在我提供的代码末尾不断抛出异常。我只是学习如何使用动态数组,所以我知道它与我使用它们有关,因为我的程序在我的其他c ++类中工作得很好但我不得不改变它以使用动态数组来实现这个项目。 / p>

我的计划有什么问题?我已经对网络进行了研究并反复阅读了本书中的章节,我无法弄明白。

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <cstddef>

using namespace std;

// function prototypes
void readFile(ifstream& inFile);
char assignGrade(int score, int numQues);

int main()
{
    int numQues = 20;
    int numStud = 0;
    string *studentIDs;
    studentIDs = new string[numStud];
    char *correctAnswers;
    correctAnswers = new char[numStud];
    char *studentAnswers;
    studentAnswers = new char[numQues];
    ifstream inFile;

    cout << "\nRedo Programming Exercise Six of Chapter Eight\nUsing Dynamic Arrays..." << endl;

    cout << "\nPlease Enter the Number of Students: ";
    cin >> numStud;
    cout << endl;

    readFile(inFile);

    inFile.getline(correctAnswers, '/n');                           // read the correct answers first

    for (int i = 0; i < numStud; i++)                               // loop students
    {
        inFile >> studentIDs[i];                                    // get the student ID

        inFile.get();                                               // discard the space between the student ID and the answer

        for (int j = 0; j < numQues; j++)                           // loop questions
        {
            studentAnswers[j] = inFile.get();                       // get the student's answers
        }// end for

        cout << "Student ID: " << studentIDs[i] << endl;            // output student id

        int score = 0;                                              // declare and initialize score to zero

        cout << "Answers: ";                                        // display "Answers: "

        for (int j = 0; j < numQues; j++)                           // loop each question
        {
            cout << studentAnswers[j];                              // output student's answers

            if (studentAnswers[j] == correctAnswers[j])                                             // if student answer equals correct answer
                score += 2;                                                                             // correct answer
            else if (studentAnswers[j] != correctAnswers[j] && studentAnswers[j] != ' ')
                score -= 1;                                                                         // incorrect answer but not a blank
            else if (studentAnswers[j] == ' ')
                score = 0;

             delete[] studentAnswers;
        }// end for


        if (score < 0)
            score = 0;                                                                                  // don't allow for negative scores

        cout << endl;                                                                                   // new line, housekeeping

        char grade = assignGrade(score, numQues);               // call assignGrade function

        cout << "Grade: " << grade << "\n" << endl;             // display grade

    }// end for

    delete[] studentIDs;

    system("pause");
    return(0);
}

void readFile(ifstream& inFile)
{
    inFile.open("Ch12_Ex2Data.txt");        // use inFile to open Ch8_Ex6Data.txt

    if (!inFile)                            // if the file can't be opened or it is corrupt
    {
        cout << "There was an error opening the input file...\nPlease check file and try again!\n" << endl; // display error message

        system("pause");
        exit(1);                            // exit the program
    }
} // end readFile function

char assignGrade(int score, int numQues) 
{    
    double percentScore = static_cast<double>(score) / (numQues * 2);       // calculate the score percentage

    cout << "Score: " << percentScore * 100 << "%" << endl;                 // display the score

    if (percentScore >= 0.9)                // if score is greater than or equal to 90%, return A
        return 'A';
    else if (percentScore >= 0.8)           // if score is greater than or equal to 80%, return B
        return 'B';
    else if (percentScore >= 0.7)           // if score is greater than or equal to 70%, return C
        return 'C';
    else if (percentScore >= 0.6)           // if score is greater than or equal to 60%, return D
        return 'D';
    else                                    // any score lower thn 60%, return F
        return 'F';
} // end assignGrade function

它在调试器中不断破坏:     static void __CLRCALL_OR_CDECL assign(_Elem&amp; _Left,const _Elem&amp; _Right)     _NOEXCEPT         {//分配一个元素         _Left = _Right;         }

1 个答案:

答案 0 :(得分:0)

// Redo Programming Exercise Six of Chapter Eight
// Using Dynamic Arrays -- C++ Advanced

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

using namespace std;

// function prototype
void readFile(ifstream& inFile, string fileName);

int main()
{
    // variables, pointers and dynamic arrays
    int numStud = 200;                          // stores the number of students, max students was 200
    int numQues = 20;                           // stores the number of questions on test
    string *studentIDs;                         // pointer variable for studentIDs dynamic array
    char *correctAnswers;                       // pointer variable for correctAnswers dynamic array
    correctAnswers = new char[numQues];         // correctAnswers dynamic char array
    char *studentAnswers;                       // pointer variable for studentAnswers dynamic array
    studentAnswers = new char[numQues];         // studentAnswers dynamic char array
    char *fileName;                             // pointer variable for fileName dynamic char array
    fileName = new char[25];                    // fileName dynamic char array
    ifstream inFile;                            // input stream variable inFile

    // display message to explain the program
    cout << "\nJames Flowers - Chapter 12 - Programming Exercise 2" << endl;
    cout << "\nRedo Programming Exercise Six of Chapter Eight\nUsing Dynamic Arrays..." << endl;

    // request file name for fileName dynamic char array
    cout << "\nPlease Enter the File Name (Ch12_Ex2Data.txt): ";
    cin >> fileName;
    cout << endl;

    // call readFile function to read the file
    readFile(inFile, fileName);

    // request number of students for studentIDs dynamic char array and some calculations
    cout << "\nHow many students took this test? (4): ";
    cin >> numStud;
    cout << endl;

    studentIDs = new string[numStud];                       // create studentIDs dynamic char array

    inFile.getline(correctAnswers, '/n');                   // read the correct answers first

    for (int i = 0; i < numStud; i++)                       // loop students
    {
        inFile >> studentIDs[i];                            // get the student ID

        inFile.get();                                       // discard the blank space

        inFile.getline(studentAnswers, '/n');               // get the student's test answers

        cout << "Student ID: " << studentIDs[i] << endl;    // output student id

        int score = 0;                                      // declare and initialize score to zero

        cout << "Answers: ";                                // display "Answers: "

        for (int j = 0; j < numQues; j++)                   // loop each question
        {
            cout << studentAnswers[j];                      // output student's answers

            if (studentAnswers[j] == correctAnswers[j])     // if student answer is correct
                score += 2;                                 // add 2 to score
            else if (studentAnswers[j] != correctAnswers[j] && studentAnswers[j] != ' ')        // incorrect answer but not blank
                score -= 1;                                 // subtract 1 from score
            else if (studentAnswers[j] == ' ')              // if question left blank
                score -= 0;                                 // nothing subtracted from score

            studentAnswers[j] = ' ';                        // clear each indice for next student
        }// end for

        cout << endl;                                       // new line, housekeeping

        char grade = 0;                                     // char variable grade initialized to 0

        double percentScore = static_cast<double>(score) / (numQues * 2);  // calculate the score percentage

        cout << "Score: " << percentScore * 100 << "%" << endl;            // display the score

        if (percentScore >= 0.9)                // if score is greater than or equal to 90%, return A
            grade = 'A';                            // grade = A
        else if (percentScore >= 0.8)           // if score is greater than or equal to 80%, return B
            grade = 'B';                            // grade = B
        else if (percentScore >= 0.7)           // if score is greater than or equal to 70%,
            grade = 'C';                            // grade = C
        else if (percentScore >= 0.6)           // if score is greater than or equal to 60%,
            grade = 'D';                            // grade = D
        else                                    // any score lower than 60%,
            grade = 'F';                            // grade = F

        cout << "Grade: " << grade << "\n" << endl;     // display grade
    }// end for

    system("pause");    // pause for readability
    return(0);
}// end main

// readFile function reads the file, if not displays error message
void readFile(ifstream& inFile, string fileName)
{
    inFile.open(fileName);                      // use inFile to open input file

    if (!inFile.is_open())                      // if the file can't be opened or it is corrupt
    {
        cout << "There was an error opening the input file...\nPlease check file name and try again!\n" << endl;    // display error message

        system("pause");                        // pause for readability
        exit(1);
    }// end if
} // end readFile function