Thread: While Loop Not Reinstating

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    24

    While Loop Not Reinstating

    Hi all,

    I recently started writing a program that generates a random number, then gives the user four chances to guess the number. Upon either a correct guess, or upon using all four chances, the program is supposed to prompt the user to either play again or quit the program. My problem is in the end of the while loop in the main function. The code is below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <time.h>
    
    void guess( );
    
    int main( )
    {
    	char again = 'y';
    	
    	while (again == 'y')
    	{
    		printf("\nTry to guess a number between 1 and 100.\n");
    
    		guess( );
    
    		printf("\n\nWould you like to play again? (y/n): ");
    		scanf("%c", &again);
    		printf("%c", again);
    		fflush(stdin);
    	}
    	printf("\n*****Program Terminated*****\n\n");
    
    	return (EXIT_SUCCESS);
    }
    void guess( )
    {
    	int number;
    	int counter=1;
    	int random;
    
    	srand(time(NULL));
    	random = 1 + (rand() % 100);
    
    	while (counter <= 4)
    	{	
    		printf("You have four tries to guess the number.\nYour guess: ");
    		scanf("%d", &number);
    		fflush(stdin);
    
    		if (number == random)
    		{
    			printf("\nHooray! You win!");
    			counter = 5;
    		}
    		else if (counter == 4)
    		{
    			printf("\nSorry, you lose.  The number was %d.", random);
    		}
    		else 
    		{
    			printf("\nSorry, that is incorrect -");
    			if (number < random)
    			{
    				printf(" the number is larger.");
    			}
    			else
    			{
    				printf(" the number is smaller.");
    			}
    			printf("\n\nYou have %d guesses left.", 4 - counter);
    		}
    		counter++;
    	}
    }
    When I run the program, it prints the statement asking the user whether to try again, but then it doesn't let me input a "y" or "n". Instead, it automatically exits the loop and terminates the program.

    Any comments or suggestions would be greatly appreciated. Thanks.

    -Sean
    Last edited by seanksg; 04-04-2011 at 03:44 PM. Reason: Clarification

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    printf("\n\nWould you like to play again? (y/n): ");
    scanf("%c", &again);
    getchar();
    printf("%c", again);
    
    
    
    printf("You have four tries to guess the number.\nYour guess: ");
    scanf("%d", &number);
    getchar();
    fflush(stdin); does not clear the input queue... fflush() is intended to force output buffers to be written to disk.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When you press Y what do you press next? Enter. Ok, so you read the Y with your scanf call, and that leaves the ENTER key waiting to be read. Your next pass through the loop grabs that.

    "But I did this: fflush(stdin);"

    Well you should read this: Cprogramming.com FAQ > Why fflush(stdin) is wrong and then this: Cprogramming.com FAQ > Flush the input buffer


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    Code:
    printf("\n\nWould you like to play again? (y/n): ");
    scanf("%c", &again);
    getchar();
    printf("%c", again);
    
    
    
    printf("You have four tries to guess the number.\nYour guess: ");
    scanf("%d", &number);
    getchar();
    fflush(stdin); does not clear the input queue... fflush() is intended to force output buffers to be written to disk.
    I removed the fflush(stdin). It now allows me to enter a "y" or an "n", but when I press enter afterwards, it terminates the program, rather than restarting the loop. Is it because of using the enter key as quzah said?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No I just made all that up. Don't listen to me.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by quzah View Post
    No I just made all that up. Don't listen to me.


    Quzah.
    I can't tell if you're being sarcastic. The internet kind of prevents that. But if you are. Why? I'm new to C programming, and am just trying to learn... no need to be a dick.

    If not, sorry for calling you a dick, but why would you respond with something that doesn't help?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by seanksg View Post
    I removed the fflush(stdin). It now allows me to enter a "y" or an "n", but when I press enter afterwards, it terminates the program, rather than restarting the loop. Is it because of using the enter key as quzah said?
    Yes it's because of the enter key... just like Quzah said.

    Look I don't much like the guy... but he is a pretty good programmer and a willing helper. Like most of us he's at his best when you don't ignore his advice.

    That's what the getchar() calls are for in my code samples... they take the enter key out of the queue for you. But you appear to have ignored that too...

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    Yes it's because of the enter key... just like Quzah said.

    Look I don't much like the guy... but he is a pretty good programmer and a willing helper. Like most of us he's at his best when you don't ignore his advice.

    That's what the getchar() calls are for in my code samples... they take the enter key out of the queue for you. But you appear to have ignored that too...
    To both quzah and commontater, thank you for the help,

    quzah, i wasn't ignoring you. the faqs you pointed me to were informative, but this is a class assignment, and the faqs suggested commands that i'm not supposed to use.

    i'll just stick to my professor for help from now on

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummmm ... I know how you're going to react to this but I'm going to say it anyway...

    If your professor is teaching you to use stuff like fflush(stdin); he's not helping you one bit. You already have an example of how his requirements are wrong... Now you get to make a simple choice... Do you want to actually learn C programming or do you just want a good grade?

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    Ummmm ... I know how you're going to react to this but I'm going to say it anyway...

    If your professor is teaching you to use stuff like fflush(stdin); he's not helping you one bit. You already have an example of how his requirements are wrong... Now you get to make a simple choice... Do you want to actually learn C programming or do you just want a good grade?
    I honestly agree. I was very upset having learned that my prof gave me code that doesn't work. Saying that I would just stick to my professor for help was a frustrated response. I do want to learn the language, in fact, this class has sparked my interest in programming. I only meant that if I was going to look for help, I'd rather look in a place where I don't run the risk of being depricated for not understanding something right off the bat. But I guess I should have voiced the fact that I hadn't learned things in the faqs and didn't understand them.

    Thanks again though, and I do plan on showing my professor those faqs. You're absolutely right, he shouldn't be teaching that code.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by seanksg View Post
    I honestly agree. I was very upset having learned that my prof gave me code that doesn't work. Saying that I would just stick to my professor for help was a frustrated response. I do want to learn the language, in fact, this class has sparked my interest in programming. I only meant that if I was going to look for help, I'd rather look in a place where I don't run the risk of being depricated for not understanding something right off the bat. But I guess I should have voiced the fact that I hadn't learned things in the faqs and didn't understand them.

    Thanks again though, and I do plan on showing my professor those faqs. You're absolutely right, he shouldn't be teaching that code.
    Ok... little tip with computer geeks ... we're a pretty direct bunch. Not deliberatly rude just no nonsense communication. For some it's offputting but amongst our own "kind" it's a very efficient way of relating complex matters in minimums of time. Sometimes we do cross the line a little, but I seriously doubt it's done out of nastiness. The trick here --especially *here*-- is to understand that while it's direct and perhaps blunt... it's not necessarily meant to be offensive.

    LOL... Well, except when Quzah does it...
    Last edited by CommonTater; 04-04-2011 at 06:00 PM.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Quote Originally Posted by CommonTater View Post
    Ok... little tip with computer geeks ... we're a pretty direct bunch. Not deliberatly rude just no nonsense communication. For some it's offputting but amongst our own "kind" it's a very efficient way of relating complex matters in minimums of time. Sometimes we do cross the line a little, but I seriously doubt it's done out of nastiness. The trick here --especially *here*-- is to understand that while it's direct and perhaps blunt... it's not necessarily meant to be offensive.

    LOL... Well, except when Quzah does it...
    Duly noted... I'll spend some more time on the faqs and see if I can't decipher them a bit more.

    Quote Originally Posted by CommonTater View Post
    Yes it's because of the enter key... just like Quzah said.

    Look I don't much like the guy... but he is a pretty good programmer and a willing helper. Like most of us he's at his best when you don't ignore his advice.

    That's what the getchar() calls are for in my code samples... they take the enter key out of the queue for you. But you appear to have ignored that too...
    Just read this post... I put the getchar() into my code. So I'm led to believe that my problem is that I'm still not handling clearing the input buffer correctly... looking at the faq, I'm not sure how to apply it to my code. I have to read up more on some of the things it uses
    Last edited by seanksg; 04-04-2011 at 06:55 PM. Reason: clarification

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread