Skip to content

Commit 68fe0cb

Browse files
committed
fixed case without operators
1 parent 218cece commit 68fe0cb

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

chapter_5/exercise_5_10/expr.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void push(float element);
1313

1414
int main(int argc, char *argv[]) {
1515
char Error = 0;
16+
int op_count = 0;
1617

1718
// Using int instead of size_t for loop variable to match signed argc
1819
// This prevents sign comparison warnings
@@ -29,19 +30,23 @@ int main(int argc, char *argv[]) {
2930
char op = *argv[i];
3031
switch (op) {
3132
case '+':
33+
op_count++;
3234
push(number1 + number2);
3335
break;
3436

3537
case '-':
38+
op_count++;
3639
push(number1 - number2);
3740
break;
3841

3942
case '*': // This char might require to be escaped when passed
4043
// as an argument.
44+
op_count++;
4145
push(number1 * number2);
4246
break;
4347

4448
case '/':
49+
op_count++;
4550
if (number2 == 0) {
4651
Error = 4;
4752
} else {
@@ -87,8 +92,13 @@ int main(int argc, char *argv[]) {
8792

8893
return EXIT_FAILURE;
8994
}
90-
91-
printf("result: %.3f", pop());
95+
96+
if (op_count == 0 && argc > 2) {
97+
printf("Error: Operator is missing!");
98+
return EXIT_FAILURE;
99+
} else {
100+
printf("result: %.3f", pop());
101+
}
92102

93103
return EXIT_SUCCESS;
94104
}

0 commit comments

Comments
 (0)