Error I've Never Encountered before.

Hello,

I have an error that I am having a hard time comprehending because there isn't a lot of explanation, and I have tried searching for hours for similar problems but it doesn't seem like a lot of people have gotten this error.

I am creating a Roman Numeral class (homework, but i have all of the coding done) and it is giving me a reading location error.

roman.h:
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
#ifndef ROMAN_H
#define ROMAN_H
#include <string>

using namespace std;

class roman
{
private:
	string numeral;

public:
	// Constructors
	roman();
	roman(string);
	roman(int); // Extra Credit

	// Mutator
	bool setNumeral(string);

	// Accessor
	string getNumeral();

	// Other Functionality
	int toInt();
	void print();

};

#endif 


roman.cpp (implementation file)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "roman.h"
#include <iostream>

using namespace std;

//default constructor
roman::roman()
{
	numeral = "";
}

//non-default constructor
roman::roman(string num)
{
	numeral = num;
}

roman::roman(int decNum)
{
	int t=0, h=0, ten=0, o=0;
	string thousand [5] = { "", "M", "MM", "MMM", "MMMM"};
	string hundred [10] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
	string tens [10]= {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
	string ones [10] = {"", "I" ,"II","III","IV","V","VI","VII","VIII","IX"};

	t = decNum % 1000;
	decNum -= t * 1000;
	h = decNum % 100;
	decNum -= h *100;
	ten = decNum % 10;
	decNum -= ten * 10;
	o = decNum % 1;

	numeral = thousand[t] + hundred[h] + tens[ten] + ones[o];
}

bool roman::setNumeral(string num)
{
	numeral = num;
	return true;
}

string roman::getNumeral()
{
	return numeral;
}

int roman::toInt()
{
	int sum = 0, prev = 1000;
	string tempNum = numeral;

	for(int i = 0; i < tempNum.length(); i++)
	{
		switch (numeral[i])
		{
		case 'M':
			sum += 1000;
			if(prev < 1000)
				sum -= 2 * prev;
			prev = 1000;
			break;
		case 'D':
			sum += 500;
			if(prev < 500)
				sum -= 2 * prev;
			prev = 500;
			break;
		case 'C':
			sum += 100;
			if(prev < 100)
				sum -= 2 * prev;
			prev = 100;
			break;
		case 'L':
			sum += 50;
			if(prev < 50)
				sum -= 2 * prev;
			prev = 50;
			break;
		case 'X':
			sum += 10;
			if(prev < 10)
				sum -= 2 * prev;
			prev = 10;
			break;
		case 'V':
			sum += 5;
			if(prev < 5)
				sum -= 2 * prev;
			prev = 5;
			break;
		case 'I':
			sum += 1;
			prev = 1;
			break;
		}
	}


	return sum;
}

void roman::print()
{
	cout << " " << numeral;
}





Driver: romanDrive.cpp
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
#include <iostream>
#include <conio.h>
#include <string>
#include "roman.h"

using namespace std;

int main()
{
	// Create three roman objects (tests all three constructors)
	roman r1("MCMIX"), r2, r3(1084);

	// Test the print functions
	cout << "Roman r1 = ";
	r1.print();
	cout << "\nRoman r2 = ";
	r2.print();
	cout << "\nRoman r3 = ";
    r3.print();

	// Test the accessor
	string data = r3.getNumeral();
	cout << "\nRoman r3 string = " << data << endl;

	// Test the mutator
	r3.setNumeral("MMCDXXIX");
	cout << "Updated roman r3 = ";
	r3.print();

	// Test the "other functions"
	cout << " with a numerical value of " << r3.toInt() << endl;
	cout << r1.toInt();
	_getch();
}


Then I get this error (included a picture so that you can see my Visual C++ set up, and the error screen)
http://tinypic.com/r/f1lx4/6

for those that can't click the pic:
this is the error I get:
Unhandled exception at 0x00ae2ff6 in Roman.exe: 0xC0000005: Access violation reading location 0x00350250.

and it pulls up the xstring include file and points to this line of code:
1
2
3
4
	size_type size() const
		{	// return length of sequence
		return (this->_Mysize);
		}

specifically the return line.

Please help!
You're stepping out of bounds of your lookup tables. Take a closer look at your roman constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
roman::roman(int decNum)
{
	int t=0, h=0, ten=0, o=0;
	string thousand [5] = { "", "M", "MM", "MMM", "MMMM"};  // maximum 5
	string hundred [10] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; // maximum 10
	string tens [10]= {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
	string ones [10] = {"", "I" ,"II","III","IV","V","VI","VII","VIII","IX"};

	t = decNum % 1000;  // <- double check these calculations
	decNum -= t * 1000;
	h = decNum % 100;
	decNum -= h *100;
	ten = decNum % 10;
	decNum -= ten * 10;
	o = decNum % 1;

	// at this point, if t>=5 or if h>=10 (etc), you will go out of bounds of the above
	//  array and the program may explode like you're seeing.
	numeral = thousand[t] + hundred[h] + tens[ten] + ones[o];
}



Note my comments.

Your calculations there are wrong. Remember that % gives you the remainder after division.

So let's plug in an example number. Let's say that decNum = 1539:

1
2
3
4
5
// decNum = 1539
t = decNum % 1000;  // t = 539
decNum -= t * 1000;  // decNum -= 539000, so decNum = -537461

// ... etc 


See the problem?
Topic archived. No new replies allowed.