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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fernster
    New Member
    • Sep 2018
    • 2

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

    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(loopAga in);
    }

    return 0;
    }


    When I test it only prints 1 letter, Im very new to programming sorry
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • Fernster
      New Member
      • Sep 2018
      • 2

      #3
      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

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.

        Comment

        Working...