Connect with us

C- reading in a character

Discussion in 'Microcontrollers, Programming and IoT' started by foTONICS, Nov 28, 2011.

Scroll to continue with content
  1. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    Hey all,

    I'm working on a lab for programming and have run into a hiccup. Below is a sample of my code (edited for clarity):

    #include <stdio.h>
    #include <stdlib.h>

    char d;

    int main (void)
    {
    printf("*\t\tCharacter Arithmetic\n");
    printf("***************************************************\n");
    printf("* Please select one of the following options\n");
    printf("* Followed by the proper response\n");
    printf("***************************************************\n");
    printf("* Functions:\n");
    printf("* a. isDigit\n");
    printf("* b. isLetter\n");
    printf("* c. toUpper\n");
    printf("* d. toLower\n");
    printf("* q. Quit\n");
    printf("***************************************************\n");
    printf(" CHOOSE A FUNCTION: ");
    char f=getchar();
    system ("cls");

    switch (f)
    {
    case 'a':
    printf("Please enter a digit:");
    scanf("%c", &d);
    printf("you entered %c", d);
    system("PAUSE");
    }
    }


    my problem is that after i choose my function, in this example 'a', my program doesnt wait for me to enter a digit, it skips the scanf command and just jumps straight to the second printf statement in my switch statement.

    any guidance would be appreciated
     
  2. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    okay so i scanned in the character 'd' as a integer (%d) instead of %c, and its somewhat working, any reasoning behind this?

    ASCII?
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-