differentiating 1 from x as a string

Hello, I'm given an assignment where I need to implement a compiler for a language called Simple. for instance given an expression "y = x * 1" I need to output

lvalue y
rvalue x
push 1
*
=

so I need to know if there's an integer involved so I have write it as "push int". That's why I need to know if there's something I can represent as a double or int, so I can treat them differently.

Thanks for your replies.
There are a bunch of functions lying around that you can use for this, isdigit being the most obvious. Alternately, you could just try converting it to a double or something straight off and test to see if the task succeeded. For example:
1
2
3
4
5
6
7
8
9
std::string s;
std::cin >> s;

try {
    double val = std::stod(s);
    std::cout << "push " << val;
} catch (const std::invalid_argument&) {
    std::cout << "lvalue " << s;
}
Last edited on
big thanks for your answer, I wasn't quite familiar with exception handling in c++, thanks for that as well!
Topic archived. No new replies allowed.