Roman Numeral conversion program throwing an exception. Newbie question












-3















The exception is "Exception thrown at 0x0F71514F (vcruntime140d.dll) in ConsoleApplication28.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC. occurred"



It seems to have something to do with the three lines



            lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;


and for the life of me I can't figure out what the problem is. I can take out these lines and it works ok although it's incomplete. Any help would be appreciated.



    #include <iostream>
#include <string>


using namespace std;

string toLongRoman(int a);
string toLower(string s);

int main() {

int input = 1, error = 1, oneColumn[35];
string lowerOutput, lowerOutputSecond, twoColumn[35][2];



do {
cout << "Please enter a number between 1 and 5000: ";
cin >> input;
if (input >= 1 && input <= 5000) {
error = 0;
break;
}
else {
cout << "Invalid input, try again." << endl;
error = 1;
cin.clear();
cin.ignore(80, 'n');
}
} while (error == 1);

for (int i = 0; i < 35; ++i)
{
oneColumn[i] = input;
twoColumn[i][1] = toLongRoman(input);
lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;

input = input + 1;

}


for (int c = 0; c < 20; ++c) {
cout << oneColumn[c] << " " << twoColumn[c][1] << " " <<
twoColumn[c][2] << endl;
}


}


string toLongRoman(int a) {
int b, stringPos = 0;
char romanString[100] = "0";

while (a >= 1000) {
b = a / 1000;
if (b >= 1) {
romanString[stringPos] = 'M';
stringPos++;
a = a - 1000;
}
else {
break;
}
}

while (a >= 500) {
b = a / 500;
if (b >= 1) {
romanString[stringPos] = 'D';
stringPos++;
a = a - 500;
}
else {
break;
}
}

while (a >= 100) {
b = a / 100;
if (b >= 1) {
romanString[stringPos] = 'C';
stringPos++;
a = a - 100;
}
else {
break;
}
}

while (a >= 50) {
b = a / 50;
if (b >= 1) {
romanString[stringPos] = 'L';
stringPos++;
a = a - 50;
}
else {
break;
}
}

while (a >= 10) {
b = a / 10;
if (b >= 1) {
romanString[stringPos] = 'X';
stringPos++;
a = a - 10;
}
else {
break;
}
}

while (a >= 5) {
b = a / 5;
if (b >= 1) {
romanString[stringPos] = 'V';
stringPos++;
a = a - 5;
}
else {
break;
}
}

while (a >= 1) {
b = a / 1;
if (b >= 1) {
romanString[stringPos] = 'I';
stringPos++;
a = a - 1;
}
else {
break;

}
}

return romanString;

}

string toLower(string s)
{
char result[50] = { 0 };

for (unsigned int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
result[i] = s[i];

}



return result;
}









share|improve this question


















  • 3





    Just FYI -- You are taking an overly complex way of converting a number to Roman Numerals. A simple table and some division/logic is all that's required. Should be 15 lines or less.

    – PaulMcKenzie
    Nov 24 '18 at 22:49













  • Since the issue seems to be with your code that has nothing to do with the function to convert to roman numerals, might as well show you a simple Roman Numeral program. I leave it to you to understand what it's doing and why it works.

    – PaulMcKenzie
    Nov 24 '18 at 23:40













  • 0xCCCCCCCC = Uninitialized Stack memory stackoverflow.com/questions/127386/…

    – drescherjm
    Nov 25 '18 at 0:02


















-3















The exception is "Exception thrown at 0x0F71514F (vcruntime140d.dll) in ConsoleApplication28.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC. occurred"



It seems to have something to do with the three lines



            lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;


and for the life of me I can't figure out what the problem is. I can take out these lines and it works ok although it's incomplete. Any help would be appreciated.



    #include <iostream>
#include <string>


using namespace std;

string toLongRoman(int a);
string toLower(string s);

int main() {

int input = 1, error = 1, oneColumn[35];
string lowerOutput, lowerOutputSecond, twoColumn[35][2];



do {
cout << "Please enter a number between 1 and 5000: ";
cin >> input;
if (input >= 1 && input <= 5000) {
error = 0;
break;
}
else {
cout << "Invalid input, try again." << endl;
error = 1;
cin.clear();
cin.ignore(80, 'n');
}
} while (error == 1);

for (int i = 0; i < 35; ++i)
{
oneColumn[i] = input;
twoColumn[i][1] = toLongRoman(input);
lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;

input = input + 1;

}


for (int c = 0; c < 20; ++c) {
cout << oneColumn[c] << " " << twoColumn[c][1] << " " <<
twoColumn[c][2] << endl;
}


}


