Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Switch Statement Only Defaults c++

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 "; }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You should put breaks after all bigger switch statements.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading