Thread: A very simple doubt

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    A very simple doubt

    Hi,

    Im unable to to read and print the 2nd char in the following code. please help.it is not taking the 2nd input from the keyboard. I know that it is taking the new line char from the buffer instead of giving an option to enter another char. I tried using scanf, still the same prob. i tried fflush, it didnt work. please help me to solve this problem.

    input :a

    output: ch1=a
    ch2=


    Code:
    #include<stdio.h>
    int main()
    {
        char ch1,ch2;
        ch1=getchar();
        printf("ch1=%c\n",ch1);
        ch2=getchar();
        printf("ch2=%c\n",ch2);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    getchar will take whatever character is in the buffer, enter-key or no.

    If you want scanf to skip "whitespace" characters (space, tab, enter-key), then you have to put that in your format specifier:
    Code:
    scanf(" &#37;c", &ch1);

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    thank you.

    But i want to do it using getchar option. Is is possible? if yes then how?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    getchar will read in \n when it is typed. You can check: if the result of the getchar is \n, read in another character.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How are you typing the input characters. Is it a character followed by newline followed by another character?
    If that's the case then ch2 will contain newline. Instead type one character followed by another w/o interleaving whitespace.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Character followed by a newline. actually ch1 and ch2 in my actual code come at two different places in a 400 line code.So i need to enter new line and be able to get the next char to be accepted. I need the following to happen:

    input:
    a followed by newline
    b followed by newline

    output:
    ch1=a
    ch2=b

    please provide me with the code as it would be really helpful.

    thank you.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    In that case all you need to do is to loop while reading from stdin while sifting out the whitespace or have three instances of getchar().

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Sorry, im a beginner in c. Can you please provide me with the code for shifting the white spaces. sorry again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 04:41 AM