Thread: Problem with repeats in the output

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Problem with repeats in the output

    At first I want to say, that I'm a beginner and I'm just curious, why is the result in the output doubled and due what it's so.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char ch;
        
        do {
            
            printf("Enter char: \n");
            ch = getchar();
            if(ch == 'q') break; 
            printf("Hello World ! \n");
            
            
        } while(ch!='q');
        
        
    }
    And this is the output:
    Code:
    Enter char: 
    a
    Hello World ! 
    Enter char: 
    Hello World ! 
    Enter char:

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    When you press 'a' then <enter>, you actually put two characters in the input buffer, the 'a' and a newline. You need to flush the input buffer: Cprogramming.com FAQ > Flush the input buffer.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You also need to return 0.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    Thank you !

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    OK I'm curious. I am guilty of using a scanf and fflush (stdin); which everyone tells me is not good. However this program can be fixed in about .2 seconds doing it that way.

    I've been playing with the FAQ method of clearing the input buffer and cannot get a smooth run of this simple program. Here is where I'm at with the other posters program:

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    
    
    
    
      char   ch;
      char  buf[1];
      
      do
      {
    	
    	
    	printf ("Enter a Char: ");
    	ch = getchar();
    	while ((ch = getchar()) != '\n' && ch != EOF);
    	if (fgets(buf, sizeof(buf), stdin))
      {
      printf ("Hello world!\n");
      }
     
      
      }while (ch != 'q');
      return 0;
    }
    This program will run as expected but it never breaks the loop. My guess is that's because the input buffer is clear before the do while gets to the while part so it never sees the q. How do you get around this.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    My gosh... all that just to ignore a character?

    Code:
    do
      { if (ch != '\n')
          printf("Enter a character : ");
         ch = getchar();
        if (ch != '\n')
          printf(" You entered : %c",ch); }
    while (tolower(ch) ! = 'q');
    Let the thing houseclean itself.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Whenever you get to the end of your do-while loop, ch must needs be '\n'. Did you intend to compare buf[0]? Or, if you are looking for the char that was entered, then use a different char variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. repeats in a string
    By jodders in forum C++ Programming
    Replies: 20
    Last Post: 02-23-2010, 07:46 AM
  2. Problem with Output
    By vileoxidation in forum C++ Programming
    Replies: 11
    Last Post: 10-10-2009, 11:21 PM
  3. Random number generation without repeats
    By kishore84 in forum C Programming
    Replies: 1
    Last Post: 02-05-2009, 12:17 PM
  4. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  5. do loop repeats one time too many
    By sunburnbyRA in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 05:26 PM