Menu

newbie question

transitive
2008-06-05
2012-09-26
  • transitive

    transitive - 2008-06-05

    I have just recently begun teaching myself to program in C++ and in general I'm very inexperienced with programming. I haven't had any problems or confounding errors yet but I just encountered one tonight. I was hoping someone here might be able to explain whats going on here to me.

    I am using Dev-C++ version 4.9.9.2

    The program I am having trouble with is a very short and simple one, just an elementary example of an if/else clause. This is the full source code

    include <iostream>

    using namespace std;

    int main()
    {
    int first, second;
    cout<<"please enter a big number: ";
    cin>>first;
    cout<<"please enter a smaller number: ";
    cin>>second;
    if (first > second)
    cout<<"\ngood job.";
    else
    cout<<"\nnope, you fail.";

    cin.get();
    return 0;
    }

    The problem I am having is the very commonly reported problem where the program compiles and runs succesfully but vanishes in a fraction of a second before I can see the output. I am aware that windows automatically closes programs that have been completed and my fix so far has been to include the cin.get(); line of code to make the program wait for a keystroke before completing. It has worked until now. For some reason though, it won't work with this program.

    Here is a sample of my full compiler log.

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Documents and Settings\Daniel\Desktop\my own programs\simple if-else clause.cpp" -o "C:\Documents and Settings\Daniel\Desktop\my own programs\simple if-else clause.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
    Execution terminated
    Compilation successful

    This looks normal to me and in fact the program does execute. My question then is this: why is my cin.get() code being ignored in this program? What should I do to prevent this?

    Thanks in advance.

     
    • transitive

      transitive - 2008-06-05

      K.Z.,

      I have tried various other suggested fixes and they have been even more problematic for me than the somewhat simpler method I have been using. I am a true novice so perhaps I'll learn more about it in the future, but for the moment it has been an unnecessary (and ineffective) complication for me to try and get a system("pause") command to work. i tried following the instructions on the FAQ for that and it produced no results.

      I have, at least in the short term, found a satisfactory fix to the problem, but what I was really hoping was that a more experienced programmer here could help me understand why the cin.get() command in my code is being disregarded by this program.

       
      • cpns

        cpns - 2008-06-05

        cin.ignore() might work in only very limited circumstances (when you user does not accidentally mistype 25#<enter> for example (On a UK keyboard # is next to the <enter> key. The ignore() will consume the #, leaving the <enter> for the get().

        Better:

        include <limits>

        ...

        // Flush line
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );

        // Wait
        cin.get();

        To be honest you should probably flush the line after all input that will not consume the whole line including the '\n'; anything that is not a getline() basically.

        My guess is that if you did not get system("pause") to work, you did not include <stdlib.h>. There is not magic, everything happens for a reason. Flailing around from one solution to another without understanding what is going on is not going to make you a great programmer.

        Clifford

         
      • Wayne Keen

        Wayne Keen - 2008-06-05

        The cin is not ignored by your program. There is a carriage return that is in the buffer after your earlier cin was used to read in your last number. This is fed to your last cin, which goes away happily, as does your display window.

        For grins, put

        cin.ignore()

        in front of your last cin, and see what happens. Don't accept this as a magic word, understand why it works.

        There is not magic by the way to getting system("pause") to work - if you think there is, you are doing something wrong. The best wway to deal with that is to post code that actually uses it, and has problems, along with your associated "Basic 3".

        Wayne

         
    • transitive

      transitive - 2008-06-05

      Thanks Wayne and cpns, your responses have been helpful.

      the cin.ignore() did work in this program. if i'm understanding correctly, the problem was that the enter keystroke from my second cin command was being passed along to the cin.get() command at the end of the program. is this right? that was an unexpected result, i was not aware that a keystroke could be applied to more than one cin. thank you for making me aware of this.

      cpns, could you clarify the additional statement in your cin.ignore? i see that your formulation is supposed to be a more generalized case than the basic cin.ignore(). tell me if i've understood correctly or not. your formulation dumps the entire line carried over from the previous cin, whereas the simpler ignore command only dumps the last keystroke?

       
      • cpns

        cpns - 2008-06-05

        >>i was not aware that a keystroke could be applied to more than one cin.

        There is only one cin. It is an object of the std::istream class. You probably need to know that to understand the documentation.

        Console input is line oriented and buffered. The consequences of that is no console input function call will return until <enter> is pressed (or rather, found in the buffer). If you use formatted input, the call will only process the data that matches your argument types, and then stop, leaving any unused data in the buffer. Now formatted input ignores whitespace (including <enter>), so when you read the second value, the buffered <enter> from the first input is discarded before reading the new numeric data.

        You can demonstrate this with your code by using your program this way:

        please enter a big number: 12345 678<enter>

        You will find that it does not bother waiting for the second number, it just consumes the "678" from the first line. That is why I suggest that you always flush the whole line after every input - so your users don't get any surprises if they are clumsy typists for example.

        Clifford

         
      • cpns

        cpns - 2008-06-05

        Exactly. Try it with the default ignore() and instead if entering "1234<enter>", type "1234 <enter>" for example. A user might reasonably expect your program to work despite the extraneous space.

        When called with no arguments, the default arguments for istream::ignore() are used, so it is the same as: cin.ignore( 1, EOF ) (ignore one character or until end of file, whichever is first). Whereas cin.ignore( numeric_limits<streamsize>::max(), '\n' ) ignores the maximum number of characters than can be buffered, or newline, whichever comes first. To ignore absolutely everything: cin.ignore( numeric_limits<streamsize>::max(), EOF );

        Always read the documentation: http://www.cplusplus.com/reference/iostream/istream/ignore.html

         
    • K.Z.

      K.Z. - 2008-06-06

      >>>but what I was really hoping was that a more experienced programmer here could help me understand why the cin.get() command in my code is being disregarded by this program. <<<

      Yes, as soon as I hit the send button I knew I wasn't in the position to hand out any kind of advice.
      And have been expecting someone to call me an Idiot.

      In fact I regret ever replying to anyone about anything.

      Sorry Transitive.

       
      • cpns

        cpns - 2008-06-06

        No, I think you had a point. If the suggested fixes did not work, then they were probably addressing a different problem that just happened to have similar symptoms.

        You have to discriminate between root-cause and symptomatic observation.

         
    • transitive

      transitive - 2008-06-06

      thank you again Clifford, you have been more than helpful. i've learned alot.

       
      • Wayne Keen

        Wayne Keen - 2008-06-06

        Thanks for engaging your brain. Keep doing that an you will be dangerous in no time.

        Wayne

        p.s. When I say to think about why something works, not to take it as magic words, there is usually a way to make it better.

         
    • transitive

      transitive - 2008-06-05

      for lack of an edit button...

      forgot to mention in original post. my operating system is windows XP SP2.

       
    • K.Z.

      K.Z. - 2008-06-05

      >>>The problem I am having is the very commonly reported problem where the program compiles and runs succesfully but vanishes in a fraction of a second before I can see the output.<<<

      You know it's very commonly reported,why hav'nt you bothered to follow advice in those other posts?

       
    • K.Z.

      K.Z. - 2008-06-05

      you could just use cin >> btw

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.