Thread: C Problem

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    C Problem

    hello everyone,
    I have a program below :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    	int i;
    
    	while((i=getchar())!=EOF)
    	{
    		printf("%c ",i);
    	}
    	
    	system("pause");
    }
    What I saw when this program runs is that characters input are not printed at the same time as is clear from the while loop.
    What I want is that characters should be printed as soon as it is entered.
    Can u tell me some solution to it ???????

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. Read the intro threads - you'll learn about such things as code tags - fixed
    2. Pay attention to the forum you post in - this is C, and you posted in C++ - also fixed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Input and output (to stdout) are buffered by default.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The characters are normally echoed, meaning that you see what you print. Don't know what you mean with your question.
    You should actually get double the input you post. One for the echo and one from printf().

    What do you get exactly?

  5. #5
    Registered User shrikanth's Avatar
    Join Date
    Aug 2010
    Location
    Banglore, India
    Posts
    5

    Smile

    Quote Originally Posted by sanjay.nitk View Post
    hello everyone,
    I have a program below :
    What I want is that characters should be printed as soon as it is entered.
    Can u tell me some solution to it ???????
    Code:
    #include<stdio.h>
    
    int main()
    {
      char ch,choice;
      int val=0;
      clrscr();
      while(val)
      {
        printf("\nenter the char\n");
        ch=getche();   //get the char from user
        putch(ch);     //immediately display that char
        printf("\nwant to continue..?(Y/N)\n");
        choice=getche();
        if(choice=='y' || choice=='Y')
           val=1;
        else val=0;
      }
     return 0;
     }
    As far as i understood ur problem statmnt, this is what i think you wanted..
    Last edited by shrikanth; 08-11-2010 at 01:31 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not use void main! SourceForge.net: Void main - cpwiki
    Furthermore, you are using val without initializing it.
    You are also too trigger happy in using non-standard constructs. Avoid getche at least. Use the standard getchar.
    Last edited by Elysia; 08-11-2010 at 12:34 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Here's a good K&R example.

    Code:
    #include <stdio.h>
    
    int main() {
         int x;
         while ((x = getchar()) != EOF)
              putchar(x);
         return 0;
    }
    This is a timeless classic, IMO.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Babkockdood: notice that your example is similiar to sanjay.nitk's code, except that it is simplified and uses putchar instead of printf, but that makes no real difference in this case (i.e., sanjay.nitk's observation will remain essentially the same).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User shrikanth's Avatar
    Join Date
    Aug 2010
    Location
    Banglore, India
    Posts
    5
    Quote Originally Posted by Elysia View Post
    Do not use void main! SourceForge.net: Void main - cpwiki
    Furthermore, you are using val without initializing it.
    You are also too trigger happy in using non-standard constructs. Avoid getche at least. Use the standard getchar.
    Elysia: thanks for ur advice on using void and usin uninitialized vars, i'l keep these in mind..
    But i couldn't use getchar and immediately print d char entered as sanjay.nitk wanted.. anyway i'l try again.. and what is the problem with getche()..?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    getche is not a standard C function. Therefore, it would be best to avoid if there is no absolute need. You should just keep that in mind.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM