Thread: Somebody(echoing)!! Anybody(echoing)!! Help me...

  1. #1
    0x01
    Join Date
    Sep 2001
    Posts
    88

    Somebody(echoing)!! Anybody(echoing)!! Help me...

    I have been a C programmer (NOT C++) for a long time now; and I don't understand why the following code doesn't work.

    Code:
    #include "iostream.h"
    #include "conio.h" // getch()
    
    int main()
    {
    
         cout << "Why wasn't this printed before the getch()??";
         getch();
    
         return 0;
    }
    For some reason, the getch() function runs before anything; and it doesn't stop there. There are other functions that are similiar, like delay()'s etc. This problem only occurs when the code is written in C++, NOT C.

    For instance; This works fine.

    Code:
    #include "iostream.h"
    #include "conio.h" // getch()
    
    int main()
    {
    
         printf("Press any key");
         getch();
    
         return 0;
    }
    I think... I used to know what the problem was; Its probably something small thats overlooked.

    [ Compiler: MSVC++ OS: XP ]

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    i dont know if this will solve your problem but it should be used in c++....

    use:
    #include <iostream>
    not:
    #include "iostream.h"

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    and if you are using compiler headers, use < >. use " " for self made classes.

    so if you were to use iostream.h and conio.h, it would need to be
    #include <iostream.h> not "iostream.h".

    and as perspective said, use iostream without the .h.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> cout << "Why wasn't this printed before the getch()??";

    cout << "Why wasn't this printed before the getch()??" << endl;

    Your I/O buffer is still being prepared, you have not done anything to tell the I/O system you are finished with building this line, it is still in the buffer. It is actually more complicated than that but simplistically that is what you are seeing.

    Generally, it is a bad idea to mix C and C++ I/O in the same program. Behaviour can be erratic.

    *** EDIT ***

    As long as your compiler can find the prototypes, it does not matter that much if you use old type headers or new, at least at this stage. If you want to use newer type stuff, then you should probably use new type headers, i.e. <iostream>, and almost certainly, a "using namespace std;" line.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    0x01
    Join Date
    Sep 2001
    Posts
    88
    Hm... I knew the endl meant "end line" and that it ends places the cursor below the previous line,
    but it didn't know exactly why it was needed or why such a thing was used until now...

    I will try it out later, I am sure that is the cure for this horrible unforgiveing disease.

    Btw- I read the "useing namespace std" faq... I don't understand.. ha.. I know that sound blunt, but try this -> C programmer going to C++.... I never seen such a thing "useing namespace std" and is it commonly used whatever it is.

    Sorry if this reply didn't make any sense, I am in a hurry.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    cout << "blah blah" << endl;

    Cursor at start of next line...

    cout << "blah blah" << flush;

    ...cursor at end of line.

    Namespaces are a way of partitioning the old global address space. In C global variables were all collected together into the global namespace. You have 2 global variables with the same name, you have trouble. Yes, you don't write code like that, but suppose you buy 2 libraries that both rely on a global called "Version" for example?

    In C++ the "global" namespace can be partitioned into many named namespaces, (actually there can be an unnamed namespace as well), and you can define a global symbol to exist in a named namespace. Thus, library XYZ can have a global called Version in namespace xyz, and library ABC can have a global called Version in namespace abc, and the two are not the same.

    using namespace std;

    Means all the names that are defined in namespace std are available to your program. All the standard library routines are in namespace std.

    If you get the situation that I described above, you would not include the whole of both namespaces, you can either say...

    using xyz::Version;

    ... then any reference to Version uses the xyz value. If you need to use both in the same routine, you can prefix the variable name at usage point...

    x = xyz::Version;
    y = abc::Version;
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    That getch() behavior (same thing with getchar()) seems to be a Microsoft Visual C++ "feature".

    I compiled your code in Dev-C++ and with Borland C++ Builder 5.5 and both of them printed "Why wasn't this printed before the getch()??" before pausing for the getch().

    I also compiled it with CC on my school's Unix system. getch() didn't work at all there so I had to change it to getchar() and #include <stdio.h>, and then it also printed the line before pausing.

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    That getch() behavior (same thing with getchar()) seems to be a Microsoft Visual C++ "feature".
    Nope sorry. Its from the standard. That is the behaviour of the standard input/output classes. They are buffered and will only print when an endl is sent or a flush command or when the buffer is full. There is another condition and that is on input. This is becaused cout is tied to cin. So when cin is used cout is flushed immediately. getch() is a non-standard function so whether that call to getch() flushes the output or not will be completely implementation defined.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    OK, Stoned_Coder, now you have me thoroughly confused. In MSVC++ knave's original code behaves that way (i.e. pausing for getch() and THEN printing) only when you use the C-style directive
    #include <iostream.h>

    If you change it to
    #include <iostream>
    using namespace std;

    and make no other changes (NO << endl ) it prints immediately
    "Why wasn't this printed before the getch()??"
    and THEN pauses for the getch().

    With the other compilers, it prints immediately (BEFORE pausing for getch() no matter which directive is used.

    Can you explain that please?

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    cant explain totally as getch() is nonstandard and doesnt have its implementation defined by the c++ standard so there is no way bar compiler documentation in knowing how that function behaves. On the other hand the behaviour of the input/output classes is rigidly set by the c++ standard. So the flushing of the buffer in the second case but not the first cannot be explained there. Not too sure why that happened,all i can say is what the standard said should have happened. One thing i can say is that the old iostream classes in <iostream.h> are TOTALLY DIFFERENT from the new iostream classes in <iostream>. For the most part implementation of the new headers looks something like...
    Code:
    namespace std
    {
       #include<stdlib.h>
    }
    Just including the contents of stdlib.h inside namespace std is normally how <cstdlib> is implemented. Look at the old <iostream.h> heirarchy and the new <iostream> heirarchy. They are totally different. One possibility is that getch is tied to the new ostream classes but not the old. that would explain the inconsistent flushing but the compiler documentation for getch() doesnt say whether this is true or not.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Student Forever! bookworm's Avatar
    Join Date
    Apr 2003
    Posts
    132
    Every advice mentioned above should be followed,but I dont know y everyone here loves getch() so much! Y doesnt anyone try std::cin.get.I had been given the advice to use it and it solved this very problem.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by bookworm
    Every advice mentioned above should be followed,but I dont know y everyone here loves getch() so much! Y doesnt anyone try std::cin.get.I had been given the advice to use it and it solved this very problem.
    I use std::cin.get()

Popular pages Recent additions subscribe to a feed