1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using getchar, cant change variable?

  1. Sep 27, 2012 #1
    1. The problem statement, all variables and given/known data

    I have some code and I use getchar to read in some input from the user say a5

    I have assigned

    char letter;
    int number;

    letter = getchar();
    number = getchar();

    so that putchar(letter) prints a

    and putchar(number) prints 5

    but when I try .... letter=letter - 'a';

    I am trying to make this my zero index of array.(i should probably also have a minus one ther for zero indexing.)

    putchar(letter) does not print anything??

    I am basically trying to convert the letters into integers so I can use an array with input say [a][5].



    HELPPPPP!!!!!!!
     
    Last edited by a moderator: Sep 27, 2012
  2. jcsd
  3. Sep 27, 2012 #2

    Mark44

    Staff: Mentor

    If letter is 'a', and you reset its value using letter = letter - 'a', then letter's new value will be 0, which is the ASCII code for the null character. That's why you aren't getting anything when you try to print it as a character, which is what putchar does. To convert a number to a numeric character, add '0' (ASCII code 48) to it.

    So for example, if letter is 3 (not the character '3'), you can convert the number 3 to the character '3' by adding '0'. putchar(3 + '0') will display 3.

    Without seeing more of your code, it's hard for me to tell what you are trying to do.

    BTW, when you post code, it is considered polite to put a [ code ] tag at the top and [ /code ] at the bottom (no spaces, though). It makes your code easier to read.
     
  4. Sep 27, 2012 #3

    Code (Text):
     thanks will use next time
    I think thats what I want though...

    I am trying to program a game called reversi and I have a [8][8] array.
    The user does not input numeric co-od's though ie instead of [0][0] the user input a1 which is meant to be the element of the array [0][0].

    thats why I thought I could do by using

    Code (Text):

    letter = letter -'a'
     
    so the letter a would be 0
    "" "" b would be 1

    "" "" h would be 7

    which then I could use in my array?

    Thanks.
     
  5. Sep 30, 2012 #4

    Mark44

    Staff: Mentor

    To map the characters 'a' through 'h' to the numbers 0 through 7, subtract 'a' from the input character.

    To map the characters '1' through '8' to the numbers 0 through 7, subtract '1' from the input numeric character.

    For example, if the user types the characters 'b' and '4', you would get 'b' - 'a' == 1 and '4' - '1' == 3. It might seem obvious why '4' - '1' is 3, but the arithmetic that is performed is not quite as simple. In C, characters are stored as their ASCII codes, so the subtractions that are performed are 98 - 97 (ASCII codes for 'b' and 'a') and 52 - 49 (ASCII codes for '4' and '1').
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook