Thread: debuging problem

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That discards at most 128 unread characters from the input stream up to and including the first newline. (If you are entering input from the console, there probably can't be unread characters beyond the first newline.)
    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).

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vampirekid.13
    you will notice i made your for loops that had a cin >> have a full code block and added cin.get() after each one, then i also added a cin.get(); right before the return 0;


    that was the problem, the code wasn't crashing, there just was nothing left to execute and keep the console open. therefor terminating the process.
    Your analysis is on the right track, but your solution is not particularly good. The indiscriminate use of std::cin.get() is unnecessary at many points. Where it is necessary (and it is not necessary in this program), it would be better to use std::cin.ignore() instead (possibly supplying the number of characters to ignore), or the std::ws manipulator.

    As a way to prevent the closing of the console window, it is a poor man's solution compared to what matsp, anon and JVene have suggested.

    EDIT:
    Quote Originally Posted by anon
    Before all these cin.get()s one should put a cin.ignore()
    That would be wrong, since it means that if a user enters the numbers all on a single line, only the first number will be read, unless enough numbers were added to be beyond 128 characters in length.
    Last edited by laserlight; 05-06-2009 at 11:07 AM.
    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

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    the other thing is right before return 0; do another cin.get(); to keep the console up. if it still closes add another cin.get(); before return 0;
    I meant these. Naturally don't empty the buffer if you don't want it to be empty.

    Code:
    //not the right way
    cin.get();
    cin.get();
    cin.get();
    return 0;
    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).

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    16
    Quote Originally Posted by laserlight View Post
    Your analysis is on the right track, but your solution is not particularly good. The indiscriminate use of std::cin.get() is unnecessary at many points. Where it is necessary (and it is not necessary in this program), it would be better to use std::cin.ignore() instead (possibly supplying the number of characters to ignore), or the std::ws manipulator.

    As a way to prevent the closing of the console window, it is a poor man's solution compared to what matsp, anon and JVene have suggested.

    EDIT:

    That would be wrong, since it means that if a user enters the numbers all on a single line, only the first number will be read, unless enough numbers were added to be beyond 128 characters in length.
    im no expert, shoot i only played w/ c++ for a few weeks, just doing my best to help.


    i still dont fully understand how cin.ignore works.

    what if someone inputs 10 chars with spaces between them instead of pressing enter?

    also, in VC++ the console closes after printing the output w/o having a system("pause"); or cin.get(); before return 0;

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    Thanks for your reply "vampirekid.13". I have added cin.get(); after every cin >> and it works perfectly now.Thanks again everybody for your help.

  6. #21
    The larch
    Join Date
    May 2006
    Posts
    3,573
    i still dont fully understand how cin.ignore works.

    what if someone inputs 10 chars with spaces between them instead of pressing enter?
    cin.ignore empties the stream so that those characters are not there anymore. Without that following input functions like cin >>, cin.get, getline etc keep reading those characters as each of them should without pausing for further input until there is nothing left to read.

    also, in VC++ the console closes after printing the output w/o having a system("pause"); or cin.get(); before return 0;
    I think a shortcut was already mentioned (Ctrl + F5?).

    Those artificial means to keep windows open are just a crutch. It is important to know about cin.ignore though.
    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).

  7. #22
    Registered User
    Join Date
    May 2009
    Posts
    16
    Quote Originally Posted by anon View Post
    cin.ignore empties the stream so that those characters are not there anymore. Without that following input functions like cin >>, cin.get, getline etc keep reading those characters as each of them should without pausing for further input until there is nothing left to read.



    I think a shortcut was already mentioned (Ctrl + F5?).

    Those artificial means to keep windows open are just a crutch. It is important to know about cin.ignore though.
    hmm, cool, ill have to use it more often, thats a cool function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM