Using getchar, cant change variable?

  • Thread starter Thread starter charmedbeauty
  • Start date Start date
  • Tags Tags
    Change Variable
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to reading user input in C using the getchar function. Participants are exploring how to convert character inputs into array indices for a game called Reversi, specifically how to handle inputs like 'a5' to access elements in a two-dimensional array.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes their approach to reading characters and integers using getchar, aiming to convert a letter and a number into indices for an array.
  • Another participant explains that subtracting 'a' from the letter results in 0, which corresponds to the ASCII code for the null character, leading to no output when printed.
  • A suggestion is made to convert numeric characters by adding '0' to the integer value to display it correctly.
  • A participant clarifies their goal of mapping characters 'a' through 'h' to indices 0 through 7 for their game, indicating that they want to use the subtraction method for both letters and numbers.
  • Another participant confirms the mapping approach for both letters and numbers, explaining the ASCII arithmetic involved in the conversions.

Areas of Agreement / Disagreement

Participants generally agree on the method of converting characters to indices using subtraction, but there is no consensus on the overall implementation or the specific code structure needed to achieve the desired functionality.

Contextual Notes

Some limitations include the lack of complete code visibility, which may affect the understanding of the problem, and the potential for misunderstandings regarding character encoding and ASCII values.

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').
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
3K
  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K