Tae
Tae

Reputation: 1695

Two conditions that don't work the same way and I can't understand why

I wrote a function that searches for a char in an array, and if found returns its successor, else returns -1. Then, if the word ends in a vowel, the algorithm adds a consonant, and vice-versa.

This code works well, even with the last word of the file:

changedChar = cipherChar(character, consonants, tConsonants);
if (changedChar != -1) charType = 'c';
else {
    changedChar = cipherChar(character, CONSONANTS, tConsonants);
    if (changedChar != -1) charType = 'c';
    else {
        changedChar = cipherChar(character, vowels, tVowels);
        if (changedChar != -1) charType = 'v';
        else {
            changedChar = cipherChar(character, VOWELS, tVowels);
            if (changedChar != -1) charType = 'v';
            else {
                changedChar = cipherChar(character, others, tOthers);
                if (changedChar != -1) charType = 'o';
                else {
                    changedChar = changeDigit(character);
                    if (changedChar != -1) charType = 'o';
                    else {
                        changedChar = cipherChar(character, punctuation, tPunctuation);
                        if (changedChar != -1) charType = 'o';
                    }
                }
            }
        }
    }
}
if (changedChar != -1) outFile << changedChar;
    if (searchChar(inFile.peek(), punctuation, tPunctuation) > -1)
        if (charType == 'v') {
            outFile << consonants[nVowel];
            nVowel < 4 ? nVowel++ : nVowel = 0;
        }
        else if (charType == 'c') {
            outFile << vowels[nConsonant];
    nConsonant < 20 ? nConsonant++ : nConsonant = 0;
        }

But this other don't adds an extra letter after the last word of the file:

charType = 'c';
changedChar = cipherChar(character, consonants, tConsonants);
if (changedChar == -1) {
    changedChar = cipherChar(character, CONSONANTS, tConsonants);
    if (changedChar == -1) {
        charType = 'v';
        changedChar = cipherChar(character, vowels, tVowels);
        if (changedChar == -1) {
            changedChar = cipherChar(character, VOWELS, tVowels);
            if (changedChar == -1) {
                charType = 'o';
                changedChar = cipherChar(character, others, tOthers);
                if (changedChar == -1) {
                    changedChar = changeDigit(character);
                    if (changedChar == -1) changedChar = cipherChar(character, punctuation, tPunctuation);
                }
            }
        }
    }
}
if (changedChar != -1) outFile << changedChar;
    if (searchChar(inFile.peek(), punctuation, tPunctuation) > -1)
        if (charType == 'v') {
            outFile << consonants[nVowel];
            nVowel < 4 ? nVowel++ : nVowel = 0;
        }
        else if (charType == 'c') {
            outFile << vowels[nConsonant];
    nConsonant < 20 ? nConsonant++ : nConsonant = 0;
        }

Why? I'm really confused.

Upvotes: 1

Views: 170

Answers (2)

ildjarn
ildjarn

Reputation: 62975

The only difference I see is that in the second piece of code if nothing is found then charType has the value 'o', whereas in the first piece of code if nothing is found then charType has whatever its initial value was before this code runs (which you haven't shown us).

Other than that, these two pieces of code appear semantically identical, so if this isn't the cause of your problem then the problem lies in code you haven't shown us.

Upvotes: 3

CharithJ
CharithJ

Reputation: 47520

If there are this many if conditions in a logic, most probably there should be a flaw in the function design. Understand your requirement properly and try to simplify the logic.

Upvotes: 5

Related Questions