Using getchar, cant change variable?

In summary: In both cases, the result is the number 1.In summary, the conversation is about using the getchar and putchar functions to read in and print out characters and numbers. The person is having trouble converting letters to numbers and is seeking help on using these functions to map characters to numbers for their program. They also mention using [ code ] tags when posting code for readability.
  • #1
charmedbeauty
271
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
  • #2
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.
 
  • #3
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.
 
  • #4
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').
 
  • #5


I would like to offer some suggestions to help you with your code.

Firstly, when using getchar() to read in user input, it is important to note that it reads in only one character at a time. So in your example, when the user inputs "a5", the first getchar() will read in 'a' and the second getchar() will read in '5'.

Secondly, when you use the expression "letter=letter - 'a'", you are essentially trying to subtract a character from another character. This operation does not make sense and will not give you the desired result.

To convert a character to an integer, you can use the function atoi() which takes in a character array and returns an integer. For example, you can do something like this:

char input[2]; //assuming the user inputs two characters
int number;

input[0] = getchar(); //read in first character
input[1] = getchar(); //read in second character

number = atoi(input); //convert input to integer

Now, if the user inputs "a5", the variable "number" will have the value 5.

Lastly, I would recommend using a debugger or printing out the values of your variables at different stages of your code to help you understand what is happening and where the issue might be.

I hope this helps you with your code. Good luck!
 

FAQ: Using getchar, cant change variable?

Why am I unable to change my variable value using getchar?

Getchar is primarily used for reading single characters from the standard input stream. It does not have the capability to modify variable values, as it only reads input and returns it as an integer value. Therefore, it cannot be used to change a variable's value.

2. Can I use getchar to read multiple characters at once?

No, getchar reads and returns only one character at a time. If you want to read multiple characters, you can use a loop and read each character individually using getchar.

3. How do I clear the input buffer while using getchar?

The input buffer can be cleared by using the fflush() function. This function clears any unread characters from the input buffer, allowing you to start fresh when using getchar to read input.

4. Is getchar the only way to read input in C?

No, there are other functions such as scanf and fgets that can also be used to read input in C. However, getchar is commonly used for reading single characters from the standard input stream.

5. How can I handle input errors while using getchar?

Getchar returns the integer value of the character read, so you can use conditional statements to handle any input errors. For example, if the value returned by getchar is EOF, it indicates an error or end of file, and you can handle it accordingly in your code.

Similar threads

Back
Top