string toLongRoman(int a) {
int b, stringPos = 0;
char romanString[100] = "0";

while (a >= 1000) {
b = a / 1000;
if (b >= 1) {
romanString[stringPos] = 'M';
stringPos++;
a = a - 1000;
}
else {
break;
}
}

while (a >= 500) {
b = a / 500;
if (b >= 1) {
romanString[stringPos] = 'D';
stringPos++;
a = a - 500;
}
else {
break;
}
}

while (a >= 100) {
b = a / 100;
if (b >= 1) {
romanString[stringPos] = 'C';
stringPos++;
a = a - 100;
}
else {
break;
}
}

while (a >= 50) {
b = a / 50;
if (b >= 1) {
romanString[stringPos] = 'L';
stringPos++;
a = a - 50;
}
else {
break;
}
}

while (a >= 10) {
b = a / 10;
if (b >= 1) {
romanString[stringPos] = 'X';
stringPos++;
a = a - 10;
}
else {
break;
}
}

while (a >= 5) {
b = a / 5;
if (b >= 1) {
romanString[stringPos] = 'V';
stringPos++;
a = a - 5;
}
else {
break;
}
}

while (a >= 1) {
b = a / 1;
if (b >= 1) {
romanString[stringPos] = 'I';
stringPos++;
a = a - 1;
}
else {
break;

}
}

return romanString;

}

string toLower(string s)
{
char result[50] = { 0 };

for (unsigned int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
result[i] = s[i];

}



return result;
}









share|improve this question


















  • 3





    Just FYI -- You are taking an overly complex way of converting a number to Roman Numerals. A simple table and some division/logic is all that's required. Should be 15 lines or less.

    – PaulMcKenzie
    Nov 24 '18 at 22:49













  • Since the issue seems to be with your code that has nothing to do with the function to convert to roman numerals, might as well show you a simple Roman Numeral program. I leave it to you to understand what it's doing and why it works.

    – PaulMcKenzie
    Nov 24 '18 at 23:40













  • 0xCCCCCCCC = Uninitialized Stack memory stackoverflow.com/questions/127386/…

    – drescherjm
    Nov 25 '18 at 0:02
















-3












-3








-3








The exception is "Exception thrown at 0x0F71514F (vcruntime140d.dll) in ConsoleApplication28.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC. occurred"



It seems to have something to do with the three lines



            lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;


and for the life of me I can't figure out what the problem is. I can take out these lines and it works ok although it's incomplete. Any help would be appreciated.



    #include <iostream>
#include <string>


using namespace std;

string toLongRoman(int a);
string toLower(string s);

int main() {

int input = 1, error = 1, oneColumn[35];
string lowerOutput, lowerOutputSecond, twoColumn[35][2];



do {
cout << "Please enter a number between 1 and 5000: ";
cin >> input;
if (input >= 1 && input <= 5000) {
error = 0;
break;
}
else {
cout << "Invalid input, try again." << endl;
error = 1;
cin.clear();
cin.ignore(80, 'n');
}
} while (error == 1);

for (int i = 0; i < 35; ++i)
{
oneColumn[i] = input;
twoColumn[i][1] = toLongRoman(input);
lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;

input = input + 1;

}


for (int c = 0; c < 20; ++c) {
cout << oneColumn[c] << " " << twoColumn[c][1] << " " <<
twoColumn[c][2] << endl;
}


}


