Thread: The scanf function won't work! HELP!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    The scanf function won't work! HELP!

    Help me guys. this is a part of my program and the scanf part doesn't function.
    im using the Turbo C version.

    the one that is bold is the part that doesn't work. can't locate my error. HELP!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void startgame(char giver[], char guesser[], int life)
    {
    
    	int ctr, run = 0, repeat, ok, correct;
    	char phrase[100], clue[100], display[100], guessed[100], choice, guess;
    
    	printf ("\nEnter the SECRET phrase %s: ", giver);
    	gets (phrase);
    
    	printf ("Enter the CLUE phrase %s: ", giver);
    	gets (clue);
    
    	system ("cls");
    
    	printf ("*******PHRASE GUESSING GAME*******\n\n");
    	printf ("The CLUE given by %s is: %s\n", giver, clue);
    
    	strcpy (display, phrase);
    
    	for (ctr = 0; ctr < 100 && phrase[ctr] != '\0'; ctr++)
    	{
    	    if (phrase[ctr] == ' ')
    	    {
            	display[ctr] = ' ';
    	    }
    
    	    else
    	    {
            	display[ctr] = '-';
    	    }
    	}
    
    	printf ("\nThe PHRASE %s need to guess is: %s", guesser, display);
    
    
    while (life!=0)
    {
    	printf ("\n\n%s, Enter the letter of your choice:\na. Guess a letter in the phrase\nb. Guess the phrase\nc. Surrender\nChoice: ", guesser);
    	scanf ("%c", &choice);
    
    	if (choice=='a')
    	{
    		printf ("\n\nYou chose a. Guess a letter in the phrase.\nEnter your guess letter: ");
    		scanf ("%c", &guess);
    	}
    }
    
    }
    
    
    void main (void)
    {
    
    char name1[10], name2[10];
    int turn = 1, life1 = 4, life2 = 4;
    
    clrscr ();
    
    printf ("*******PHRASE GUESSING GAME*******\n\n");
    printf ("Enter Player 1's name: ");
    gets (name1);
    printf ("Enter Player 2's name: ");
    gets (name2);
    
    
    while (turn<=2)
    {
    	if (turn==1)
    	{
    	startgame (name1, name2, life2);
    	}
    
    	if (turn==2)
    	{
    	startgame (name2, name1, life1);
    	}
    
    turn++;
    }
    
    getch ();
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is because the second scanf call is grabbing the newline left in the console buffer from when you hit "a [enter]". You have this problem at a few points, perhaps. You can get around it either using getchar() after a scanf call:
    Code:
    scanf ("%c", &choice);
    getchar();
    Or, the somewhat better and easier solution: put a space at the beginning of the scanf template:
    Code:
    scanf (" %c", &guess);
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do NOT use void main: http://cpwiki.sf.net/void_main
    Do NOT use gets: http://cpwiki.sourceforge.net/Gets
    And finally, why it doesn't work: http://apps.sourceforge.net/mediawik...tle=Scanf_woes
    Oh yes, and do switch compiler. Turbo C is old, and very poorly standards compliant. A list of good alternatives can be found here: http://cpwiki.sf.net/IDE
    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.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Quote Originally Posted by MK27 View Post
    This is because the second scanf call is grabbing the newline left in the console buffer from when you hit "a [enter]". You have this problem at a few points, perhaps. You can get around it either using getchar() after a scanf call:
    Code:
    scanf ("%c", &choice);
    getchar();
    Or, the somewhat better and easier solution: put a space at the beginning of the scanf template:
    Code:
    scanf (" %c", &guess);
    thanks! it worked qith getchar().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. itoa function : doesn't work for -ve number ?????
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 03-22-2005, 06:50 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM