While Loop Query - code displayed

This is a discussion on While Loop Query - code displayed within the C Programming forums, part of the General Programming Boards category; Hi, Anybody know why the code within the while loop won't function correctly? I basically want the user to enter ...

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

    While Loop Query - code displayed

    Hi,

    Anybody know why the code within the while loop won't function correctly? I basically want the user to enter letters until they run out of lives. But it keeps printing the text incorretly and not returning the correct answers.

    Code:
    while (lives !=0)
    
    {
    
    printf("\n\nPlease select a letter: ");
    scanf("%c", &letter);
    
    
    for (x=0; x < number; x++)
    
    {
    	this_guess == 0;
    	if (letter == gameword[x])
    		{
    			guessedword[x] = letter;
    			printf("Well done, you have chosen a correct letter:  %s\n", guessedword);
    			this_guess[x] = 1;
    		}
    
       if (this_guess[x] == 0)
    		{
    
    			printf("incorrect guess");
             lives--;
    
    		}
    
    }
    }
    Thanks

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should decide what should be done inside while loop and what - inside the for loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Can you not have a for loop inside a while loop? Sorry for the ignorance i've only been programming for a few weeks.

    Thanks

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    you do not need the subscript [x] on thisguess. And you have the brackets in the wrong places.

    Code:
    while (lives !=0) {
      printf("\n\nPlease select a letter: ");
      scanf("%c", &letter);
    
      this_guess = 0;   // This needs to be outside the for loop
      for (x=0; x < number; x++) {    
        if (letter == gameword[x]) {
          guessedword[x] = letter;
          printf("Well done, you have chosen a correct letter:  %s\n", guessedword);
          this_guess = 1;
        }
      } // End for loop here
      if (this_guess == 0)  { //This needs to be after the for loop.
        printf("incorrect guess");
        lives--;
      }
    }
    Last edited by Bladactania; 02-17-2009 at 09:46 AM. Reason: errors

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by darren78 View Post
    Can you not have a for loop inside a while loop?
    Of course you can. I would love it if you would indent them appropriately tho. (I think vart means something different.)

    One possible problem I noticed is that this:
    Code:
    	this_guess == 0;
    should be this
    Code:
    	this_guess = 0;
    Way, way back when I started with C I actually mixed this up so much I took a big red marker and scrawled
    ==
    on my keyboard above the function keys.
    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

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Yeah I fixed that but forgot to note it.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Excellent....thanks for your time guys.

    I have done everything above, but it seems to be looping through the while loop twice but only picking up certain parts. If i enter a letter, it will go through the loop correctly and then go through it again but will print out "please choose a letter" "incorrect guess" and will deduct a life?

    Eh?

    Any ideas?

    Thanks very much.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by darren78 View Post
    Excellent....thanks for your time guys.

    I have done everything above, but it seems to be looping through the while loop twice but only picking up certain parts. If i enter a letter, it will go through the loop correctly and then go through it again but will print out "please choose a letter" "incorrect guess" and will deduct a life?

    Eh?

    Any ideas?

    Thanks very much.
    We had someone ask the same question (in the context of a remote control, as I recall) just yesterday or so, so a board search may be useful here.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Probably something to do with the scanf picking up the '\n' character or something... I can't remember the fix to that though...

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put a getchar(); line after each scanf() call. Pulls that newline right off there.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    What if the user types "hello" then hits enter...

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I would suggest putting...
    Code:
    while (lives !=0) {
      printf("\n\nPlease select a letter: ");
      scanf("%c", &letter);
    
      if (letter != '\n') {  // Only accepts the letters
        this_guess = 0;   // This needs to be outside the for loop
        for (x=0; x < number; x++) {    
          if (letter == gameword[x]) {
            guessedword[x] = letter;
            printf("Well done, you have chosen a correct letter:  %s\n", guessedword);
            this_guess = 1;
          }
        }
      } // End for loop here
      if (this_guess == 0)  { //This needs to be after the for loop.
        printf("incorrect guess");
        lives--;
      }
    }
    note the if statement...

    incidentally, now if the user types several letters, your program will count/test/reject each one in turn until it hits the '\n'

    Just a warning - your program accepts ANY character, including numbers, &, !, etc...

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks all for your help.

    Will give it a go after dinner.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    No Problemmo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. Window message loop (Keys)
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 09-12-2006, 06:15 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 10:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21