Thread: A strange error

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    A strange error

    Here is the main.cpp from my program .Everything works fine when the while loop is not there...i.e..it is done only once..
    Code:
    #include <iostream>
    #include <string>
    #include "token.h"
    #include "rpn.h"
    #include "shunt_y.h"
    using namespace std;
    
    int main()
    {
    
        deque<token> dt;
        char q;
        string input;
        
        while(1)
        {
        cout<<"Enter the expression:\n>";
        getline(cin,input);
        dt = tokenize(input);
        dt = infix_to_rpn(dt);
        cout<<rpn_eval(dt)<<endl<<"Quit?\n>";
        cin>>q;
        if (q == 'y' )break;
        else continue;
        }
        return 0;
    
    }
    At runtime....whenever I opt not to quit....the input string automatically assumes a garbage value...not letting me into type into the prompt...
    Looks somewhat like the following quote:
    Enter the expression:
    >log ( 10 ^ 5 ) - sin ( 3.142 / 2 )
    4
    Quit?
    >n
    Enter the expression:
    >0
    Quit?
    >n
    Enter the expression:
    >0
    Quit?
    >
    and so on...

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    __fixed it__on a suggestion that I should have put a
    Code:
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    after the
    Code:
     cin>>q;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Correct, the cin >> q leaves the newline in the buffer. cin.ignore clears the input buffer.
    Also, else continue; is unnecessary. The loop while repeat once it reaches the end anyway.
    Also consider indenting better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Your console input buffer is not fully empty that's why you are not allowed to use that...
    Use instead. Try reading ignore function provided by cin...

    And by the way that's not a strange error... :-)

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your console input buffer is not fully empty that's why you are not allowed to use that...
    Not quite true. You are allowed to use it alright.

    The thing is that there is an unread newline character. getline reads anything up to the first newline character. If there are no other characters preceeding the newline, it means that getline successfully extracted an empty line (empty string).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    you also could have initialised your input string to " " at the start of the loop
    i think.....

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Not quite true. You are allowed to use it alright.
    My bad..... I didn't mean to write that...
    I know we still can use that but what i mean was to write console ignore to clear your buffer out... Anyways, nice compilation of my statement :-)

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by bobknows View Post
    you also could have initialised your input string to " " at the start of the loop
    i think.....
    What good would that do?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM