what is the best method to take a user input of a character and convert this to a specific number? Then store this number to an array. I have a bit of code that I have re-written multiple times and so far have not been able to get it to work.
printf("Row: ");
scanf("%d",&fire[0]);
fire[0]--;
I would like to take in letter input from the user and write an integer to the fire array as the value. Example (A=1, B=2, C=3...)
You want to read a character and convert it to its offset from A
+ 1:
int fire[10];
char ch;
printf("Row: ");
if (scanf(" %c", &ch) != 1) {
/* end of file */
return 1;
}
if (ch >= 'A' && ch <= 'Z') { /* assuming ASCII */
fire[0] = ch - 'A' + 1; /* A -> 1, B -> 2, etc. */
} else {
/* not an uppercase letter */
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.