I have a task where I expect random input of chars
or ints
. Is there any way I can understand what is coming next and say cin >> int
, or cin >> char
. I want to do it with the istream operator >>
, because they told us not to do it with a parser. To be precise, I expect an input of a prefix expression. So I don't know when to read an operator, when to read a number. If you have any suggestions I'd appreciate them :)
Asked
Active
Viewed 74 times
0
-
Could you wack up an example to help us with the exact file format? I'm not sure what you mean by the input being a char or an int. – Bathsheba Nov 03 '16 at 14:41
-
By `char`, do you mean "not a digit"? (Digits are characters, too.) – molbdnilo Nov 03 '16 at 14:41
-
1read it in as a `std::string` and parse it from there. – NathanOliver Nov 03 '16 at 14:42
-
@molbdnilo Yes, thats exactly what I mean :D – Petar D. Nov 03 '16 at 14:42
-
@Bathsheba prefix example: `- * 8 5 + * 20 4 * + 5 8 10` – Petar D. Nov 03 '16 at 14:44
-
@user7091569 May be [this](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) helps you a bit. – πάντα ῥεῖ Nov 03 '16 at 14:47
-
Aren't stacks used for the "prefix" expression? – Omid CompSCI Nov 03 '16 at 14:56
-
@OmidCompSCI yes they are, but how can I understand if I read an operand or an operator? – Petar D. Nov 03 '16 at 14:57
-
@user7091569, it doesn't matter, because you keep pushing to the stack while checking if it is an operand. When it is an operand you pop from the stack two numbers and use the operand. – Omid CompSCI Nov 03 '16 at 15:01
-
@OmidCompSCI so you tell me to push the stack with a broken input? :) – Petar D. Nov 03 '16 at 19:16
-
@user7091569 how is it broken all you are doing is pushing to the stack and checking if there is an operand, once there is an operand you pop the numbers and use the operation with those popped numbers. You always have to check if the number of operands and numbers are suffice to carry out the expression, else you would throw an error. Unless you are validating the users input one character at a time using some do-while or something, but that is usually not what people do.... Let the user enter what they want and carryout the operation, if can't be done print that out to the user. – Omid CompSCI Nov 03 '16 at 19:34
-
@OmidCompSCI how can I predict what to read here: `+ 5 4` ,`* + 5 4 3` First time i have operator, operands, next time i have 2 operator, then operands – Petar D. Nov 03 '16 at 19:39
-
@user7091569 lets just assume it is postfix like 54+, so in a stack you put character at a time, so you check if 5 is operand if not push, so your stack has 5. You then check if 4 is operand its not, push, so your stack looks like 5 4. You check if next is operand its a + so you pop the two numbers and it is 5 4, and use the operand 5 + 4 = 9. And you can push that back on the stack if need be and have more operations. – Omid CompSCI Nov 03 '16 at 19:55
-
@user7091569 This is postfix expression, I hope you can understand this and be able to implement it using prefix very similar concept. Of course you should have something where you are making sure there is enough operands or numbers. – Omid CompSCI Nov 03 '16 at 19:56
-
@OmidCompSCI imagine I have a number more then 1 digit, lets say 180. Then I want to divide it by 90. If I work with chars, I would need parser. It is much simpler to use normal operations on `int` right? I think you haven't read my question carefully – Petar D. Nov 03 '16 at 20:02
-
@user7091569 as 'character' I meant everything up to a space correct? So if you had 180 and want to divide by 90, it would look like, 180 90 /. Push 180 push 90 pop 180 90 and use the operand 180 / 90. You would know this if you created your stack class and can change the type of stack you use, instead of character can use strings. Making a generic stack class would be the solution to this. – Omid CompSCI Nov 03 '16 at 20:11
-
@OmidCompSCI then I'd have to use `atoi` for strings, which is not generally what I wanted. – Petar D. Nov 03 '16 at 20:13
-
@user7091569 I see, however atoi is a very reliable built in function, also not relly considered parsing, technically just a typecasting lol, but it is a solution that you can use if none other is out there. Sorry man! – Omid CompSCI Nov 03 '16 at 20:14
1 Answers
-1
You can do, without a parser:
int n;
cin >> n;
if (cin.good())
{
// manage integer
}
else
{
char c;
cin.clear();
cin >> c;
// manage character
}

Garf365
- 3,619
- 5
- 29
- 41
-
-
Upvoted. Possibly the best way, subject to the artificial no-parsing constraint. – Bathsheba Nov 03 '16 at 14:52
-
-
Yeah, good solution, except [when you read an integer and then hit EOF](http://coliru.stacked-crooked.com/a/0e47aed78aed49b8). This is in fact a blunt instrument with more failure cases than you can shake a stick at. Don't do parsing like this! /cc @Bathsheba – Lightness Races in Orbit Nov 03 '16 at 15:06
-
@LightnessRacesinOrbit I don't really understand when `std::cin` can reach end of file – Garf365 Nov 03 '16 at 15:18
-
@LightnessRacesinOrbit Also I agree with you, I always prefer `std::getline` and a parser but OP's requirements is explicitly a solution without parser, just `std::cin` – Garf365 Nov 03 '16 at 15:21
-
@Garf365: It has to eventually. Admittedly the testcase I presented doesn't reproduce for `std::cin` (and off the top of my head I'm actually not sure why that is). – Lightness Races in Orbit Nov 03 '16 at 17:34