Elijah
Elijah

Reputation: 15

C++ Debug Assertion Failed string arrays

I am coding an assignment for my class where a user will input 10 letter answers, and the program will return a grade. I recently changed my char arrays to string arrays, because I think it makes it easier to read. I went to debug my code and am now getting the error "Deubug Assertion Failed." I do not know what this means or how to fix it.

Any help would be appreciated. Thanks!

Below is my code:

// Lab 8
// programmed by Elijah Barron

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


//Function headers
string inputAnswers(string given);
int numCorrect(string correctAnswers, string given);

int main()
{
    string correctAnswers = "BCADBADCAB";
    string given;
    int numRight = 0;

    inputAnswers(given);

    numCorrect(correctAnswers, given);

    double grade = 10 * numRight;

    cout << "Your quiz grade is " << grade << "%" << endl;

    return 0;
}
//Get the answers


string inputAnswers(string given)
{
    for (int n = 0; n < 10; n++)
    {
        cout << "Please enter your answer for question #" << n + 1 << " ";
        cin >> given[n];
    }
    return given;
}

//Find if answers are correct or incorrect

int numCorrect(string correctAnswers, string given)
{
    int numRight = 10;
    int n = 0;
    for (int n = 0; n < 10; n++);
    {
        if (given[n] != correctAnswers[n])
            numRight -= 1;
    }

    return numRight;
}

Upvotes: 0

Views: 665

Answers (2)

splrs
splrs

Reputation: 2432

The immediate issue is that given will start off as an empty string as you haven't assigned it a value:

cin >> given[n];

is causing the assert failure because you're trying to change the first (second, third etc) character in a string with a length of zero. To fix the assert problem (but not the program, which will always return 0%), just initialise the string:

string given = "ZZZZZZZZZZ";


To fix the rest of the stuff (btw this isn't the only way):

Change:

string inputAnswers(string given); //for both prototype and function.

to:

void inputAnswers(string& given); //pass by reference instead of pass by value.
//also get rid of "return given;"

Change:

int n = 0; //the n here is different to the one in the next line
for (int n = 0; n < 10; n++); //this n's scope begins and ends here thanks to the semicolon
{//the code here is executed once, this isn't in the loop!
    if (given[n] != correctAnswers[n]) //we're using the first n here, which is 0.
        numRight -= 1;
}

to:

for (int n = 0; n < 10; n++) //only one n variable and no semicolon
{// now this is in the loop and will execute 10 times.
    if (given[n] != correctAnswers[n])
        numRight -= 1;
}

Don't bother with this line:

int numRight = 0; //Set at 0 and then never changed.

and change:

numCorrect(correctAnswers, given);

to:

int numRight = numCorrect(correctAnswers, given); //declared when necessary and assigned the correct value

Upvotes: 1

iwolf
iwolf

Reputation: 1062

You either want to reserve enough space in your vector to hold 10 characters, or use push_back to populate the vector. Indexing a vector with [] won't grow the vector for you.

EDIT:

Ignore the first part about reserve. That doesn't stop the debug assertion. You will want to change this

cin >> given[n];

To something like this:

char input;
cin >> input;
given.push_back(input);

Upvotes: 0

Related Questions