multiple getchar() in a row

Discussion in 'C' started by MareoRaft, Aug 28, 2010.

  1. MareoRaft

    MareoRaft New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Here is a simple program which gives me unexpected results:

    int main()
    {

    printf("\nTo calculate a^b, enter value a now.\n");
    int a = getchar() - '0';
    printf("\nEnter b now.\n");
    int b = getchar() - '0';
    printf("a = %d, b = %d\n\n", a, b);

    return 0;
    }
    if i hit a digit for a, then hit enter, it assigns -38 as the value of b.

    the only way it works is if i enter a and b right next to each other, such as 52. this is not how i want it to behave. your advice greatly appreciated!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice