Find a number help - 09/27/06 07:29 PM
Can someone help me if possible?
I have wrote a program to guess a randomly generated number correctly within 3 goes.
Nearly cracked it, just one final thing I noticed when I was debugging, if I put 2 wrong answers in, then enter the right answer on the third go, it will tell me I have had too many goes and kick me out, but if I increase the count condition by one, it keeps telling me that my third answer is always correct, even if it isn't.
I have already tried moving the increment value to outside the loop, but obviously after the first loop, the value will not change.
Could someone have a look and see if it can be rectified or suggest any ideas?
I have wrote a program to guess a randomly generated number correctly within 3 goes.
Nearly cracked it, just one final thing I noticed when I was debugging, if I put 2 wrong answers in, then enter the right answer on the third go, it will tell me I have had too many goes and kick me out, but if I increase the count condition by one, it keeps telling me that my third answer is always correct, even if it isn't.
I have already tried moving the increment value to outside the loop, but obviously after the first loop, the value will not change.
Could someone have a look and see if it can be rectified or suggest any ideas?
Code
#include <stdio.h> #include <stdlib.h> #include <time.h> // Guess a number 1-6 with 3 tries void main() { int a, ans; int count = 0; srand((unsigned) time (NULL)); ans = ((rand()%6)+1); printf("%d\n", ans); printf("Three chances to to guess a number between 1 - 6: "); scanf("%d", &a); while ((a != ans) && (count < 2)) { count++; printf("Wrong! Please try again: Number of goes so far (%d): ", count +1); scanf("%d", &a); } if (count >=2 ) { printf("Too many goes. End of game. The number was %d\n", ans); } else { printf("Correct!\n"); printf("You had %d goes.\n", count + 1); } }