Calculator Not Showing Result

So I copied this code from the Programming Principles book by Stroustrup on how to make a calculator but I cannot make the result display. Using visual studio 2013. Typing something like 5+5 does not yield results. But the book says typing something like 1+2*3 should work.

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
  #include "std_lib_facilities.h"


int main()
{
	cout << "Enter an expression." << endl;
	int val1=0;
	int val2;
	char op;
	cin >> val1;
	if (!cin) error("No first operand");
	while (cin >> op){
		cin >> val2;
		if (!cin) error("no second operand");
		switch (op){
		case'+':
			val1 += val2;
			break;
		case'-':
			val1 -= val2;
			break;
		case'*':
			val1 *= val2;
			break;
		case'/':
			val1 /= val2;
			break;
		default:
			cout << "Result:" << val1 << endl;
			keep_window_open();
				return 0;
		}
	}
	error("bad expression");


}
Last edited on
That's a weird style of code but you have to use #include <iostream> to use cin and cout
Maybe this is just meant as an example for me in the book and I'm not supposed to compile it yet. I'll just keep reading for now and possibly edit the question later on.
did you try doing #include <iostream>?
yes. I tried
1
2
#include <iostream>
using namespace std;


But then visual studio tells me I get errors for these lines of code because identifier error is unspecified.
 
error("Not first operand ")


I use the std_lib_facilities because from my understanding it makes it so I don't have to use <iostream>, <string>, etc.
The error() function isn't part of the standard library. It's a Stroustrup special, something he wrote himself and put in std_lib_facilities.h
Topic archived. No new replies allowed.