Thread: Need help with Snap card game loop.

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

    Need help with Snap card game loop.

    This is a simplified snap card game, I don't need to have the program do anything but to tell me if the value of the card matches with the last set, but it seems to go into a infinite loop, can anyone please tell me which part went wrong? I looked many places and couldn't find an answer to it. Please and many thanks.


    Code:
    #include <stdio.h>
    
    char o, n, s;
    
    int main() {
    	printf("input your cards. eg. 3D as 3 of Diamonds.\n");
    	scanf("%c%c", &n, &s);
    	while(n!=o) {
    		o=n;
    		scanf("%c%c", &n, &s);
    	}
    	printf("SNAP\n");
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Clue: "Newlines" ('\n'), are characters too. Pressing "enter" adds one. So what happens is the first scanf reads (for example) '3' into n, 'D' into s...
    ...then the next scanf reads '\n' into n, '3' into s
    ...then the next scanf reads 'D' into n, '\n' into s
    ...then the pattern starts again
    Meaning you never get a correct correspondence.

    You can get out of that by telling scanf to ignore a character:
    Code:
    scanf("%c%c%*c",&n,&s);
    Notice the * which tells scanf to discard this item.

    That will screw up too if the user accidentally leaves spaces at the end, so a more robust solution is:
    Code:
    char n, s, discard = 0;
    [...]
    scanf("%c%c",&n,&s);
    while (discard != '\n') scanf("%c",&discard);
    This will read the first two characters of input then chuck the rest up to and including the next newline. Important to discard to anything other than 10, which is the ASCII value for '\n'. If you don't know what ASCII character values are, they are numeric values representing the basic character set:

    http://www.asciitable.com/

    So, eg, 'A' == 65.
    Last edited by MK27; 04-03-2011 at 09:19 AM.
    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
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Thanks a lot. I got really stuck there, the hint you gave me was a big help and I was able to find out where I did wrong thanks to it. Many thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  2. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. game loop
    By X PaYnE X in forum Windows Programming
    Replies: 6
    Last Post: 02-15-2005, 01:33 PM
  5. Bridge Card Game
    By IneedHelp in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2003, 06:32 PM