error & keep_window_open, WHAT classes?



I am with PPP using C++ book by Bjarne Stroustrup


Currently finishing touches to the calculator.cpp between theme 6 & 7

## a final WORKING version of the calculator using beginners code similar to the book would solve ALL my problems to be honest##


Ok... so;

I need to declare error & keep_window_open functions before using them but I don't know what class should I use

string error(); // (string) this one????

I tried to find error() function in std facilities but it wasn't explained there so basically I have no idea what its real return class is, same goes for keep_window_open which basically calls for error

why am I seeing this ==>> ("~1") <<== on his website files? in keep_window_open("~1")

my book's brackets for keep_window_open() are empty

I might need to declare a few other things, this is the code I have so far:


#include "../../std_lib_facilities.h

//------------------------------------------------------------------------------
// main > primary > expression > term ORDER moved to primary > term > expression > main


class Token {
public:
char kind; // what kind of token
double value; // for numbers: a value
Token(char ch) // make a Token from a char
:kind(ch), value(0) { }
Token(char ch, double val) // make a Token from a char and a double
:kind(ch), value(val) { }
};

class Token_stream {
public:
Token get(); // get a Token
void putback(Token t); // put a Token back
private:
bool full{ false }; // is there a Token in the buffer?
Token buffer; // here is where we keep a Token put back using putback()
};

//----------

void Token_stream::putback(Token t)
{
if (full) error("putback() into a full buffer");
buffer = t; // copy t to buffer
full = true; // buffer is now full
}

Token Token_stream::get() {
if (full) { // do we already have a Token ready?
full = false; // remove Token from buffer
return buffer;
}
char ch;
cin >> ch; // note that >> skips whitespace (space, newline, tab, etc.)

switch (ch) {
case ';': // for "print"
case 'q': // for "quit"
case '(': case ')': case '+': case '-': case '*': case '/':
return Token(ch); // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
cin.putback(ch); // put digit back into the input stream
double val;
cin >> val; // read a floating-point number
return Token('8', val); // let '8' represent "a number"
}
default:
error("Bad token"); // note that >> skips whitespace (space, newline, tab, etc.)
}
}

//-----------

Token_stream ts; // provides get() and putback()
double expression() // read and evaluate a Expression, declaration so that primary() can call expression()







// THIS WAS HERE DECLARED ON DOWNLOADED FILE NOT SURE IF ITS NEEDED?
double term(); // read and evaluate a Term

//------------------------------------------------------------------------------








double primary() // read and evaluate a Primary
{
Token t = get_token();
switch (t.kind) {
case '(': // handle '(' expression ')'
{
double d = expression();
t = get_token();
if (t.kind != ')') error("')' expected");
return d;
}
case '8': // we use '8' to represent a number
return t.value; // return the number's value
default:
error("primary expected");
}
}
//------------------------------------------------------------------------------

double term()
{
double left = primary();
Token t = get_token(); // get the next token

while (true) {
switch (t.kind) {
case '*':
left *= primary();
t = ts.get();
break;
case '/':
{
double d = primary();
if (d == 0) error("divide by zero");
left /= d;
t = ts.get();
break;
}
default:
ts.putback(t);
return left;
}
}
}

//------------------------------------------------------------------------------

double expression()
{
double left = term(); // read and evaluate a Term
Token t = get_token(); // get the next token
while (true) {
switch (t.kind) {
case '+':
left += term(); // evaluate Term and add
t = ts.get();
break;
case '-':
left -= term(); // evaluate Term and subtract
t = ts.get()
break;
default:
ts.putback(t); // put t back into the token stream
return left; // finally: no more + or -: return the answer
}
}
}

//------------------------------------------------------------------------------

int main()
try {
while (cin)
cout << expression() << '\n';







keep_window_open("~0"); // ~ 0 wasnt there on the book







}
catch (exception& e) {
cerr << e.what() << '\n'; // endl to '\n'
keep_window_open("~1"); // "~1" wasnt there
return 1;
}
catch (...) {
cerr << "exception \n";
keep_window_open("~2"); // ~ 0 wasnt there on the book
return 2;
}

//------------------------------------------------------------------------------

Extracted from http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

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
#include<iostream>
#include<string>
#include<stdexcept>

using namespace std;

// eg. error("putback() into a full buffer");
inline void error(const string& s)
{
	throw runtime_error(s);
}

inline void keep_window_open()
{
	cin.clear();
	cout << "Please enter a character to exit\n";
	char ch;
	cin >> ch;
	return;
}

// eg. keep_window_open("~1")
inline void keep_window_open(string s)
{
	if (s=="") return;
	cin.clear();
	cin.ignore(120,'\n');
	for (;;) {
		cout << "Please enter " << s << " to exit\n";
		string ss;
		while (cin >> ss && ss!=s)
			cout << "Please enter " << s << " to exit\n";
		return;
	}
}
Topic archived. No new replies allowed.