c++ help (getch and kbhit)

Discussion in 'Mac Help/Tips' started by scem0, Mar 28, 2003.

  1. scem0 macrumors 604

    scem0

    Joined:
    Jul 16, 2002
    Location:
    back in NYC!
    #1
    Can someone explain these two functions to me? I don't
    understand how to use them correctly.

    Here is a very simple program, and all I want it to do is return
    a the value for what they enter:

    Code:
    #include <iostream.h>
    #include <process.h>
    #include <conio.h>
    
    int menu();
    
    void main()
    {
    int ans;
    
    ans = menu();
    
    // menu function after main section
    
    if (ans == 1)
    cout << "1";
    
    else if (ans == 2)
    cout << "2";
    
    else if (ans == 3)
    cout << "3";
    
    else if (ans == 4)
    cout << "4";
    
    
    
    
    
    }
    
    int menu()
    {
    	cout << "+----------------------------+ \n";
    	cout <<	"|           DODGER           | \n";
    	cout <<	"+----------------------------+ \n";
    	cout << "|  WHERE WOULD YOU LIKE TO   | \n";
    	cout << "|         GO TODAY?          | \n";
    	cout << "+----------------------------+ \n";
    	cout << "|1:     Play the game        | \n";
    	cout << "+----------------------------+ \n";
    	cout << "|2:     Instructions         | \n";
    	cout << "+----------------------------+ \n";
    	cout << "|3:         Quit             | \n";
    	cout << "+----------------------------+ \n";
    	cout << "|  Press the number that     | \n";
    	cout << "| corresponds to your choice | \n";
    	cout << "+----------------------------+ \n";
    if(getch()==1)
    {
    	return 1;
    }
    else if (getch() == 2)
    {
    	return 2;
    }
    else if (getch()==3)
    {
    	return 3;
    }
    else return 4;	
    }
    
    
    I know I used getch wrong, because I don't know how to use it.
    Can someone help?
     
  2. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    Within menu()

    use a variable: char choice

    use the cin input stream instead of getch--it's not tied to x86 machines.

    cin >> choice;

    then

    return choice; (you'll need to change menu to return a char)

    in main()

    You can simply

    cout << menu();

    to get and display the menu choice

    (and yes, you do have to figure out a few things--I don't do other people's work for them. :))
     
  3. scem0 thread starter macrumors 604

    scem0

    Joined:
    Jul 16, 2002
    Location:
    back in NYC!
    #3
    I wanted it to where they didn't have to press their choice and
    then press enter. I wanted them to just press the number
    of their choice and they are returned back to the main function.
    Hence, I wouldn't have an extra variable (choice).

    Plus, that is too ez. :D
     
  4. scem0 thread starter macrumors 604

    scem0

    Joined:
    Jul 16, 2002
    Location:
    back in NYC!
    #4
    Okay, I'm back. I got getch to work, but I am
    still lost on kbhit. But I will explain how getch
    works so someone might not have to go through
    what I went through to find it.

    You have to initialize a variable, I'll call it scemo :D
    and then set it equal to getch(), and the
    character they input will be set to 'scem0'.
    So it would look kind of like this:
    Code:
    #include <blah>
    ...
    
    void main (void)
    {
        char scemo;
    
    cout << "Please enter a character" << endl;
    scemo = getch();
    
    cout << "The letter you typed in was " <<
         scemo << endl;
    }
    
    So, it is a lot easier than I thought.

    I still don't understand kbhit. Can anyone
    explain it? Or could someone direct me to
    a website that explains it?

    Help would be appreciated, because this is for
    a computer science project. ;)
     
  5. Steradian macrumors 6502

    Joined:
    Dec 3, 2002
    Location:
    San Jose
    #5
    This for a Dossier, or just another comp sci project thier Scemo?
     
  6. scem0 thread starter macrumors 604

    scem0

    Joined:
    Jul 16, 2002
    Location:
    back in NYC!
    #6
    This is for my high school Comp Sci I project.
     
  7. scem0 thread starter macrumors 604

    scem0

    Joined:
    Jul 16, 2002
    Location:
    back in NYC!
    #7
    Okay, I figured out getch and kbhit, but now I have one more '?':

    How can I fill in the values for a 2 dimensional array when I
    initialize it?

    Will this work?
    Code:
    char game[27][10] =
        {
        {+,-,-,-,-,-,-,-,-,-,-,-,+,-,+,-,-,-,-,-,-,-,-,-,-,-,+}
        {|,>,|,|,|,|,|,|,|,|,|,|,|,' ',|,|,|,|,|,|,|,|,|,|,|,<,|}
    	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
    	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
    	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
        {|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
    	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
    	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
    	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
    	{+,-,-,-,-,-,-,-,-,-,-,-,+,-,+,-,-,-,-,-,-,-,-,-,-,-,+}
        };
    
     
  8. GeeYouEye macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #8
    I think you have the right idea, but since they're of type char, I think you need single quotes around each one. Could be wrong though.
     
  9. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #9
    You're right...he needs apostrophes.
     
  10. scem0 thread starter macrumors 604

    scem0

    Joined:
    Jul 16, 2002
    Location:
    back in NYC!
    #10
    y'all were correct...... It took forever, but thank god for 'find and
    replace' ;). I have everything working now, except it is sooooo
    slow on old computers. It works great on my p4 2.4 GHz, but
    it sucks on our schools pentium 2's. :( :rolleyes: :eek:.
     
  11. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #11
    I'm sure you'll learn to programme more efficiently if you keep working at it.
     

Share This Page