Thread: Every other scanf not working

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    In a box
    Posts
    1

    Every other scanf not working

    Ok, I have two problems. first of all, every other scanf is ignored. and second, of all it ends up displaying:

    To play Simple Simon, watch the screen for a sequence of digits.
    Watch carefully, as the digits are only displayed for a second!
    The computer will remove them, and then prompt you to enter the same sequence.
    When you do, you must put spaces in between the digits.

    Good luck!
    Press enter to play

    7 8
    Now enter your sequence - don't forget the spaces
    7 8
    Wrong!


    Your score is 0
    Do you want to play agan (y/n)?
    Ok... well its suppost to have the numbers BELOW the enter your sequence and its suppost to display them for a second and than have them disappear... and theres suppost to be three digits to start with... but it only outputs 2... but instead it waits a second and than outputs everything after the first 78... everythings EXACTLY how it is in the book so I don't know whats wrong =P (My only guess is that its an old book)
    Also, it stops at do you want to play agan... and ignores the scanf... heres the code...

    Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <time.h>
    
        #define TRUE 1
        #define FALSE 0
    
        int main(void)
        {
    	char another_game = 'Y';
    	char correct = TRUE;
    	int counter = 0;
    	int sequence_length = 0;
    	int i = 0;
    	long seed = 0;
    	int number = 0;
    	long now = 0;
    	long time_taken = 0;
    
    	printf("\nTo play Simple Simon, ");
    	printf("watch the screen for a sequence of digits.");
    	printf("\nWatch carefully, as the digits are only displayed for a second!");
    	printf("\nThe computer will remove them, and then prompt you");
    	printf(" to enter the same sequence.");
    	printf("\nWhen you do, you must put spaces in between the digits. \n");
    	printf("\nGood luck!\nPress enter to play\n");
    	scanf("%c", &another_game);
    
    	do
    	{
    	  correct = TRUE;
    
    	  counter = 0;
    
    	  sequence_length = 2;
    
    	  time_taken = clock();
    
    	  while(correct)
    	  {
    	    sequence_length += counter++%3 == 0;
    
    	    seed = time(NULL);
    
    	    now = clock();
    
    	    srand((int)seed);
    	    for(i = 1 ; i <= sequence_length ; i++)
    	      printf("%d ", rand() % 10);
    
    	    printf("\r");
    	    for(i = 1; i <= sequence_length; i++);
    	      printf("  ");
    
    	    if(counter == 1)
    	      printf("\nNow enter your sequence - don't forget the spaces\n");
    	    else
    	      printf("\r");
    
    	    srand((int)seed);
    	    for(i = 1 ; i <= sequence_length ; i++)
    	    {
    	      scanf("%d", &number);
    	      if(number != rand() % 10)
    	      {
    		correct = FALSE;
    		break;
    	      }
    	    }
    	printf("%s\n", correct ? "Correct!" : "Wrong!");
    	  }
    
    	  time_taken = (clock() - time_taken) / CLOCKS_PER_SEC;
    
    	  printf("\n\n Your score is %d", --counter * 100 / time_taken);
    
    	  fflush(stdin);
    	  fflush(stdout);
    
    	  printf("\nDo you want to play agan (y/n)? ");
    	  scanf("%c", &another_game);
    	} while(another_game == 'y' || another_game == 'Y');
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > srand((int)seed);
    You should only do this once, right at the start of the program. rand() doesn't get any more random by calling srand() repeatedly, and in fact can get a lot less random if you call srand() too much.

    > for(i = 1; i <= sequence_length; i++);
    That last ; means this loop does nothing.

    > fflush(stdin);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    If this is in your book, you need another book.
    If your compiler ignores this call, which it is perfectly entitled to do, then that will surely mess up your scanf call to read the next character. It will instead read the newline at the end of the previous input, not the Y/N you typed in for another go.

    If your book also talks about using gets() as well, then that's another sign of a bad book.
    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.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    If your compiler ignores this call, which it is perfectly entitled to do, then that will surely mess up your scanf call to read the next character. It will instead read the newline at the end of the previous input, not the Y/N you typed in for another go.
    Using %1s instead of %c will solve the whitespace problem. %1s skips whitespace, %c doesn't.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Using &#37;1s would also require changing the variable from a simple char to an array of char with at least 2 elements.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Using scanf in a for loop
    By agentsmith in forum C Programming
    Replies: 2
    Last Post: 12-18-2007, 09:37 AM
  4. Counter Errors for otherwise working program???
    By jereland in forum C Programming
    Replies: 7
    Last Post: 03-22-2004, 08:37 PM
  5. Quzah's printf and scanf tutorial...
    By quzah in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 03:59 AM