Need help with very simple C++

Discussion in 'Software' started by NeoNemesis, Jul 24, 2004.

  1. NeoNemesis

    NeoNemesis Moutharrhea

    Hello, I'm have been working on a project here at my computer for little bit and I have decided to put in a better menu system. Right now I have it so the user inputs a letter or number and then has to press enter. I want to make it so like the user only has to press the key and it goes right away. I also want to make it so that it will exit the program (not just to dos, but out of dos too) when the user presses 'x'. I am using regular c++, DJGPP so no visual c++ or anything too complicated. This should be very easy but I am very new to C++ (decided to learn it over the summer) and so as you can see these types of problems are alot of the problems I have come up with. :rolleyes:
     
  2. NeoNemesis

    NeoNemesis Moutharrhea

    can anyone help me? i know for a fact that there are prob several people that can because this is so simple.
     
  3. snakefoot

    snakefoot Sergeant Major

  4. NeoNemesis

    NeoNemesis Moutharrhea

    lol didn't see that. I must not have been on that day since usually i only check the new posts. I thought that thread was dead.
     
  5. NeoNemesis

    NeoNemesis Moutharrhea

    Oh, well I tried using the cin.get() command it didn't work. I still had to press enter for it to work. Thanx for the idea though. :)
     
  6. snakefoot

    snakefoot Sergeant Major

    The press any key (and not only enter) seems out of reach for c++, guess you have to use the C way, and accept that your program will only compile on PC platforms (So it will probably fail if you try to run it on a mainframe):

    Code:
    #include <conio.h>   //getch()
    #include <iostream>
    using namespace std;
    
    int main()
    {
       cout << "Press any key to continue.\n" << endl;
       char hey=getch(); // non-standard method (PC only)
       switch(hey)
       {
          case '1':
             cout << "Hey 1 pressed" << endl;
             break;
          case '2':
             cout << "Hey 2 pressed" << endl;
             break;
          default:
             cout << "Hey don't press " << hey << endl;
             break;
       }
       return 0;
    }
    
     
  7. GregoryDalton

    GregoryDalton Private E-2

    not sure if this works on C++ - but in C there is 'getch' - I think that only got one character before carrying on... worth looking into. Cant remember which library it is in though. :)

    Cheers,
    Greg
     
  8. brettcpp

    brettcpp Private E-2

    You can always use select() to monitor stdin, I'm not sure if this will work for the Windows version of select() that is defined in winsock.h. It most likely will not. I've always used getch().

    Brett
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds