getchar() not working?

This is a discussion on getchar() not working? within the C Programming forums, part of the General Programming Boards category; I want the code to be able to repeat, but after entering 'y', the program terminates. After trying to debug ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    getchar() not working?

    I want the code to be able to repeat, but after entering 'y', the program terminates.
    After trying to debug it, I've narrowed it down to the getchar() at the end not working because when I try to print repeat after getting it, it doesn't print.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle(int [][13]);
    void deal(int [][13], char *[], char *[]);
    int ftns(int [5], int [5]);
    
    int main()
    {
     char *suit[4]={"Hearts", "Diamonds", "Clubs", "Spades"};
     char *face[13]={"Ace", "Deuce", "Three", "Four", "Five", "Six",
      "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    	 
     int deck[4][13] = {0};
     char repeat = 'y';
     
     do	//use while loop
     { 
       printf("MADE IT HERE");
     srand(time(0));
    	 
     shuffle(deck);
     deal(deck, face, suit);
    
    	/*Repeat if necessary*/
    	getchar(); /*get garbage*/
    	printf("\n\nWould you like to repeat this process? (Y/N) ");
    	printf("\nRepeat before:       %c\n", repeat);
    	repeat = getchar();
           printf("Repeat after:   %c", repeat);
    
     }
    while(repeat == 'y' || repeat == 'Y');
     
    	return 0; 
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    hmmm...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > getchar(); /*get garbage*/
    You might try
    g = getchar(); /*get garbage*/
    printf( "The garbage char was %c\n", g );

    Did shuffle() and deal() read any input?
    If they did, did they use scanf() (poorly), or fgets() (wisely)

    There may be no garbage to get rid of, so all you're doing is throwing away your good answer.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    Bay Area, CA
    Posts
    53
    I think what's happening is after pressing enter for the first getchar, the second getchar eats the newline ('\n') that's still in the buffer. Since newline is not equal to either y or Y, the loop ends after one go.

    After getting rid of the first getchar and flushing the input buffer after the second getchar, the loop worked as intended for me (although without knowing what the other functions do, obviously, since only main was posted). It keeps going until something other than y or Y is entered.
    Last edited by Sorin; 01-18-2009 at 12:46 AM.

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Quote Originally Posted by Sorin View Post
    I think what's happening is after pressing enter for the first getchar, the second getchar eats the newline ('\n') that's still in the buffer. Since newline is not equal to either y or Y, the loop ends after one go.

    After getting rid of the first getchar and flushing the input buffer after the second getchar, the loop worked as intended for me (although without knowing what the other functions do, obviously, since only main was posted). It keeps going until something other than y or Y is entered.
    Yeah I agree with Sorin. Use something else like fgets or make your own get char where you use getchar plus a an extra line for eating up left over characters like newline

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    You can try some like
    PHP Code:
    while (!isalpha(ch getchar())); 
    This should eat up newline char or anything other than a letter. I think.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    It's not that reading to a newline is a problem.

    It's that if the last thing read was a newline (eg, by calling fgets()), then all the attempts to read to a newline will inevitably trash the next line of input the user expected to type.

    So instead of "where's my input gone", the question becomes "why do I have to press return twice".
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    While you are figuring out what's wrong, you may as well look at http://cpwiki.sourceforge.net/Indentation for fixing your horrible indentation and http://apps.sourceforge.net/mediawik...arameter_names for explanation of why removing parameter names from prototypes is bad.
    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. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  2. getchar() != '\n'
    By Morgan in forum C Programming
    Replies: 4
    Last Post: 03-06-2003, 05:41 AM
  3. getchar() help!
    By Brian n. in forum C Programming
    Replies: 5
    Last Post: 12-10-2002, 10:44 PM
  4. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 12:11 PM
  5. getchar() and while loops
    By dbaryl in forum C Programming
    Replies: 2
    Last Post: 07-26-2002, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21