string toLongRoman(int a) {
int b, stringPos = 0;
char romanString[100] = "0";

while (a >= 1000) {
b = a / 1000;
if (b >= 1) {
romanString[stringPos] = 'M';
stringPos++;
a = a - 1000;
}
else {
break;
}
}

while (a >= 500) {
b = a / 500;
if (b >= 1) {
romanString[stringPos] = 'D';
stringPos++;
a = a - 500;
}
else {
break;
}
}

while (a >= 100) {
b = a / 100;
if (b >= 1) {
romanString[stringPos] = 'C';
stringPos++;
a = a - 100;
}
else {
break;
}
}

while (a >= 50) {
b = a / 50;
if (b >= 1) {
romanString[stringPos] = 'L';
stringPos++;
a = a - 50;
}
else {
break;
}
}

while (a >= 10) {
b = a / 10;
if (b >= 1) {
romanString[stringPos] = 'X';
stringPos++;
a = a - 10;
}
else {
break;
}
}

while (a >= 5) {
b = a / 5;
if (b >= 1) {
romanString[stringPos] = 'V';
stringPos++;
a = a - 5;
}
else {
break;
}
}

while (a >= 1) {
b = a / 1;
if (b >= 1) {
romanString[stringPos] = 'I';
stringPos++;
a = a - 1;
}
else {
break;

}
}

return romanString;

}

string toLower(string s)
{
char result[50] = { 0 };

for (unsigned int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
result[i] = s[i];

}



return result;
}









share|improve this question














The exception is "Exception thrown at 0x0F71514F (vcruntime140d.dll) in ConsoleApplication28.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC. occurred"



It seems to have something to do with the three lines



            lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;


and for the life of me I can't figure out what the problem is. I can take out these lines and it works ok although it's incomplete. Any help would be appreciated.



    #include <iostream>
#include <string>


using namespace std;

string toLongRoman(int a);
string toLower(string s);

int main() {

int input = 1, error = 1, oneColumn[35];
string lowerOutput, lowerOutputSecond, twoColumn[35][2];



do {
cout << "Please enter a number between 1 and 5000: ";
cin >> input;
if (input >= 1 && input <= 5000) {
error = 0;
break;
}
else {
cout << "Invalid input, try again." << endl;
error = 1;
cin.clear();
cin.ignore(80, 'n');
}
} while (error == 1);

for (int i = 0; i < 35; ++i)
{
oneColumn[i] = input;
twoColumn[i][1] = toLongRoman(input);
lowerOutput = twoColumn[i][1];
lowerOutputSecond = toLower(lowerOutput);
twoColumn[i][2] = lowerOutputSecond;

input = input + 1;

}


for (int c = 0; c < 20; ++c) {
cout << oneColumn[c] << " " << twoColumn[c][1] << " " <<
twoColumn[c][2] << endl;
}


}


string toLongRoman(int a) {
int b, stringPos = 0;
char romanString[100] = "0";

while (a >= 1000) {
b = a / 1000;
if (b >= 1) {
romanString[stringPos] = 'M';
stringPos++;
a = a - 1000;
}
else {
break;
}
}

while (a >= 500) {
b = a / 500;
if (b >= 1) {
romanString[stringPos] = 'D';
stringPos++;
a = a - 500;
}
else {
break;
}
}

while (a >= 100) {
b = a / 100;
if (b >= 1) {
romanString[stringPos] = 'C';
stringPos++;
a = a - 100;
}
else {
break;
}
}

while (a >= 50) {
b = a / 50;
if (b >= 1) {
romanString[stringPos] = 'L';
stringPos++;
a = a - 50;
}
else {
break;
}
}

while (a >= 10) {
b = a / 10;
if (b >= 1) {
romanString[stringPos] = 'X';
stringPos++;
a = a - 10;
}
else {
break;
}
}

while (a >= 5) {
b = a / 5;
if (b >= 1) {
romanString[stringPos] = 'V';
stringPos++;
a = a - 5;
}
else {
break;
}
}

while (a >= 1) {
b = a / 1;
if (b >= 1) {
romanString[stringPos] = 'I';
stringPos++;
a = a - 1;
}
else {
break;

}
}

return romanString;

}

string toLower(string s)
{
char result[50] = { 0 };

for (unsigned int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
result[i] = s[i];

}



return result;
}






c++ exception






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 22:46









BaldDonkeyBaldDonkey

11




11








  • 3





    Just FYI -- You are taking an overly complex way of converting a number to Roman Numerals. A simple table and some division/logic is all that's required. Should be 15 lines or less.

    – PaulMcKenzie
    Nov 24 '18 at 22:49













  • Since the issue seems to be with your code that has nothing to do with the function to convert to roman numerals, might as well show you a simple Roman Numeral program. I leave it to you to understand what it's doing and why it works.

    – PaulMcKenzie
    Nov 24 '18 at 23:40













  • 0xCCCCCCCC = Uninitialized Stack memory stackoverflow.com/questions/127386/…

    – drescherjm
    Nov 25 '18 at 0:02
















  • 3





    Just FYI -- You are taking an overly complex way of converting a number to Roman Numerals. A simple table and some division/logic is all that's required. Should be 15 lines or less.

    – PaulMcKenzie
    Nov 24 '18 at 22:49













  • Since the issue seems to be with your code that has nothing to do with the function to convert to roman numerals, might as well show you a simple Roman Numeral program. I leave it to you to understand what it's doing and why it works.

    – PaulMcKenzie
    Nov 24 '18 at 23:40













  • 0xCCCCCCCC = Uninitialized Stack memory stackoverflow.com/questions/127386/…

    – drescherjm
    Nov 25 '18 at 0:02










3




3





Just FYI -- You are taking an overly complex way of converting a number to Roman Numerals. A simple table and some division/logic is all that's required. Should be 15 lines or less.

– PaulMcKenzie
Nov 24 '18 at 22:49







Just FYI -- You are taking an overly complex way of converting a number to Roman Numerals. A simple table and some division/logic is all that's required. Should be 15 lines or less.

– PaulMcKenzie
Nov 24 '18 at 22:49















Since the issue seems to be with your code that has nothing to do with the function to convert to roman numerals, might as well show you a simple Roman Numeral program. I leave it to you to understand what it's doing and why it works.

– PaulMcKenzie
Nov 24 '18 at 23:40







Since the issue seems to be with your code that has nothing to do with the function to convert to roman numerals, might as well show you a simple Roman Numeral program. I leave it to you to understand what it's doing and why it works.

– PaulMcKenzie
Nov 24 '18 at 23:40















0xCCCCCCCC = Uninitialized Stack memory stackoverflow.com/questions/127386/…

– drescherjm
Nov 25 '18 at 0:02







0xCCCCCCCC = Uninitialized Stack memory stackoverflow.com/questions/127386/…

– drescherjm
Nov 25 '18 at 0:02














1 Answer
1






active

oldest

votes


















0














Arrays are 0 based, so twoColumn[i][2] = lowerOutputSecond is out of bounds in the second dimension - change the [2] to be [1] to be in bounds (and change the other [1] in your code to be [0]






share|improve this answer
























  • You are my new favorite person! Thank you muchly that did it!

    – BaldDonkey
    Nov 24 '18 at 23:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463013%2froman-numeral-conversion-program-throwing-an-exception-newbie-question%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Arrays are 0 based, so twoColumn[i][2] = lowerOutputSecond is out of bounds in the second dimension - change the [2] to be [1] to be in bounds (and change the other [1] in your code to be [0]






share|improve this answer
























  • You are my new favorite person! Thank you muchly that did it!

    – BaldDonkey
    Nov 24 '18 at 23:41
















0














Arrays are 0 based, so twoColumn[i][2] = lowerOutputSecond is out of bounds in the second dimension - change the [2] to be [1] to be in bounds (and change the other [1] in your code to be [0]






share|improve this answer
























  • You are my new favorite person! Thank you muchly that did it!

    – BaldDonkey
    Nov 24 '18 at 23:41














0












0








0







Arrays are 0 based, so twoColumn[i][2] = lowerOutputSecond is out of bounds in the second dimension - change the [2] to be [1] to be in bounds (and change the other [1] in your code to be [0]






share|improve this answer













Arrays are 0 based, so twoColumn[i][2] = lowerOutputSecond is out of bounds in the second dimension - change the [2] to be [1] to be in bounds (and change the other [1] in your code to be [0]







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 23:10









Ian4264 Ian4264

13514




13514













  • You are my new favorite person! Thank you muchly that did it!

    – BaldDonkey
    Nov 24 '18 at 23:41



















  • You are my new favorite person! Thank you muchly that did it!

    – BaldDonkey
    Nov 24 '18 at 23:41

















You are my new favorite person! Thank you muchly that did it!

– BaldDonkey
Nov 24 '18 at 23:41





You are my new favorite person! Thank you muchly that did it!

– BaldDonkey
Nov 24 '18 at 23:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463013%2froman-numeral-conversion-program-throwing-an-exception-newbie-question%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks