This is a homework assignment. I'm experiencing a weird error and I have no idea what is causing it. This is not the entire assignment, but it is what I have so far. As it's homework, I don't want someone to tell me exactly how to code it, rather I would like to know what is causing my crash and how I might be able to begin looking for how to solve it. My program seems to be crashing once it hits the return statement in the main method. All output appears to be exactly what I was trying to obtain, and my file is being read just fine. Once I hit enter after all my information has been printed, I'm presented with a message box that says "Unhandled exception at 0x5286ad84 (msvcp100d.dll) in Exams.exe: 0xC0000005: Access violation writing location 0x00000063." Thanks for any help anyone can provide.
#include <stdio.h>
#include <string>
#define MAX_STUDENTS 100
#define MAX_QUESTIONS 50
using namespace std;
int fGetAnswers(FILE *p_inputf, int p_studentIds[], string p_studentAnswers[]);
int fGetAnswers(int p_studentIds[], string p_studentAnswers[]);
void printData(int numOfStudents, int numOfQuestions, string *p_correctAnswers, int studentIds[], string studentAnswers[]);
int main(void)
{
// Declarations
FILE *inputf = fopen("examdat.txt", "r"); // Opens "examdat.txt" and creates a pointer to it.
int studentIds[MAX_STUDENTS];
int *p_ids = studentIds; // Pointer to the studentIds array to pass to functions for manipulation.
string studentAnswers[MAX_QUESTIONS];
string *p_answers = studentAnswers; // Pointer to the studentAnswers array to pass to functions for manipulation.
int numOfQuestions;
string correctAnswers;
int numOfStudents;
fscanf(inputf, "%d %s", &numOfQuestions, correctAnswers); // Fetches the first line from the exam.dat file which contains the number of questions and the correct answers.
int readFrom = 0;
while (readFrom != 1 && readFrom != 2) // Loops until proper input is received. Asks if the user wants to read student data from the text file or enter it manually.
{
printf("1. Read student data from file.\n");
printf("2. Enter student data manually.\n\n");
printf("Please make a selection> ");
scanf("%d", &readFrom);
if (readFrom != 1 && readFrom != 2)
printf("\nInvalid entry!\n");
}
if (readFrom == 1) // Calls fGetAnswers to retrieve answers from the text file.
numOfStudents = fGetAnswers(inputf, p_ids, p_answers);
else // Calls fGetAnswers to retrieve answers from the user via the keyboard.
numOfStudents = fGetAnswers(p_ids, p_answers);
fclose(inputf);
printData(numOfStudents, numOfQuestions, &correctAnswers, studentIds, studentAnswers);
getchar();
getchar();
return(0);
}
There may be lots of errors, using a debugger will help you track them down. But two I spotted are
printf("%d\t%s\n", numOfQuestions, *p_correctAnswers);
You cannot use C++ strings with printf, only C strings. This will work
printf("%d\t%s\n", numOfQuestions, p_correctAnswers->c_str());
The c_str method returns a C style string from a C++ style string.
And another similar error
int result = fscanf(p_inputf, "%d %s", &p_studentIds[i], p_studentAnswers[i]);
you cannot use C++ strings with fscanf. This one is hard to solve. Since you want to use C++ strings (which is a smart move) I think you should also learn some C++ I/O, look up iostreams.
Since you're using a C++ compiler, you can undo a few C habits (earlier the better!)
#include <iostream> // for std::cout and std::cin
using namespace std::cin;
using namespace std::cout;
using namespace std::endl;
Instead of printf (and the issue that john pointed out regarding C style strings), use:
cout << numOfQuestions << "\t" << correctAnswers << endl;
You can read about cout here
User contributions licensed under CC BY-SA 3.0