I've got a fairly complex calculator that prints output when the user inputs ";" (and hits enter.) What I'm trying to do now is allow the user to print output when they hit enter, (without use of semicolon.) I need to know how I can implement this.
Side note: The calculator uses tokenization to read user input
This is part of the calculator's source, and the only part that needs to be changed. It is a member function of Token_stream that is called when various other functions of the calculator call to get the next token.
const char let = '#';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sq_rt = '@';
const char power = '^';
const char constant = '~';
Token Token_stream::get()
{
if (full) { full=false; return buffer; }
char ch;
cin >> ch;
switch (ch) {
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '%':
case ';':
case '=':
case ',':
case constant:
case power:
case sq_rt:
case let:
case quit:
case name:
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.unget(); // equivalent of putback
double val;
cin >> val;
return Token(number,val);
}
default:
if (isalpha(ch)) { // in order to create variable names
string s;
s += ch;
while(cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_')) s += ch;
cin.unget();
if (s == "const") return Token(constant);
if (s == "quit") return Token(quit);
if (s == "pow") return Token(power);
if (s == "sqrt") return Token(sq_rt);
if (s == "let") return Token(let);
if (s == "name") return Token(name);
return Token(name,s);
}
else if (isspace(ch)) { // my attempt at allowing enter to produce output
string s;
cin.get(ch);
s += ch;
if(s == "\n") return Token(print);
}
}
error("Bad token");
}
So far if the user enters 5*5 and hits space then enter..nothing happens. However if they hit space, then enter a random character (any character, not just ;) then hit enter, it ouputs the answer. After this, the user can use enter to produce output, but it saves the extra token that the user input after they hit space, so the order of operations gets all screwy.
In short, I need to know how to allow the user initially to hit space then enter (or even just enter, but working with space and enter for now) to produce output - without any additional characters.
If you'd like to see more source or have any questions regarding the calculator itself in order to solve the problem I'd be happy to post more.
P.S. The original program is from a book I'm learning C++ from. I've just been doing exercises that change it and add features to it.