switch statement compile time error

I'm trying to make a calculator that follows precedence rules,I'm following Bjarnes book anyway I get an error that I have never encountered before the error is with the switch statement but more particularly with the case '8' and default case

the error is as follows jump to case label [-fpermissive]

but I can't see anything wrong the code looks fine to me

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  #include <iostream>
#include <vector>
using namespace std;

double term();

class Token{

public:

	char kind;
	double value;

	Token(char kind)
	:kind(kind),value(0)
	{}
	Token(char kind,double value)
	:kind(kind),value(value)
	{}
};

vector<Token> tokens;
int number = 0;

void populateTokens(){

	cout << "enter an expression" << endl;

	int number;
	char op;

	while(cin >> number >> op){

		Token token1('8',number);
		tokens.push_back(token1);

		if(op == 'q'){
			return;
		}

		Token token2(op);
		tokens.push_back(token2);

	}
}

Token getToken(){

	for(int i = 0; i < tokens.size(); i++){

		if(i == number){

			return tokens[i];
		}
	}

	return NULL;
}

double expression(){

	int left = term();
	Token t = getToken();

    while(true){

    	switch(t.kind){

    	case '+': left += term();
    	break;
    	case '-': left -= term();
    	break;
    	default : return left;

    	}
    }
}

double term(){

	double left = primary();
	Token t = getToken();

	while(true){

		switch(t.kind){

		case '*' :
			left *= primary();
			break;
		case '/' :
			left /= primary();
			break;
		default :
			return left;

		}
	}
}

int primary(){

      Token t = getToken();
      switch(t.kind){
      case '(' :
    	  double d = expression();
    	  t = getToken();
          if(t.kind != ')'){
        	  cout << "error" << endl;
          }
          return d;
      case '8':
    	  return t.value;
      default: cout << "error" << endl;

      }
      }

}


int main() {




}
Last edited on
so the error was that I didn't have an enclosing scope braces after the case,

I never knew you had to add the { } when declaring variables or objects inside a case label,

so the question is why do you need to enclose them inside { }? the switch statement has an enclosing braces so why do need another?
so the question is why do you need to enclose them inside { }? the switch statement has an enclosing braces so why do need another?


Without them, case 8 can access the variable double d because it is within the same scope. But what is the value of d? The line 106 where the variable would have been initialised does not get executed for case 8.
that makes sense thanks =)
Topic archived. No new replies allowed.