Switch statement not using "default"

Hi, I need some help with this program. This problem is from "Programming: Principles and Practice using C++" by Bjarne Stroustrup. The problem is, when there are no cases left, the default case doesn't run, the switch just exits. Is it because there are no operators left? How can I fix 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{	
	cout << "Please enter an expression (+ or - or * or /)\n";
	int lval = 0, rval, res;
	char op;

	cin >> lval;
	if (!cin)
		cerr << "No first operand.\n";
	while (cin >> op)
	{
		cin >> rval;
		if (!cin)
			cerr << "No second operand.\n";
		switch (op)
		{
		case '+':
			lval += rval;
			break;
		case '-':
			lval -= rval;
			break;
		case '*':
			lval *= rval;
			break;
		case '/':
			lval /= rval;
			break;
		default:
			cout << "Result: " << lval << endl;
			system("pause");
			return 0;
		}
	}
	cerr << "Bad expression.\n";

	system("pause");
    return 1;
}
Can you clarify your problem? What character are you entering, that you think should be triggering the default case, but isn't?
When there are no more operators, I want the default case to be used. From what I'm understanding, there needs to be some character present for this to happen instead of nothing. How can I get the default case to run when there are no operators left?
By "no operators left", do you mean that the user presses ENTER at the prompt without entering a character?

If so, then that causes the while loop to exit, which means that no code in your switch-case statement will execute. Anything that you want to happen when the loop exits, you'll have to put after the loop.
In practice, one could exit the loop while (cin >> op) by signalling 'end of input' by pressing ctrl-Z or ctrl-D. Normally however, pressing enter results in the prompt continuing to wait for more input. Any 'invalid' character will do - but then the next prompt cin >> rval; will be reached. Here, it is ok to enter anything at all - even if that cin statement fails, it doesn't matter when the default case is executed.
Thanks for the help. I realized that instead of printing the result from inside the default case, I just moved it to outside the while (cin >> op) loop. Here is the modified code, which works much better:

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
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int main()
{	
	cout << "Please enter an expression (+ or - or * or /)\n";
	int lval = 0, rval, res;
	char op;
	string choice;

	cin >> lval;
	if (!cin)
		cerr << "No first operand.\n";
	while (cin >> op)
	{
		cin >> rval;
		if (!cin)
			cerr << "No second operand.\n";
		switch (op)
		{
		case '+':
			lval += rval;
			break;
		case '-':
			lval -= rval;
			break;
		case '*':
			lval *= rval;
			break;
		case '/':
			lval /= rval;
			break;
		default:
			cout << "No operator.\n";
			system("pause");
			return 1;
		}
	}
	cout << "Result: " << lval << endl;
	
	system("pause");
    return 0;
}
Topic archived. No new replies allowed.