Thread: strange problem with dev cpp

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    5

    strange problem with dev cpp

    hi whenever i compile my programs with devcpp and then run it i hav this problem where my program ends whenever i press enter after i type in stuff to fill my variable.

    for example:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int num;
    cout << "enter a number";
    cin >> num;
    cout << "the number you enterd is: " << num << endl;
    return 0;
    }
    this program ends right after i enter a value for num. it never gets to the cout statement at the end. this problem is not just specific to that program i just wrote. i have the same problem for every program in which i have to enter a value from the keyboard to fill a variable. could someone tell me how i can fix this problem?
    Last edited by henrychan22; 06-18-2006 at 02:20 AM.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    23
    It will return the value that you entered, but then it will close down immediately. Try running it from the command prompt (Start>Run) or adding the lines

    char response;
    cin >> response;

    immediately before <return 0;>. These are only a couple of ways of achieving what you want.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    You can use
    cin.get();
    just before resturn although as you have used a cin command you will have to put:
    Code:
    cin.ignore();
    cin.get();
    Here is the FAQ entry:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    23
    I know about cin.get(), but why is it necessary to have the cin.ignore() before it in this case? I've used it before and not had to have cin.ignore() with it. What's specific about this example?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It doesn't work properly after using cin >>, or is it that it doesn't work properly after using cin.get(); before it.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    get() leaves the trailing null character on the buffer. Ignore() removes it so that a new get() can operate normally.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    23
    It doesn't work properly after using cin >>, or is it that it doesn't work properly after using cin.get(); before it.
    When I just used cin.get(), it didn't pause the console but when I used cin.ignore() before cin.get(), it did pause.


    get() leaves the trailing null character on the buffer. Ignore() removes it so that a new get() can operate normally.
    Thank you. It's nice to know that something works, but I prefer to know why!

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Actually, istream's operator >> leaves a newline ('\n') in the buffer. This is because it is not part of an integer and would not make sense to be converted along with 'num', and so we use ignore() to throw away the newline, and get() to wait for you to press a character. Make sure to keep this fact in mind when you mix input gathering methods like operator >> and getline, because the getline will just eat the newline and return if it's left in the buffer, and confuse the hell out of you.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The best solution of all is to run your programs in a console window. There are a few reasons that win to do this:

    win - you see the output without pausing
    win - you can enter parameters easily yourself and actually remember what they are
    win - the advantage that you can keep the same window open all the time, make changes, and recompile

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  2. strange problem with operator overloading
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2008, 11:18 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. strange problem
    By kumarqt in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2006, 02:02 AM
  5. Strange problem with fts_open call
    By jhopper in forum C Programming
    Replies: 0
    Last Post: 02-26-2002, 12:01 AM