423,688 Members | 1,879 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

How do I convert an alphanumeric # to a numeric in C

P: 2
This is what I have so far.

[code]
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <math.h>
#include <string.h>

int main(void){

char letter;

char loopAgain = 'Y';
while (loopAgain == 'Y'){

printf("Enter a letter: \n");
scanf(" %c", letter);
letter = toupper(letter);

if (letter == 'A' || letter == 'B' || letter == 'C'){
printf("2");
}
else if (letter == 'D' || letter == 'E' || letter == 'F'){
printf("3");
}
else if (letter == 'G' || letter == 'H' || letter == 'I'){
printf("4");
}
else if (letter == 'J' || letter == 'K' || letter == 'L'){
printf("5");
}
else if (letter == 'M' || letter == 'N' || letter == 'O'){
printf("6");
}
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S'){
printf("7");
}
else if (letter == 'T' || letter == 'U' || letter == 'V'){
printf("8");
}
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z'){
printf("9");
}

printf("\n");
printf("Repeat? Y/N: ");
scanf(" %c", &loopAgain);
loopAgain = toupper(loopAgain);
}

return 0;
}


When I test it only prints 1 letter, Im very new to programming sorry
Sep 11 '18 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,186
What letter did you enter and what letter printed?

If you don't know how to use your debugger, this is an excellent opportunity to try it out. Debugging is every bit as important as writing the code in the first place.
Sep 11 '18 #2

P: 2
When I entered any of the letters from #2-9 it printed 1 letter in the debugger
For example:
Enter a letter:
adt
4
Repeat? Y/N:

Iím not sure how to make it show multiple numbers
Sep 11 '18 #3

Expert 100+
P: 2,384
Notice the last line printed was Press any key to continue... but the posted source code should have printed Repeat? Y/N:. The program youíre running is not the program you posted.
Sep 12 '18 #4

Post your reply

Sign in to post your reply or Sign up for a free account.