getting a character without enter

This is a discussion on getting a character without enter within the C++ Programming forums, part of the General Programming Boards category; how do i read input without the user having to press enter so if i had a text game and ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    getting a character without enter

    how do i read input without the user having to press enter

    so if i had a text game and i wanted them to input some thing how would i get it with out them having to press, say 'w' then enter. so instead the program would read the w and respond without the user needing to press enter?

    hope you understand

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    oops

    could i just use getch() or do i need != EOF this may sound stupid but what does EOF mean

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    EOF means End Of File.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    so i dont need it?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Depends on what you want to get from the user and how you want to get it.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    i just want a single character from the keyboard

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Most likely not then. However it all depends. Without the context of a program its hard to say.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok im going to post some of my code the part im asking about so then you can also help me find some erros. theres a few...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h>
    
    using namespace std;
    
    void map();
    //int movement();
    //int x(1);
    //int y(1);
    
    int main()
    {
      map();
      system("PAUSE");	
      return 0;
    }
    
    void map()
    {
        char armap[26][]={
            "+oooooooooooooooooooooooo+",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                   0    |",
            "+oooooooooooooooooooooooo+"
            };
        
        armap[x][y] = "8";
        
        for(int i =0; i <= 26; i++)
        {
            for(int j = 0; j <= 12; j++)
            {
                 cout<<armap[i][j];   
                 
            }
            cout<<endl;
        }
      //  movement();
    }
    
    
    /*int movement()
    {
        
        char direction;
        cout<<"use 'w' for up 'a' for left 's' for down and 'd' for right."<<endl;
        direction=getch();
        
        if (direction==('w' || 'W'))
        {
             y--;
             map();
        }    
        else if (direction==('s' || 'S'))
        {            
            y++;
            map();
        }    
        else if (direction==('a' || 'A'))
        {            
            x--;
            map();
        }    
        else if (direction==('d' || 'D'))
        {            
            x++;
            map();
        }    
        else if (direction==('q' || 'Q'))
        {            
            return(0);
        }
        else
        {
            movement();
        }
    }
    there are alot of errors regarding the array (there is another post just on this problem) and all the movement function is commented out because im still weeding out other problems

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    if (direction==('w' || 'W'))
    That won't work. The correct syntax is:
    Code:
    if ( (direction=='w') || (direction=='W') )
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    char armap[26][]={
    if you know both dimensions, you should define both.
    Code:
    armap[x][y] = "8";
    where are x and y defined?
    Code:
    for(int i =0; i <= 26; i++)
    this loop goes from 0..26, the array is only from 0..25
    Code:
    system("PAUSE");
    I wouldn't do this... just put cin.get() before the return.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    i havent looked it over myself really to find problems but thanks. x and y are defined at the very top they're just commented out like the rest of direction();

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok i changed the array to look like this
    Code:
       armap[][12]={    
            "+oooooooooooooooooooooooo+",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                   0    |",
            "+oooooooooooooooooooooooo+"};
    but i still get the errors


    initializer-string for array or chars is too long


    i tried armap[26][12] and armap[12][26] and neither worked came up with same error

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok i solved my array problem... heres how

    the array is
    Code:
       armap[12][26]={    
            "+oooooooooooooooooooooooo+",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                        |",
            "|                   0    |",
            "+oooooooooooooooooooooooo+"};
    so its 12x26 right? well i made the array 12x25 without changing the values in the definition and it worked. why?

  15. #15
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try armap[][27]. You're forgetting about the null-terminator.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. Einstine calculator
    By Mach_ie in forum C Programming
    Replies: 2
    Last Post: 08-30-2003, 09:37 AM

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