-3

So for my comp sci class I was tasked with adding, subtracting, dividing, and multiplying Roman Numerals if they pass 12 rules that I have been given.

I created a 13 element boolean array to keep track of which rules were broken. I created a function called checkErrors, which calls each of the 12 rule checking functions. I call the checkErrors to check each roman numeral before the calculation is done and print out which of the errors is wrong (if any) but instead of just printing out the ones that are wrong it prints each one.

Below is a snippet from checkErrors which is the same for each rule (checkErrorOne, checkErrorTwo...etc.). If the rule is broken, errors[0] is set to false to show that the whole roman numeral is not legal.

`bool checkErrors(string romanNumeral, bool * errors) {
if (checkErrorOne(romanNumeral) == false) {
    errors[1] = false;
    errors[0] = false;
}
if (checkErrorTwo(romanNumeral) == false) {
    errors[2] = false;
    errors[0] = false;
}
if (checkErrorThree(romanNumeral) == false) {
    errors[3] = false;
    errors[0] = false;
}
if (checkErrorFour(romanNumeral) == false) {
    errors[4] = false;
    errors[0] = false;
}
if (checkErrorFive(romanNumeral) == false) {
    errors[5] = false;
    errors[0] = false;
}
if (checkErrorSix(romanNumeral) == false) {
    errors[6] = false;
    errors[0] = false;
}
if (checkErrorSeven(romanNumeral) == false) {
    errors[7] = false;
    errors[0] = false;
}
if (checkErrorEight(romanNumeral) == false) {
    errors[8] = false;
    errors[0] = false;
}
if (checkErrorNine(romanNumeral) == false) {
    errors[9] = false;
    errors[0] = false;
}
if (checkErrorTen(romanNumeral) == false) {
    errors[10] = false;
    errors[0] = false;
}
if (checkErrorEleven(romanNumeral) == false) {
    errors[11] = false;
    errors[0] = false;
}
if (checkErrorTwelve(romanNumeral) == false) {
    errors[12] = false;
    errors[0] = false;
}

if (errors[0] == true)
    return true;
else if (errors[0] == false)
    return false;

} `

to return the value of the first element to say if it is legal.

After this when I go to print each rule that was broken I use the following:

void printFunction(string romanNumeral1, string romanNumeral2, string mathOperator, bool errors[]){
if (checkErrors(romanNumeral1, errors) == false && checkErrors(romanNumeral2, errors) == true){

    cout << romanNumeral1 << " is illegal becasue" << endl;
    if (errors[1] == false)
        cout << "Contains a letter that is not M, D, C, L, X, V, or I;" << endl;
    if (errors[2] == false)
        cout << "I is not followed by I, V, or X;" << endl;
    if (errors[3] == false)
        cout << "X is not followed by I, V, X, L, or C;" << endl;

for each of the 12 rules. It is printing out every one of the errors, not each one that is wrong.

If someone could help with my logic it would be greatly appreciated.

void printFunction(string romanNumeral1, string romanNumeral2, string mathOperator, bool errors[]){
if (checkErrors(romanNumeral1, errors) == false && checkErrors(romanNumeral2, errors) == true){

    cout << romanNumeral1 << " is illegal becasue" << endl;
    if (errors[1] == false)
        cout << "Contains a letter that is not M, D, C, L, X, V, or I;" << endl;
    if (errors[2] == false)
        cout << "I is not followed by I, V, or X;" << endl;
    if (errors[3] == false)
        cout << "X is not followed by I, V, X, L, or C;" << endl;

Edit: Why does the code I wrote print out each of the 12 errors I created instead of only printing out the ones that were violated?

Thanks

Zack Sloan
  • 133
  • 1
  • 1
  • 8

2 Answers2

3

Off-topic: Your program can be simplified by having an array of error messages.

The if statement ladder would be replaced by a loop:

for (unsigned int i = 0U; i < MAXIMUM_ERROR_TYPES; ++i)
{
  if (errors[i])
  {
    std::cout << error_messages[i];
  }
}

Since there is less code, there will be less possibility of injected defects.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Ok, first of all, you didn't post your checkErrors code, so i am assuming here. I think that you defined is as

bool checkErrors(string, bool errors[13]){

which means, that it does not take a pointer, but a copy of the 13 booleans. So they get modified within your function, but not outside it. Change it to:

bool checkErrors(string, bool* errors){

ps. Next time, please think about how people with no knowledge about your poblem will understand it. Saves you a lot of flak ;)

David van rijn
  • 2,108
  • 9
  • 21