Loop and a half before runtime exception

I changed my 2D array call in the proto and def--no linker error this time. This time I tossed in a couple of cout's and found out my loop: read name-5 ints (2 digit)-name-3 ints and the first digit of the fourth int.

Then it threw a:
Unhandled exception at 0x01053ef6 in AssignmentNine.exe: 0xC0000005: Access violation writing location 0xcccccccc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Declaration in Funcs9Ex13.h
//	Header guard
#ifndef FUNCS9EX13_H
#define FUNCS9EX13_H
//[No namespace declared]
//	Include files

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

void fnReadNamesAndScores(std::ifstream&, std::string*, int*);

//Definition in Funcs9Ex13.cpp
#include "Funcs9Ex13.h"

using namespace std;

void fnReadNamesAndScores(ifstream &inFile, string OneDimArrOfStr[], int TwoDimArrOfInt[][NUMB_TESTS])
{
	int i = 0;
	while(inFile) {
		inFile >> OneDimArrOfStr[i];
		int score = 0, j = 0;
		while(score) {
			inFile >> TwoDimArrOfInt[i][j];
			j++;
		}
		i++;
	}
}

//Implementation in Grades9-13.cpp
#include "Funcs9Ex13.h"

using namespace std;

const int NUMB_STUDENTS = 10;
const int NUMB_TESTS = 5;
int main()
{
	ifstream ReadFile;
	fnOpenFiles(ReadFile, "ch9_Ex13Data.txt");

	string arrNames[NUMB_STUDENTS];
	int arrScores[NUMB_STUDENTS][NUMB_TESTS];

	fnReadNamesAndScores(ReadFile, arrNames, *arrScores);
	
		system("pause");
	return 0;


Now what am I doing wrong? (arrays, structs and classes have given me problems since I first tried learning C++ in 2002 before I became a mountain hermit for a few years. I'm still denting my brain pan against brick.)

Whether I get this fixed in time to turn it in tomorrow night, I WILL get it to work. Eventually.

ERandall
Run the debugger to see where the problem occurs.
The address 0xcccccccc smells like an uninitialized (pointer) variable.
I'm rather surprised that links.

int* is not the same type as int TwoDimArrOfInt[][NUMB_TESTS] so the code should fail to link.

I would suggest changing line 14 to

void fnReadNamesAndScores(std::ifstream&, std::string[], int[][NUMB_TESTS]);

and then changing line 50 to:

fnReadNamesAndScores(ReadFile, arrNames, arrScores);

Notice the lack of asterisk.

When you do:

fnReadNamesAndScores(ReadFile, arrNames, *arrScores);

the array name is demoted to type int* and then you dereference it, meaning you're passing the value of the first uninitialized int value and it's being interpreted as an address. In debug configurations (using VC++,) uninitialized variables contain the bit pattern for 0xcccccccc which explains your error message.


By the way, the while control condition on line 27 can never true.
Last edited on
Topic archived. No new replies allowed.