Thread: Input in C++

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    15

    Input in C++

    I'm fairly new to c++, but I know a lot of the basics. I have decided to create a console game of some kind. At first, I wanted to create a roguelike, but after weeks of reading source files, tutorials, and books, I still have absolutely no clue how to do that. So I decided to make a game without even simple ascii graphics, a text-based game. After working on it for a while, I realized that the method of input I had been using, cin, wasn't good enough. I need a way to have a person hit a key, and the program to immediately respond, instead of waiting for the person to type whatever they want and then hit enter. How would I go about doing this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    one popular method:

    Code:
    char c;
    
       cout << "Makes a selection:\n\n"
               << "[P]ick up\n"
               << "[I]ventory\n"
               << "> ";
       cin >> c;
    
      // then use a switch statement to control what happens next
      switch ( c )
      {
      case 'P':
          if ( item <= 0 )
          {
             cout << "You pick it up";
          }
    
           else if ( item >= 1 )
          {
             cout << "You already have it";
          }
          break;
    
       case 'I':
          inven();  // call a function to deal with this
          return here(); // return to this section of code when inven is done
          break;
       }
    this is one idea, there are many others. You could divise a class that deals with user input
    but depending how far ahead in C++ you are that may be over your head. Remember, if the user hits an icoreect key, you must deal with this issue using the deafult switch command, but that is again only one way. Good luck in your creation

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    One other point, if you want to avoid the "hit enter" then you could assign c to getchar()

    Code:
    c=getch();
    This would eliminate the need to press enter. getch() is defined in the conio.h library which is a little non-standard, but it is an efficinet way to achieve the result

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > which is a little non-standard

    Can you say completely?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    ok ok, I got it wrong -< il go sit the corner with a dunce hat on ( chuckes...!!! lol )

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >a little non-standard
    As in half dead or a little pregnant?
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Awesome, thanks everyone! I read that page from the FAQ, and ended up using this to accomplish my goal:

    Code:
    #include <conio.h>
    while ((ch = getch())){
              if (ch == 'n')
              goto newgame;
    }

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    don't use goto either! its horrid! use function call instead

    Code:
    if ( ch == 'n' )
              {
                 newgame();
              }
    goto is frowned upon in C++ as a throw back to spagetti code, a mess of code jumps!
    using function calls make the system safer and your code looks much better.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >don't use goto either! its horrid!
    Hmm, can you explain in detail why goto shouldn't be used in this particular situation?

    >use function call instead
    Actually, a loop seems to be more appropriate, judging by the very incomplete snippet given.

    >goto is frowned upon in C++ as a throw back to spagetti code, a mess of code jumps!
    That's a cop out. It's easy to say "never use goto" and throw around the phrase "spaghetti code", but that's not even close to the whole story. I get the feeling you're telling others what you were told, and "don't use goto" comes from a teacher rather than personal experience.
    My best code is written with the delete key.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I did not mean to offend prelude, i was just told that goto should be avoided in all cases. There is usually always another way to solve a problem without having to use goto. I was only making a suggestion, it is not the absolute

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I did not mean to offend prelude
    Rest assured, if you offend me, there won't be any doubt.

    >i was just told that goto should be avoided in all cases
    Yea, that's the typical overreaction to a lot of holy war issues. It's not your fault at all. Though I will recommend a guideline that I try to follow religiously: Never use absolutes unless you're absolutely sure. If someone says "always" or "never", they're probably wrong.
    My best code is written with the delete key.

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I understand. Thank you for correcting me on the issue. Does the kwyword goto go back further than the BASIC language? I am just interested to know

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks dave, that was a great link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 11:47 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM