case switch

Jul 27, 2019 at 4:59am
I'm working on an assignment and I was wondering if you can call functions in case switches? Maybe a dumb questions forgive me. I have a question about overload functions but I think I should one question at a time for my brain. lol

Jul 27, 2019 at 5:04am
Yes and yes.

And they are better called "switch cases" instead of "case switches".
Jul 27, 2019 at 5:11am
Thank you, and after each case, its a good idea to use a break. Is that correct?
Jul 27, 2019 at 5:14am
Absolutely. You pretty much always want to use a break at the end of a case.
Jul 27, 2019 at 5:14am
You can use any function in the control expression of the switch:

1
2
3
4
switch (f(x))
{
  ...
}

However, the branch expressions must have constant value. Hence, you may use literals, const values, or constexpr expressions (which include constexpr functions).


The nature of your question, however, leads me to think you are a little confused about the purpose of a switch statement. It is simply a convenience form to reduce a bunch of if...else if... statements where the only thing that changes is the value being compared. Compare:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Harder to read and maintain
if (x == 1)
{
  ...
}
else if (x == 2)
{
  ...
}
else if (x == 3)
{
  ...
}
else
{
  ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Easier to read and maintain
switch (x)
{
  case 1:
    ...
    break;

  case 2:
    ...
    break;

  case 3:
    ...
    break;

  default:
    ...
}

Essentially, it is a branch-lookup device built into C and C++ syntax.
More reading at Wikipedia: https://en.wikipedia.org/wiki/Switch_statement

Hope this helps.
Last edited on Jul 27, 2019 at 5:15am
Jul 27, 2019 at 5:16am
I was assuming you meant calling functions within the case code, not the case test. As Duthomhas says, the actual case tests need to be constant (they can't even be a variable).
Jul 27, 2019 at 5:19am
It does, I forgot about that default. I need to figure out what my default should be lol. I have to do a couple of overload functions too lol. I'm stuck on that too. I get the basic concept but you never really have to do basic ones when you are doing an assignment lol.

For example I'm trying make an overload function dealing with pay salary. the user must enter their information as far as hours and what not. I know the formula to get their salary, but I don't know how to execute it in an overload functions lol.

Jul 27, 2019 at 6:07am
@dutch, I was meaning calling functions inside the case. Lets say for example my function I want to call is GetData(). I'm terrible at examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// Easier to read and maintain
switch (x)
{
  case 1:
    ...GetData(); //Could you do that?
    break;

  case 2:
    ...
    break;

  case 3:
    ...
    break;

  default:
    ...
}

Jul 27, 2019 at 6:45am
Yes, you can definitely use a function there.
Jul 27, 2019 at 6:55am
ok thank you. This is really helping. Its so hard to get help with this stuff. Obviously you want to learn, but its hard not just giving the answer when it comes to coding lol. So if you are trying to do an overload function. I'm trying to do something with salary. Like what ever the user inputs for amount and I would divide it by 52 which is 52 weeks. I get the logic and the math, but its different when you are trying to make it into an overload function. lol
Jul 27, 2019 at 7:39am
oh could you call a function inside an overload function? lol. That help in my overload functions
Jul 27, 2019 at 9:40am
> could you call a function inside an overload function?

Yes.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

// overloaded function with 2 args
double max_of( int a, int b ) { return a<b ? b : a ; }

// overloaded function with 3 args
int max_of( int a, int b, int c )
{
    // call overloaded function with 2 args (twice)
    return max_of( a, max_of(b,c) ) ;
}

// overloaded function with 4 args
int max_of( int a, int b, int c, int d )
{
    // call overloaded function with 3 args, then overloaded function with 2 args
    return max_of( a, max_of(b,c,d) ) ;
}

int main()
{
    std::cout << max_of( 23, 45, 42, 17 ) << '\n' ; // 45
}

http://coliru.stacked-crooked.com/a/a3c4e2fa3c8056d5
Jul 27, 2019 at 10:03am
is it the only purpose for an overload function to pass different type of variables? I making a salary calculation, so i just made everything a double variable.

1
2
3
4

void getEmployeesSalary(double &); //this is for the employee to enter the amount 
                                                          //I have seven employees
                                                         


I am required to make an overload function. So I was just thinking of calling the function getEmployeesSalary(); inside the overload function and some how make it : getEmployeesSalary() and divide it by 52. lol. Guys, I'm probably extremely off here.
Jul 27, 2019 at 1:17pm
Can you post the full assignment? I think we could give you more useful info if you do. I'm wondering if you're supposed to overload the function or override it. Those are two very different things in C++.

And another small thing, for you getEmployeeSalary(), it would probably be better as double getEmployeeSalary();. That way you can put it inside an expression.
Jul 27, 2019 at 2:33pm
you mean like my instructions?
Jul 27, 2019 at 2:34pm
i had it like my teacher had some examples

I had it like this: void getEmployeeSalary(double &);
Jul 27, 2019 at 5:27pm
I think you are overthinking this.

Yes, post your instructions and we can give you very straightforward advice that will help simplify your thoughts.
Topic archived. No new replies allowed.