Newebie: Switch case statements inside a function??

Dear experts,
I would like to declare a function whose output is not an integer, this function actually shall convert an Integer number to some predefined string. I want this function specifically for modulation schemes exactly as stated below:

e.g
case:if the input to the function is 2: ouput of the function is "BPSK"
case:if the input to the function is 4: ouput of the function is "4PSK"
case:if the input to the function is 8: ouput of the function is "8PSK"

and so on for other schemes. but as a beginner I would like to try it with these 3 cases.

for giving the understanding idea. I tried to write something like below, but of course because of no knowledge for functions, this code is weirdly incomplete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;

mod_scheme(int);

int main ()
{
  int i;
  cout<<"Please enter a number from [2 or 4 or 8]"<<endl;
  cin >> i;
  
  string mod;
  mod=mod_scheme(i);
  cout<<"The Selected Modulation Scheme is: "<<mod<<endl;
}


mod_scheme(int i)

{  
  if (i==2)
    mod=2PSK
    if (i==4)
      mod=4PSK
      if(i==8)
        mod=8PSK
}



Best Regards
Ali
I don't know your intended use of the return values, but I'd start with returning a string. I'd also use a switch instead of a bunch of ifs.
You need a return type for the function. If it returns nothing, you need to put void before the function name in both the function prototype and definition.

And right now, the mod_scheme function doesn't know what the mod variable is - it's a local variable only visible in the main function. If you want the other function to be able to modify the content of mod, you would need to pass the mod variable by reference.

Or you can set up the function to return the string associated with the input number. You can also use a switch/case structure instead of the ifs.

1
2
3
4
5
6
7
8
{  
  if (i==2)
    return "2PSK";
  else if (i==4)
    return "4PSK";
  else if(i==8)
    return "8PSK";
}


I'd probably add in code to validate the number input - if they don't enter 2,4 or 8, you might want to prompt them to try again.
Last edited on
Lines 5 and 19; mod_scheme needs to declare what type it returns even if it's void. From your description, you probably want to return a string.

lines 23,25,27: mod is undefined here. You defined mod at line 13, but that is a local variable within main. mod_scheme can't reference that variable. Also, the statements are not valid. 2PSK, 4PSK and 8PSK need to be quoted and the statements need to be terminated with a ;

1
2
3
4
5
6
7
8
9
10
11
string mod_scheme (int i)
{  string mod;
    switch (i)
    {
    case 2: mod = "2PSK"; break;
    case 4: mod = "4PSK"; break; 
    case 8: mod = "8PSK"; break;
    default: // You probasbly should do something here
    }
    return mod;
}

Last edited on
Bundle of Thanks Members,
your suggestion meant a lot to me. I got the Idea of how to declare a function definition and it's prototype and I have achieved my desired output.

Thumbs UP. Wish you all a very nice day

Best Regards
Ali
Topic archived. No new replies allowed.