jeudi 19 mars 2015

Outputting an array and it skips one element


So I will be honest, I am very new to programming and I have read other boards that have this same topic and I am unable to find where I might be messing up.


For my program I am inputting a text file of a multiple choice quiz into two different arrays. One array holds the quiz questions and the multiple choice answers (so it is a 2D array), while the other array holds the answers. These are then outputted to the user and the user can take the quiz and then get the results of their quiz at the end.


So I've done probably the most basic debugging of stepping through my program and making sure that my file loaded into my arrays properly (which it did) so I don't think it's an issue with loading my file into the arrays, but how they're being outputted.


So what's my problem?



  1. When I start to output my quiz and the user starts to take it the first question comes up fine (as well as the multiple choice answers) and the answer to it is correct, BUT when it moves on to the second question it skips and outputs the third question instead, but still outputs question 2's multiple choice answers, which throws the whole test off. So essentially it is outputting array[2][0] and then the rest of the elements in row 1, instead of array[1][0] and then the rest of the elements in row 1. So all the questions are off by one.


Does anyone have any ideas as to why it's only skipping that one element of the array but reading out everything else just fine? Thank you for your help in advance!


Below is the loops for my codes, everything has been declared and initialized.



int countRow = 0; //Counter for the rows in the array
int countCol = 0; //Counter for the columns in the array
const int ARRAY_ROW = 11; //Rows in the array
const int ARRAY_COL = 6; //Columns in the array
string line; //String variable to put into the array

//Filling the 2D array and the answer array
for (countRow = 0; countRow < ARRAY_ROW; countRow++)
{
for (countCol = 0; countCol < (ARRAY_COL); countCol++)
{
getline(inFile, line);
testArr[countRow][countCol] = line;
}

//Filling the answer array
if (countCol == 6)
{
getline(inFile, line);
testAnswers[countRow] = line;
}

}


string choice; //Variable for user input
string answer; //Variable for the correct answer
int rightAnswer = 0; //Accumulator for the right answer
int row = 0; //Counter for rows
int col = 0; //Counter for columns
int questionNum = 1; //Number of the question
char option = 'A'; //Lettering options for the user to choose from


cout << "Select the appropriate option for your desired answer." << endl;
cout << endl;
cout << endl;

for (row = 0; row < ARRAY_ROW; row++)
{
cout << "Question " << questionNum << ": " << testArr[row][col] << endl;
cout << endl;
questionNum++;

for (col = 1, option = 'A'; col < ARRAY_COL, option < 'F'; col++, option++)
{
cout << option << " - " << testArr[row][col] << endl;
}

cout << endl;
answer = testAnswers[row];
cin >> choice;
cout << endl;

if (choice == answer)
{
cout << "Correct, you chose the right answer: " << answer << endl;
cout << endl;
cout << endl;
rightAnswer++;
}

else
{
cout << "Incorrect, the correct answer is: ";
cout << answer << endl;
cout << endl;
cout << endl;
}
}



Aucun commentaire:

Enregistrer un commentaire