Switch Statement confusion...

I am trying to use a switch statement inside of a boolean funciton. What it is supposed to check is if variable score is between two numbers in each case, and act accordingly.

The problem is, I am not quite clear on how to do that.

in the past doing things like the following with the case statement constants

1
2
3

case > 0 || < 59:


...have gotten me nothing but errors. I want to use this to try and simplify the nested if-else structure I was originally using.

How exactly do I do what I am wanting to do, if I can do it at all?
You can't do that with a switch. (Switch is kind of useless in general I should add). You'll have to stick to the usual if-else thing. It's not that messy..
Switches cant do if statements like that, you have to use ifs for that. Switches are for exact values, so:

case 1:
case 2:
case 3:
case 4:
.
.
.
case 58:
COMMANDS
break;

However, they arent useless at all. They help alot in some games like mine:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "header.h"
#include "movechar.h"
#include "printmap.h"

void cmdcent()
{
 bool gameLoop = true;
 char cmd;
 while(gameLoop == true)
 {
  cmd = getch();
  switch(cmd)
  {
   case '8':
    movechar(0, -1, '0');
    break;
   case '2':
    movechar(0, 1, '0');
    break;
   case '4':
    movechar(-1, 0, '0');
    break;
   case '6':
    movechar(1, 0, '0');
    break;
   case '7':
    movechar(-1, -1, '0');
    break;
   case '9':
    movechar(1, -1, '0');
    break;
   case '1':
    movechar(-1, 1, '0');
    break;
   case '3':
    movechar(1, 1, '0');
    break;
   case '>':
    movechar(0, 0, '>');
    break;
   case '<':
    movechar(0, 0, '<');
    break;
   case ';':
    movechar(0, 0, ';');
    break;
   default:
    break;
  }
 }
}
Last edited on
Having tried this for myself, it would seem that it is not possible to structure Switch statements this way.

I want to use this to try and simplify the nested if-else structure

I'm trying to think of a better way to simplify your nested if-else statements but nothing really springs to mind.

Post your code for your nested if-else statements.
@b1gb0y2013
equivalent code:
1
2
3
4
5
6
7
8
9
int dir[9][2] = {
   {-1, 1}, {0, 1}, {1, 1},
   {-1, 0}, {0, 0}, {1, 0},
   {-1,-1}, {0,-1}, {1,-1}
};
if( cmd <= '9' && cmd >= '1' )
   movechar( dir[cmd-'1'][0], dir[cmd-'1'][1], '0' );
else if( cmd == ';' || cmd == '<' || cmd == '>' )
   movechar(0, 0, cmd);

Pretty useless, I'd say..
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
bool figureGrade( int score, char & grade)
{
if (score < 0 || > 100)
	cout << "Grade ont given, invalid score.\n";
	return false;
else
{
if (score >= 0 && < 60)
	grade = 'F';
	return true;
else
if (score >= 60 && < 70)
	grade = 'D';
	return true;
else
if(score >= 70 && < 80)
	grade = 'C';
	return true;
else
if(score >=80 && < 90)
	grade = 'B'
	return true;
else
if(score >=90 && <=100)
	grade = 'A';
	return true;
}


There is the code for the boolean function.


[WARNING: Topic Change Here]

After that I have a small problem with another, completely different program. This one is doing (what I assume) to be Array operations using I/O files, due to the nature of the functions given the by instructor. The first two variables of each function, I can understand, but there is an extra int variable that seems to be out of place to me.

void input (istream &, int [], int);

The rest of the three functions (which are, respectively): print, copyArray, and revCopy, also have this extra int variable, and I don't quite understand why it's there, or what it's purpose for the functions are. I understand everything else perfectly well, though.
Last edited on
Your bool figureGrade( int score, char & grade), function looks fine to me, I can't really see a way to improve upon it but other members may be able to comment further on it.

cout << "Grade ont given, invalid score.\n"; //not sure what an "ont" is? Did you mean int?

[WARNING: Topic Change Here]
Whoa! I feel warned :)

As for this mysterious int, maybe it defines the size of the array? Without more information I couldn't speculate further.

I hope this helps!
Well. I do know I am supposed to pass istream into the function as a reference variable, but I dunno exactly what entails either, as I have never used istream before...At least not like this.
Topic archived. No new replies allowed.