user3499156
user3499156

Reputation: 1

I can't get my switch function to work

Question is to sum up all values of 1 to 10, inclusive, excluding the values 3 and 6, however I have no idea why my switch function is not filtering out 3 and 6. Here is my code

#include <iostream>
using namespace std;

int main() 
{
    int a=1,total=0;

    while (a<=10)
    {
        a++;

        switch (a)
        {
            case 3:
                continue;
            case 6:
                continue;
            default:
                total = total + a;
        }
    }

    cout << "Sum of the total numbers are " << total << endl;

    return 0;
}

Upvotes: 0

Views: 354

Answers (2)

Chris Maes
Chris Maes

Reputation: 37742

Your code works correctly, you only make minor mistakes in your central loop: a goes from 2 to 11 instead of from 1 to 10. it should be like this:

int a=0, total=0;

while (a < 10)
{
    a++;

    // rest of code
}

EDIT so my answer is more complete. The fix above will get your code working so it produces the correct results, but as @Pete noted, continue is not the way to get out of a switch case statement. Your continue statement directly moves you back to the next loop of your while loop. A better and cleaner code would be like this:

int a=0,total=0;

while (a < 10)
{
    a++;
    switch (a)
    {
        case 3:
            break;
        case 6:
            break;
        default:
            total = total + a;
    }
    // in every case you will get here; even if a==3 or a==6
}

EDIT2 if you prefer to let a loop from 1 to 10, this is possible as well:

int a=1,total=0;

while (a <= 10)
{
    switch (a)
    {
        case 3:
            break;
        case 6:
            break;
        default:
            total = total + a;
    }
    // in every case you will get here; even if a==3 or a==6
    a++;
}

Upvotes: 1

Pete
Pete

Reputation: 6723

If you add the following to the end of your while loop:

cout << "Current total: " << total << " a=" << a << endl;

it will become clear what the issue is. Your output will look like this:

Current total: 2 a=2
Current total: 6 a=4
Current total: 11 a=5
Current total: 18 a=7
Current total: 26 a=8
Current total: 35 a=9
Current total: 45 a=10
Current total: 56 a=11
Sum of the total numbers are 56

As you can see, it's correctly skipping 3 and 6, but it's missing 1 and is adding 11, which are two things I don't think you were expecting.

Also, you're using continue. With a switch statement, you want to use break to keep from executing the cases after the current one. (To elaborate on this a bit, I think continue would have been fine because I think it was doing what you wanted: transferring control back to the while statement. This won't work if the a++ is moved after the switch statement, however. If you start a at 0, change your condition to a < 10 as mentioned in the other post, then you could use the continue statements instead of the break)

If you move a++; to the end of your while loop and fix the continue statements, I believe it will work as you expect.

Worried my edits might confuse the matter, here are two alternate ways you can structure the code to get the result you're looking for:

#include <iostream>
using namespace std;

int main() 
{
    int a=1,total=0;
    while (a<=10)
    {
        switch (a)
        {
            case 3:
                break;
            case 6:
                break;
            default:
                total = total + a;
        }
        a++;
    }
    cout << "Sum of the total numbers are " << total << endl;
    return 0;
}

or

#include <iostream>
using namespace std;

int main() 
{
    int a=0,total=0;
    while (a<10)
    {
        a++;
        switch (a)
        {
            case 3:
                continue;
            case 6:
                continue;
            default:
                total = total + a;
        }
    }
    cout << "Sum of the total numbers are " << total << endl;
    return 0;
}

Upvotes: 3

Related Questions