Thread: getchar () and declare variable

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    getchar () and declare variable

    I need to write a program to turn the characters inputed into ASCII value and output the value,

    so i use getchar() and I encounter a problem.

    Because I getchar() only allow me to input a character and a time and I have to declare a variable for that, what if in the case where there could be infinity characters to be input.

    Also the project forbid me to use array so I wonder if there is a solution to this with loop , or while something like that~

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your intuition is right. A while loop sounds perfect. if you have an ascii based system (as most do), then all your letters and numbers you input, are already in ascii value. Whether an 'a' is printed as an 'a', or as it's ascii value of 64, depends on how you ask for it to be printed:

    1) as a letter: a

    or

    2) as a number: 97

    Here's a small start to your program.

    Code:
    printf("\n Enter q to quit ");
    while((ch=getchar()) != 'q') 
       printf("\n %c equals %d ", ch, ch);
    If you don't have a quick reference for the ascii table values, goto:
    ascii tables
    and download one. You'll use it a lot while you're programming.

    Why does the above code work on so many keypresses at once? Because your system has a keyboard buffer that will hold - better yet, you tell me how many keypresses the keyboard buffer on your system will hold.


    And welcome to the forum.
    Last edited by Adak; 10-18-2009 at 05:25 AM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    this is exactly what I needed ! Thanks !

    and I guess the keyboard buffer could hold infinite keypress isn't it?

    by the way what's the purpose for the 2nd ch in the statement

    printf("\n %c equals %d ", ch, ch);

    this is the first time I see such coding^^"
    Last edited by sfff; 10-18-2009 at 10:59 PM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Code:
    printf("\n Enter q to quit ");
    while((ch=getchar()) != 'q') 
       printf("\n %c equals %d ", ch, ch);
    I am also curious...

    since ch is an integer....why is "%c" needed ?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Obelisk View Post
    Code:
    printf("\n Enter q to quit ");
    while((ch=getchar()) != 'q') 
       printf("\n %c equals %d ", ch, ch);
    I am also curious...

    since ch is an integer....why is "%c" needed ?
    %c means "print the character".

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    oh i see...

    jus tested that code out...i got wat u meant..thx

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    okay i see , thx

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Hi i have another question,
    I wanna carry out a bit rotation (shift right to 1bits for 1st ASCII value, shift right 2bits for 2nd value and so on, until the 8th value with no bit rotation), how could this be done combine with the getchar() ?

    I am thinking of turn the ACSII into binary number , and then do the bit rotation,

    yet I google out and found that there shd be an easier way to directly carry out bit rotation with ACSII value,
    but I am uncertain how to form the coding.
    Last edited by sfff; 10-19-2009 at 12:25 AM.

Popular pages Recent additions subscribe to a feed