Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

getchar in namespace std

bilderbikkelbilderbikkel Posts: 754Member
Hi everybody,

I want to get getchar() in namespace std, so I can use 'std::getchar()'.
I know that the C++ standard states getchar() to be in the header file cstudio. cstdio indeed contains the code below:

[code]
#ifdef __cplusplus
namespace std {
#endif /* __cplusplus */
[/code]

Nevertheless, the code
[code]
#include
int main()
{
std::getchar();
}
[/code]

does not compile, where it does without 'std::'. Also, desperately tring
[code]
#include
[/code]
did not get getchar() in namespace std.

Anybody got an idea to so anyhow? I use the Borland STL:

[code]
/* cstdio.h

Standard C header file wrapper for stdio.h
*/

/*
* C/C++ Run Time Library - Version 11.0
*
* Copyright (c) 1997, 2004 by Borland Software Corporation
* All Rights Reserved.
*
*/
[/code]

Thanks for suggestions,
bilderbikkel

Comments

  • LundinLundin Posts: 3,711Member
    : Hi everybody,
    :
    : I want to get getchar() in namespace std, so I can use 'std::getchar()'.
    : I know that the C++ standard states getchar() to be in the header file cstudio. cstdio indeed contains the code below:
    :
    : [code]
    : #ifdef __cplusplus
    : namespace std {
    : #endif /* __cplusplus */
    : [/code]
    :
    : Nevertheless, the code
    : [code]
    : #include
    : int main()
    : {
    : std::getchar();
    : }
    : [/code]
    :
    : does not compile, where it does without 'std::'. Also, desperately tring
    : [code]
    : #include
    : [/code]
    : did not get getchar() in namespace std.
    :
    : Anybody got an idea to so anyhow? I use the Borland STL:
    :
    : [code]
    : /* cstdio.h
    :
    : Standard C header file wrapper for stdio.h
    : */
    :
    : /*
    : * C/C++ Run Time Library - Version 11.0
    : *
    : * Copyright (c) 1997, 2004 by Borland Software Corporation
    : * All Rights Reserved.
    : *
    : */
    : [/code]
    :
    : Thanks for suggestions,
    : bilderbikkel
    :



    I get this to compile on gcc but not on Borland or MS:

    [code]
    #include

    int main()
    {
    std::getchar();
    return 0;
    }
    [/code]

    While this compiles on both gcc and MS, but not Borland:

    [code]
    #include

    using std::getchar;

    int main()
    {
    getchar();
    return 0;
    }
    [/code]


    using namespace std; solves the problem. Either you need to set some annoying compiler switch, or neither MS or Borlands did bother to follow ANSI/ISO C++. I mean, why bother... it is much more fun to make your own standard :-)

    Perhaps the problem might be caused by Borland implementing getchar() as a macro rather than a function? They just call fgets() when you type getchar().
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-9-11 7:20:0[/red][/b][hr]
    : Perhaps the problem might be caused by Borland implementing getchar() as a macro rather than a function? They just call fgets() when you type getchar().
    :


    VC++ 6.0 getchar() calls _read(), not fgets(). VC++ 2005 Pro calls fgetchar() --> getc(stdin) --> _read() --> ReadFile() (win32 api function)

    bilderbikkel: Since it is obviously a c++ program, why not just use standard c++ iostreams library instead of C standard io library? use cin.get() instead of getchar().


  • LundinLundin Posts: 3,711Member
    : : Perhaps the problem might be caused by [red][b]Borland[/red][/b] implementing getchar() as a macro rather than a function? They just call fgets() when you type getchar().
    : :
    :
    :
    : VC++ 6.0 getchar() calls _read(), not fgets(). VC++ 2005 Pro calls fgetchar() --> getc(stdin) --> _read() --> ReadFile() (win32 api function)

  • bilderbikkelbilderbikkel Posts: 754Member
    [b][red]This message was edited by bilderbikkel at 2006-9-11 9:47:36[/red][/b][hr]
    Edit: a typo :-)

    Thanks Lundin and Stober for both your useful suggestions!
    I wanted to use getchar() as an alternative to std::cin.get() as std::cin.get() does not always halt the program for an input (as std::cin might still contain information from previous inputs). I hoped that getchar() might have solved this, but as you pointed out as well, it doesn't.

    I refrain from using the nonstandard header conio.

    Instead I now use this:

    [code]
    std::cout << "Please enter character to exit program" << std::endl;
    std::cin.clear(); //First clear std::cin
    std::cin.get();
    std::cin.get(); //Yes indeed! This method is needed TWICE!
    [/code]

    I encountered this problem on a beginner's C++ course I'm teaching and I wanted to offer my students something better then the solution above. I guess there is no better solution when you use std::cin.


    Again, thanks for your useful comments!

    See ya later,
  • stoberstober Posts: 9,765Member ✭✭✭
    : I hoped that getchar() might have solved this

    C and getchar() is a lot older than c++ and cin.get(), so what you thought is not possible.


  • Aberran7Aberran7 Posts: 5Member
    : : I hoped that getchar() might have solved this
    :
    : C and getchar() is a lot older than c++ and cin.get(), so what you thought is not possible.
    :
    :
    :
    Since I am fairly new to this, could someone expain to me what is wrong with using:

    [code]

    system("PAUSE");

    [/code]

    to prompt the user for a keystroke input before ending the program?
  • LundinLundin Posts: 3,711Member
    : [b][red]This message was edited by bilderbikkel at 2006-9-11 9:47:36[/red][/b][hr]
    : Edit: a typo :-)
    :
    : Thanks Lundin and Stober for both your useful suggestions!
    : I wanted to use getchar() as an alternative to std::cin.get() as std::cin.get() does not always halt the program for an input (as std::cin might still contain information from previous inputs). I hoped that getchar() might have solved this, but as you pointed out as well, it doesn't.
    :
    : I refrain from using the nonstandard header conio.
    :
    : Instead I now use this:
    :
    : [code]
    : std::cout << "Please enter character to exit program" << std::endl;
    : std::cin.clear(); //First clear std::cin
    : std::cin.get();
    : std::cin.get(); //Yes indeed! This method is needed TWICE!
    : [/code]
    :
    : I encountered this problem on a beginner's C++ course I'm teaching and I wanted to offer my students something better then the solution above. I guess there is no better solution when you use std::cin.
    :
    :
    : Again, thanks for your useful comments!
    :
    : See ya later,
    :


    Seems like the problem is with the input rather than with cin.get().
    You could use getline() or (cin>>myInteger).get() to eat all pending LF characters.
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-9-12 18:33:35[/red][/b][hr]
    : :
    : Since I am fairly new to this, could someone expain to me what is wrong with using:
    :
    : [code]
    :
    : system("PAUSE");
    :
    : [/code]
    :
    : to prompt the user for a keystroke input before ending the program?
    :


    my opinion: nothing when you use it in just your own code. Some people claim that it can let viruses into the system and makes your program slightly slower. Well, my answer is that is somebody changed out cmd.com or command.com with their own version then your computer system has much bigger problems than the occasional use of system() function.


Sign In or Register to comment.