Converting _getch() inputted numbers

This is a discussion on Converting _getch() inputted numbers within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to write some code that will work as part of a menu system. If the user presses ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Converting _getch() inputted numbers

    Hi,

    I'm trying to write some code that will work as part of a menu system. If the user presses enter, something will happen. If they press 1, something else will happen.

    To register the pressing of enter, I tried setting a variable to -1, then using "_getch()" to have the user input something- my thinking was that if the variable is still -1 afterward, then I know they pressed enter. If it's 1 then I know they pressed 1.

    However, when I use "input = _getch();", and the user presses enter, input ends up as 13, and when they press 1 it also ends up as something else. I'm pretty sure I need to convert input somehow but I don't know how. I tried messing around using typecasts and atoi and it all just screwed up.

    Can anyone help with this simple problem?

    Code:
    int main()
    {
        int input = -1;
        input = _getch();
        cout << input;
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Google ASCII. Or better, since these are characters, compare with '1' et cetera instead of 1.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Does the program have to move on if nothing is typed before pressing enter? Why not have an option for the user to input something that would do what it would do if they just simply hit enter. Just a suggestion.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use tabstop's idea, and <ENTER> will probably compare equal with either '\n' or '\r'.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Tabstop: Yeah, I realised before I could use '1'. The problem is, what happens when you want to do arithmetic on the stuff that is being put in?

    scwizzo: I'm not really sure I understand what you're getting at? You just mean press another key instead of enter, and do the same thing, or do you mean something different to that? I like enter because basically it's for a chat bot based program, and you press it repeatedly to see more lines of chat from the bots (it's more comfortable to repeatedly press enter than other keys). For now- I'll probably use some kind of random short time delay thing for each line later on.

    Thanks for the responses guys.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Yea that is what I meant, but now that we know what you're doing tabstop's idea is the route you want.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The problem is, what happens when you want to do arithmetic on the stuff that is being put in?
    _getch() only inputs a single number per call, so if the user entered 123, you'd have to combine them anyway. Or use cin instead of _getch() to input the numbers.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    I want to be able to take that '1' and then use it to sort through a list of objects of a certain type of class, with each object having a member integer variable that is an ID number.

    If it's '1' I can't do that, but if it's converted to 1 then I can say, if ( input == objectname->getid() ), you know...

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can get the value of single digits by subtracting '0' from the character (so '1'-'0' must equal 1). If the number has more than one digit, you'll have to loop through reading digit by digit and building the number as you go.

  10. #10
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by bengreenwood View Post
    I want to be able to take that '1' and then use it to sort through a list of objects of a certain type of class, with each object having a member integer variable that is an ID number.

    If it's '1' I can't do that, but if it's converted to 1 then I can say, if ( input == objectname->getid() ), you know...
    IMHO, you've got to change your way of thinking. Just think a char is like a number between 0 - 255 (unsigned char that is). And then you can actually use it similar to an int. So you can use it as an ID for an object on a note that it's not more than 256 objects. However with this method you'll most likely can't input a non printable char (less than 0x21 AFAIK) with getch.

    BTW, your line here:
    Code:
        int input = -1;
    Is rather unefficient because getch will return a char instead of an int. And (int) 1 != '1'. You can just convert it yourself (seeing as it's only 1 char) or use atoi after changing the input to a string / char * (atoi needs a const char * as the parameter instead of a char AFAIK). Hope this helps.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate Random Numbers and Assign to Days of Week
    By mms in forum C++ Programming
    Replies: 10
    Last Post: 05-04-2006, 02:51 AM
  2. converting a string of numbers into an integer value -How?
    By v3ct0r_sect0r in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 08:58 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. converting numbers to letters??
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-06-2002, 12:17 PM
  5. converting numbers from one base to another
    By partnole in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2001, 01:29 PM

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