5 Replies - 365 Views - Last Post: 14 April 2012 - 02:51 AM Rate Topic: -----

#1 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 27-January 10

Program skips prompt (C)

Posted 13 April 2012 - 02:18 PM

Below is a single method for a program I am creating that simulates the game "Hangman". When this method is run, it skips the scanf prompt. Why is it doing this? Any help would be greatly appreciated.

void play_Hangman (void) {
     int  misses = 7;
     int  i;
     int  len;
     int  new_Round = 0;
     int  round_Count = 1;
     char choice;
     char *search;
     char* word = generate_Word();
     
     len = strlen(word);
     
     printf ("%s\n", word);
     for (i = 0; i < len; i++) {
         printf ("_ ");
             }
     while (new_Round == 0) {
         printf ("\nRound: %d", round_Count);
         
         printf ("\n%d misses remain\n", misses);
         noose_Display (misses);
         
         printf ("\nGuess a letter: ");
         scanf  ("%c", &choice);//************** THIS IS BEING SKIPPED ****************
         search = strchr (word, choice);
         
         while (search != NULL) {
               printf ("\nfound at %d\n", search - word + 1);
               search = strchr (search + 1, choice);
               }        
         round_Count++;
        }
     
     return 0;
     }



Interestingly enough, it will only skip it the first time and then the scanf function becomes available on the second loop. I am so confused...

Is This A Good Question/Topic? 0
  • +

Replies To: Program skips prompt (C)

#2 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 27-January 10

Re: Program skips prompt (C)

Posted 13 April 2012 - 02:26 PM

I got rid of everything that was irrelevant to my problem to make it easier to read. I still have the same problem. Here it is:

void play_Hangman (void) {
     int  new_Round = 1;
     char choice;

     while (new_Round == 1) {
         printf ("\nGuess a letter: ");
         scanf  ("%c", &choice);
         }
     return 0;
     }


even if i were to create a new program with just this method by using "void main (void)" instead of "void play_Hangman (void)", I have the same issue. The output looks something like this:

Guess a letter: a

Guess a letter:
Guess a letter: _

This post has been edited by ninjawesome222: 13 April 2012 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: Program skips prompt (C)

Posted 13 April 2012 - 02:37 PM

It's the age old problem with reading a character using scanf. Your input buffer will look like this after entering 'a':

a\n


An a with the newline character -- the enter you pressed for the input to be accepted. The scanf reads the a, but what about that newline? It's still in the input buffer and is processed by the scanf (because it is, after all, a character) the next time around.

Try a space before the %c to eat up the newline:

scanf  (" %c", &choice);

Was This Post Helpful? 1
  • +
  • -

#4 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 27-January 10

Re: Program skips prompt (C)

Posted 13 April 2012 - 02:40 PM

View PostJackOfAllTrades, on 13 April 2012 - 03:37 PM, said:

It's the age old problem with reading a character using scanf. Your input buffer will look like this after entering 'a':

a\n


An a with the newline character -- the enter you pressed for the input to be accepted. The scanf reads the a, but what about that newline? It's still in the input buffer and is processed by the scanf (because it is, after all, a character) the next time around.

Try a space before the %c to eat up the newline:

scanf  (" %c", &choice);


It works beautifully! Thanks so much for the explanation. I'll remember that for future coding adventures. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 120
  • Joined: 01-November 11

Re: Program skips prompt (C)

Posted 13 April 2012 - 05:16 PM

At uni, we were provided with a function to sort out this problem:

void readRestOfLine()
{
   int c;
   /* Read until the end of the line or end-of-file. */   
   while ((c = fgetc(stdin)) != '\n' && c != EOF);

   /* Clear the error and end-of-file flags. */
   clearerr(stdin);
}


If you call it after you take input from the console, it will read the rest of the characters in the input buffer until it finds either '\n' (newline) or EOF (end of file), and then it clears the EOF flag from stdin, which gets set if EOF was encountered. To be honest, I'm not entirely sure how EOF occurs when reading from stdin, but anyway. There man page for clearerr() is here:

http://www.manpagez....man/3/clearerr/

Also, using scanf() doesn't allow you to do any error or type checking on the input before you store it, I generally use fgetc() or fgets() to read input and then have a look at what the user entered before deciding whether to store it, perhaps parse it to the correct type, or throw an error.
Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

Re: Program skips prompt (C)

Posted 14 April 2012 - 02:51 AM

Quote

To be honest, I'm not entirely sure how EOF occurs when reading from stdin


EOF can be signaled by user input through Ctrl-Z on Windows and Ctrl-D on Linux/Unix/etc.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1