Using getchar, cant change variable?

  • Thread starter Thread starter charmedbeauty
  • Start date Start date
  • Tags Tags
    Change Variable
AI Thread Summary
The discussion centers on using getchar to read user input for a game, specifically converting characters like 'a' and '5' into array indices. When the character 'a' is processed with the operation letter = letter - 'a', it results in a value of 0, which corresponds to the null character, causing putchar to print nothing. To correctly map characters to array indices, subtract 'a' from letters ('a' to 'h' becomes 0 to 7) and subtract '1' from numeric characters ('1' to '8' becomes 0 to 7). This approach allows for proper indexing in a game like Reversi, where user input needs to be translated into array coordinates. Proper handling of character arithmetic is essential for achieving the desired functionality.
charmedbeauty
Messages
266
Reaction score
0

Homework Statement



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:
Physics news on Phys.org
charmedbeauty said:

Homework Statement



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].
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.
 
Mark44 said:
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.


Code:
 thanks will use next time

I think that's 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:
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.
 
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').
 
Back
Top