If-else within switch cases being ignored?

Hi everyone! I'm having a problem using switch with a nested if-else statement. When I run the program, the output corresponds to just the first line of the case (the first assignment). The if-else statement appears to be completely skipped over, and I can't figure out what I'm missing here.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Función moverse

#include <stdlib.h>
#include <iostream>
using namespace std;

// Declaración de función
void Moverse(int &fila, int &columna, int fil, int colum, const int norte, const int sur, const int oeste, const int este, char direccion, int token);

int main()
{   
    int fila = 5;
    int columna = 5;
    int fil = 1;
    int colum = 1;
    int token = 0;
    const int norte = 1;
    const int sur = (-1);
    const int oeste = (-1);
    const int este = 1;
    char direccion;
    
    Moverse(fila, columna, fil, colum, norte, sur, oeste, este, direccion, token);
    cout << "jugador en " << fila << columna;
 
    return 0;
}

// Definición de función
void Moverse(int &fila, int &columna, int fil, int colum, const int norte, const int sur, const int oeste, const int este, char direccion, int token)
{
    
    cout << "¿En qué dirección te mueves? (n)orte, (s)ur, (e)ste, (o)este: ";
    
                do
                {
                    cin >> direccion;
                    
                                switch(direccion)
                                {   
                                    case 'n': colum = (columna+norte);
                                        if(colum<=5||colum>=1){
                                            columna = colum;
                                            token = 0;
                                        }
                                        else
                                        {
                                            cout << "¡Te has dado contra una pared!\n";
                                            colum = (colum-norte);
                                            token = 1;
                                        }
                                        break;
                                    case 's': colum = (columna+sur);
                                        if(colum<=5||colum>=1){
                                            columna = colum;
                                            token = 0;
                                        }
                                        else
                                        {
                                            cout << "¡Te has dado contra una pared!\n";
                                            colum = (colum-norte);
                                            token = 1;
                                        }
                                        break;
                                    case 'e': fil = (fila+este);
                                        if(fil<=5||fil>=1){
                                        fila = fil;
                                        token = 0;
                                        }
                                        else
                                        {
                                            cout << "¡Te has dado contra una pared!\n";
                                            fil = (fil-este);
                                            token = 1;
                                        }
                                        break;
                                    case 'o': fil = (fila+oeste);
                                        if(fil<=5||fil>=1){
                                        fila = fil;
                                        token = 0;
                                        }
                                        else
                                        {
                                            cout << "¡Te has dado contra una pared!\n";
                                            fil = (fil-este);
                                            token = 1;
                                        }
                                        break;
                                    default: cout << "Error: Debe introducir una dirección válida. Vuelva a elegir.\n";
                                }
                }
                while(token==1);
}
Well if(fil<=5||fil>=1) is always going to be true.

Maybe you meant && rather than ||
You have a lot of repetition. Perhaps an idea like this?
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
char directions[256] = {0};
directions['n'] == directons['N'] = 1;
directions['s'] == directons['S'] = -1;
directions['e'] == directons['E'] = 1;
directions['w'] == directons['W'] = -1; //oeste 

... switch... etc

case 'n': 
case 's': 
case 'N': //do you want the capitalized letters to work?
case 'S':
colum = (columna+directons[direccion]);
    if(colum<=5||colum>=1)
	{
        columna = colum;
        token = 0;
    }
    else
    {
        cout << "¡Te has dado contra una pared!\n";
        colum = (colum-directons['n']); //is this correct?  or is it [direccion]?
//is line 61 above correct with norte?
        token = 1;
    }
    break;
Last edited on
Maybe you meant && rather than ||


That...is exactly what I was missing...

Is it normal to feel this silly sometimes when you first start programming? LOL. Maybe this is where I should really start trying to flowchart programs as I write, might help me not make these little oversights...

Thank you!
to feel this silly sometimes when you first start programming?


What do you mean 'when you first start programming' ? Even us old-timers make silly mistakes! :)
jonnin, thank you for the suggestion!

That is an array using char values, right? We have only just started with arrays this week so it's not 100% necessary to use them for this hand-in, and the teacher also specified that we don't need to include the capitalised letters for this project.

I would love to try using the arrays though so I may give it a shot inspired by your idea if I have time in between studying for two tests on Friday and finishing another programming hand-in for Sunday...

As I understand what you wrote, I would need to still write out each possible 'case' value but then I write the full code for each case at the bottom which will then pull the value from 'directions'?

Also in line 61 (as in 49, 73, and 85) I was actually trying to make sure the value of colum or fil doesn't affect the following tries at choosing 'direccion', but now that I look over it again I realise it wouldn't matter anyway since in the first line I always assign it the new value of 'columna'/'fil'+'direccion'...!
Last edited on
What do you mean 'when you first start programming' ? Even us old-timers make silly mistakes! :)


Aaw, thanks for the encouragement. <3 :)
its very easy to scramble your logic operators for anyone. I do it an unfortunate amount of the time; earlier today I could not for the life of me get why a != should have been == and stared at it for like 20 min. Old age indeed.

ok so what did I do there...

line is is an array of characters, yes. its actually a lookup table of all the possible letters in a 256 bit char type (the default of char). I initialized all these values to zero, and then overrode the ones for directions. So if someone types 'Z' you get zero, but if they type a compass direction, they get a value.

it means that when the user types a direction, you can immediately get your value to add (these used to be norte, sur, etc). That is, I have *directly* associated n with 1, and the variable norte isnt needed anymore: if the user typed 'n' you can fetch that it is a one without any if statements or other logic, just look up the value.
This lets you rewrite the nearly identical switch statements to all be the same -- they behave like a parameter driven function now -- one block of code does the work, and it changes slightly depending on the variables. Do you see what I mean?

As I understand what you wrote, I would need to still write out each possible 'case' value but then I write the full code for each case at the bottom which will then pull the value from 'directions'?

Yes, that is partially correct. But if you keep looking, THE ONLY REASON FOR THE SWITCH STATEMENT NOW is to give you a default case if the user typed some letter NOT acceptable as a direction. Or you can ditch the switch entirely and validate the input another way. Either way is fine. Switch statements are nice when you want to check both upper and lower case of a letter, so keeping a switch gives you that and its probably a little cheaper than converting it or checking both letters with a condition.

the lookup table wastes a little bit of memory to simplify the code. There are times when that is not a great idea, but modern hardware and 256 bytes of memory is not even a blip on most machines...
Last edited on
Topic archived. No new replies allowed.