Home > Back-end >  Switch Statement Only Defaults c
Switch Statement Only Defaults c

Time:08-16

So I'm writing some code and I have this switch statement to set some bools in a 2d array. I have a nested for loop and a switch with switches in the nested loop setting on outer loop int i and inner loop int e. grammar[i][e] I'm switching on i and then on e but for some reason it just defaults on me. I have it printing out i and e.. and it still just defaults so there must be something wrong with my switch statements but I'm not seeing the issue here.

bool follows = false;
for (int i = 0; i < 12; i  )
{
    // 1 noun, 2 pronoun, 3 verb, 4 adverb, 5 adjective, 6 preposition, 7 conjunction, 8 determiner, 9 interjection, 10 exclamation, 11 notype, 12 auxiliary
    for (int e = 0; e < 12; e  )
    {
        //std::cout << " This is where we're at in grammar array "   std::to_string(i)   " and "   std::to_string(e);
        switch (i)
        {
            case 0: // noun // noun
                switch (e) {
                    case 0: // noun
                        follows = false;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = true;
                        break;
                    case 3: // adverb
                        follows = true;
                        break;
                    case 4: // adjective
                        follows = true;
                        break;
                    case 5: // preposition
                        follows = false;
                        break;
                    case 6: // conjunction
                        follows = true;
                        break;
                    case 7: // determiner
                        follows = true;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 1: // pronoun //pronoun
                switch (e) {
                    case 0: // noun
                        follows = false;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = true;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = true;
                        break;
                    case 5: // preposition
                        follows = true;
                        break;
                    case 6: // conjunction
                        follows = true;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 2: // verb // verb
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = true;
                        break;
                    case 2: // verb
                        follows = false;
                        break;
                    case 3: // adverb
                        follows = true;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = false;
                        break;
                    case 6: // conjunction
                        follows = false;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 3: // adverb //adverb
                switch (e) {
                    case 0: // noun
                        follows = false;
                        break;
                    case 1: // pronoun
                        follows = true;
                        break;
                    case 2: // verb
                        follows = true;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = true;
                        break;
                    case 5: // preposition
                        follows = false;
                        break;
                    case 6: // conjunction
                        follows = true;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 4: // adjective //adjective
                switch (e) {
                    case 0: // noun
                        follows = false;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = true;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = false;
                        break;
                    case 6: // conjunction
                        follows = true;
                        break;
                    case 7: // determiner
                        follows = true;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 5: // preposition //preposition
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = true;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = false;
                        break;
                    case 6: // conjunction
                        follows = true;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 6: // conjunction //conjunction
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = true;
                        break;
                    case 3: // adverb
                        follows = true;
                        break;
                    case 4: // adjective
                        follows = true;
                        break;
                    case 5: // preposition
                        follows = true; // change
                        break;
                    case 6: // conjunction
                        follows = false;
                        break;
                    case 7: // determiner
                        follows = false; // change
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 7: // determiner //determiner
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = true;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = true;
                        break;
                    case 6: // conjunction
                        follows = false;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 8: // interjection //interjection
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = false;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = true;
                        break;
                    case 6: // conjunction
                        follows = false;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 9: // exclamation //exclamation
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = false;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = true;
                        break;
                    case 6: // conjunction
                        follows = false;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 10: //NoType //NoType
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = false;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = true;
                        break;
                    case 6: // conjunction
                        follows = false;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = true;
                        break;
                    default:
                        break;
                }
            case 11: //auxiliary
                switch (e) {
                    case 0: // noun
                        follows = true;
                        break;
                    case 1: // pronoun
                        follows = false;
                        break;
                    case 2: // verb
                        follows = false;
                        break;
                    case 3: // adverb
                        follows = false;
                        break;
                    case 4: // adjective
                        follows = false;
                        break;
                    case 5: // preposition
                        follows = true;
                        break;
                    case 6: // conjunction
                        follows = false;
                        break;
                    case 7: // determiner
                        follows = false;
                        break;
                    case 8: // interjection
                        follows = true;
                        break;
                    case 9: // exclamation
                        follows = true;
                        break;
                    case 10: //NoType
                        follows = true;
                        break;
                    case 11: // auxiliary
                        follows = false;
                        break;
                    default:
                        break;
                }
            default:
                // TODO CHANGE THIS 
                std::cout << "Defaulting for some reason."   std::to_string(i)   " on i ";
                follows = true;
                break;
        }
        grammar[i][e] = follows;
        if (grammar[i][e]) { std::cout << std::to_string(i)   std::to_string(e)   " True "; }
        else { std::cout << std::to_string(i)   std::to_string(e)   " False "; }
    }
}

CodePudding user response:

You should put breaks after all bigger switch statements.

                            break;
                        default:
                            break;
                    }
                    break;  // <--- here
                case 1: // pronoun //pronoun
                    switch (e) {

CodePudding user response:

I forgot to add breaks to my initial outer switch statement cases. Saw it as soon as I asked the question. lol

  • Related