Thread: Stringstream runs into infinite loop

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    but what do you mean about the enum?....would it be of any advantage over the bool?
    At the moment, the bool just stores information about whether the token is or is not a double. You could do a little more and store information about whether the token, if it is not a number, is exactly what kind of operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    At the moment, the bool just stores information about whether the token is or is not a double. You could do a little more and store information about whether the token, if it is not a number, is exactly what kind of operator.
    I'm a little confused...
    Suppose the input is:
    sin 45 + cos 30

    Now... how should the enum behave when it encounters the "sin"?
    and what should be be the part of the constructor dealing with the enum be like?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Now... how should the enum behave when it encounters the "sin"?
    There could be an enumerator for a mathematical function that takes an argument.

    Quote Originally Posted by manasij7479
    and what should be be the part of the constructor dealing with the enum be like?
    Code that determines what the string value is, e.g., it could just be string comparison.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    What will your calculator make of, say: 5 + 2 * 3? Is it 21?
    Do you think about implementing brackets too? To be able to calculate: (5 + 2) * (2 - 5 * ( 4 + 12) ).

    If you answer "yes" to both or at least the second of those questions, then... Well, it may seem as an overkill at first, but implementing Dijkstra's infix to postfix algorithm and then evaluating it as an expression in Reverse Polish Notation might be just what you want.
    Adding new operators (and support for variables) is very easy once you get through those two algorithms.

    Shunting-yard algorithm - Wikipedia, the free encyclopedia
    Reverse Polish notation - Wikipedia, the free encyclopedia

  5. #20
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Great idea....THANKS.....though the first
    Quote Originally Posted by Xupicor View Post
    What will your calculator make of, say: 5 + 2 * 3? Is it 21?
    is a no...it would be 11..(I'd have the inputs in brackets...so as not to mess up priority orders).....otherwise...If possible I'd write a function to order the input into brackets according to operator priority..

    it could just be string comparison
    Is it possible to call the right functions using their pointers without using a switch-case ....but only from their names..?.....so that there will be a call only once in that place and the input can go into the correct place..//..

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    If possible I'd write a function to order the input into brackets according to operator priority..
    It is possible, but don't do it. Your aim is ultimately to evaluate the expression, not format it. If you are able to format by placing parentheses in the correct places, then you are able to evaluate the expression with the correct operator precedence even without the formatting.

    Quote Originally Posted by manasij7479
    Is it possible to call the right functions using their pointers without using a switch-case ....but only from their names..?
    No, at least not in the way that you probably have in mind. I can think of a way to make this work, but I suspect that if you were to try to do so many things at once, you simply won't succeed at all. You can come back to this idea later.

    Quote Originally Posted by manasij7479
    so that there will be a call only once in that place and the input can go into the correct place..//..
    I suggest that you simply accept the input in reverse polish notation and get a working program up and running. With that working framework in place, you can then convert to use infix notation. In fact, this is the order in which I did it as a student.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Did it...Now I'm trying to implement the infix "Shunting..." algorithm.
    I can think of a way to make this work
    Can you tell me now?..I've posted the basic RPN program as a new post, which has some minor(had to use 'p' instead of '+' and 'm' instead of '-' ....no idea why) issues .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this code produce infinite loop?
    By matchkop in forum C Programming
    Replies: 15
    Last Post: 05-25-2010, 09:52 AM
  2. Using while produces an infinite loop...
    By UCF43 in forum C Programming
    Replies: 4
    Last Post: 04-01-2010, 04:47 PM
  3. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  4. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  5. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM

Tags for this Thread