About getch() (already read the f.a.q.)

This is a discussion on About getch() (already read the f.a.q.) within the C++ Programming forums, part of the General Programming Boards category; Just to clarify, getch is windows platform only, regardless of version? (ie. winme, xp, win98). No matter what I've read, ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252

    About getch() (already read the f.a.q.)

    Just to clarify, getch is windows platform only, regardless of version? (ie. winme,
    xp, win98).

    No matter what I've read, there's no piece of code out there that I've found that actually mimics getch; getch, does what I need it to do in spite of it being c and by many accounts, bad to use.

    *Mods, feel free to del this topic (hopefully, after I've received a sat. response).

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, getch() is specific to Microsoft's compilers (and perhaps Borland's), not Windows. GCC for Windows doesn't have it. I don't think there's an MS compiler for any platform other than Windows (they don't use VC++ for their Mac software, I think), but if there were, it would probably have getch() too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Thanks for replying. The only alternative I've managed to cobble together is below. I liked getch since it erased user input error (multiple characters).

    Code:
    char getMenuChoice()
    {
    string menugrab;
    std::cout<< "(D)iamond \n";
    std::cout<< "(C)ube \n";
    std::cout<< "(S)phere  \n";
    std::cout<< "(P)lane \n";
    std::cout<< "(H)edra \n";
    std::cout<< "(O)blong \n";
    std::cout<< " \n";
    std::cout<< "(Q)uit  \n";
    std::cout<< "  \n";
    std::cout<< "Enter Choice: ";
    std::cin >> menugrab;
     void uppercase(string &s);
        {
        for (int j=0; j<menugrab.length(); ++j)
         menugrab[j]=toupper(menugrab[j]);
        }
    if (menugrab.length() > 1)
        {
        menugrab.erase(1); //not certain of pos:: 0 or 1? string count starts at 0?
        }
    char* c = new char[menugrab.length() + 1];
      for( int i = 0; i <menugrab.length(); i++ )   //change string menugrab to *char
      c[i] = menugrab[i];
      c[menugrab.length()] = '\0';
     return* c;  //char c needed and used for switch case
     }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    return toupper( menugrab[0] );

    I mean,
    > void uppercase(string &s);
    What's this doing - commenting on the next couple of lines?

    > char* c = new char[menugrab.length() + 1];
    Since you return *c rather than c, this is a memory leak.

    > menugrab.erase(1);
    Does this make menugrab.length() == 1 ?

    You convert a long string to upper case
    You then trim it to a single char
    You then allocate space for exactly 2 characters
    You finally return the first element of the array (and leak the memory)

    > I need it to do in spite of it being c and by many accounts, bad to use.
    But the cin you have also has the backspace/erase features doesn't it (up until the user presses return right) ?

    Or are you thinking about the "getch() returns without the user having to press return" type of interface?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Since you return *c rather than c, this is a memory leak.
    Tried to do that, but if I attempted to compile c without assigning a cast it wouldn't compile and if I did, the menu would crash. All of these questions raised is in part why I want to use getch; it works, eliminates user input error and isn't driving me loco the way the code uptop is doing.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    I don't see what your problem is then, because in a standalone test, it works fine.
    Code:
    #include <iostream>
    #include <string>
    using std::string;
    
    char getMenuChoice()
    {
        string menugrab;
        std::cout<< "(D)iamond \n";
        std::cout<< "(C)ube \n";
        std::cout<< "(S)phere  \n";
        std::cout<< "(P)lane \n";
        std::cout<< "(H)edra \n";
        std::cout<< "(O)blong \n";
        std::cout<< " \n";
        std::cout<< "(Q)uit  \n";
        std::cout<< "  \n";
        std::cout<< "Enter Choice: ";
        std::cin >> menugrab;
        return toupper(menugrab[0]);
    }
    int main ( ) {
        std::cout << getMenuChoice() << std::endl;
        return 0;
    }
    
    $ ./a.exe
    (D)iamond
    (C)ube
    (S)phere
    (P)lane
    (H)edra
    (O)blong
    
    (Q)uit
    
    Enter Choice: diamond
    D
    
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Well, apparently I don't have a problem then. I was under the impression that you couldn't use a std::string as a comparative for switch case (had to be # or char).

    Thanks. I'm still rather confused, but I'll get over it....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 10:52 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 02:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21