Guessing Game Java Prompt to Guess Again
Thread: Number Guessing Game
-
11-05-2015 #1
Registered User
Number Guessing Game
I take made a programme which prompts the user to guess an integer at random between one and 1000. Here is the code along with comments:
The problem is that at the cease of the plan if the user selects Y to play again (I am aware that anything other than Y volition cease it), Information technology only tells them they guessed the number correctly again and asks if they want to play again. I have been researched how to articulate the input buffer with scanf() to no avail. I accept also researched fgets(), and fscanf(); fgets() seems to only deal with char and fscanf() actually requires a file to be used as an statement. I am too bold there is a much more than legitimate fashion to articulate the input buffer with ane of these other functions. Could someone please signal me in the right direction? Thanks!Code:
// standard library #include "stdafx.h" #include <stdio.h> #include <iostream> #include <math.h> #include <stdlib.h> #include <time.h> unsigned int random(); // image role // first program int main() { srand(fourth dimension(NULL)); // randomize random number generator without seed unsigned int guessNumber = 0; // guess input received from user unsigned int randomNumber = random(); // random number generated by function char ch = 'y'; // user input to continue or end programme printf("I have a number between 1 and k.\nCan y'all guess my number?\nPlease type your first estimate.\northward\n"); do { // do while loop for playing the game while (randomNumber != guessNumber) { // inner while loop for when the user input does not equal the random number scanf("%u", &guessNumber); // // obtain user's guess if (randomNumber > guessNumber) { // if the random number is higher than the user's guess printf("Besides low. Try again.\n"); } else { // if the random number is lower than the user'southward guess printf("Besides high. Attempt again.\due north"); } } if (randomNumber = guessNumber) { // if the random number is equal to the user's guess printf("\nExcellent! You guessed the number!\north"); printf("\nWould you like to play again?\n\n"); ch = getchar(); // read character input from stdin scanf("%c", &ch); // obtain yep or no from user } } while (ch == 'y'); organisation("intermission"); return 0; } // cease program // function random unsigned int random() { unsigned int theRandomNumber = 1 + (rand() % 1000); return theRandomNumber; }
-
11-05-2015 #2
Registered User
Are you trying to learn C or C++?
Your first comment is wrong, <stdafx.h> is not a standard header.
The <iostream> header is a C++ header and will not work with a C compiler, your not using this so I advise you remove this line.
Y'all need to acquire the difference between the comparison operator== and the assignment operator=.
Code:
if (randomNumber = guessNumber)
Expect at this snippet:
What are y'all trying to accomplish with the last 2 lines? You tin force scanf() to skip leading whitespace by putting a infinite earlier the specifier:Lawmaking:
if (randomNumber = guessNumber) { // if the random number is equal to the user's guess printf("\nExcellent! You guessed the number!\n"); printf("\nWould you similar to play over again?\n\due north"); ch = getchar(); // read character input from stdin // What character are you trying to read??? scanf("%c", &ch); // obtain yes or no from user }
Code:
scanf(" %c", &ch); // Note the space.Remember C is case sensitive 'y' is not the same as 'Y'.The problem is that at the end of the program if the user selects Y to play again
Lastly your indentation could use some serious improvements, be consistent.
Jim
-
eleven-05-2015 #3
Registered User
I stock-still the scanf office; it now looks equally such:
Originally Posted by jimblumberg
Also, I apologize for not being clear, still information technology is 'y' that I input not 'Y', I simply typed 'Y' in my mail service.Code:
if (randomNumber == guessNumber) { // if the random number is equal to the user's gauge printf("\nExcellent! Y'all guessed the number!\north"); printf("\nWould y'all like to play again?\n\n"); scanf(" %c", &ch); // obtain aye or no from userThe winning number is still in the input buffer after the user enters 'y', so it immediately gives the user the message "You guessed the number! Would y'all like to play once again?" Rather than starting at the beginning. I suspect this is because the winning number is still in the input buffer, which is why I want to articulate it.
-
xi-05-2015 #4
Registered User
Did you fix the post-obit problem?
What most the #include problems and indenation?
Originally Posted by jimblumberg
Jim
-
eleven-05-2015 #5
Registered User
My code at present looks as such:
Originally Posted by jimblumberg
Lawmaking:
#include "stdafx.h" // standard library #include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> unsigned int random(); // prototype function // offset program int main() { srand(time(NULL)); // randomize random number generator without seed unsigned int guessNumber = 0; // guess input received from user unsigned int randomNumber = random(); // random number generated by function char ch = 'y'; // user input to keep or end programme printf("I have a number between 1 and thousand.\nCan you guess my number?\nPlease type your first guess.\due north\north"); exercise { // do while loop for playing the game while (randomNumber != guessNumber) { // inner while loop for when the user input does not equal the random number scanf("%u", &guessNumber); // // obtain user's guess if (randomNumber > guessNumber) { // if the random number is higher than the user's guess printf("Also low. Try again.\north"); } else { // if the random number is lower than the user's guess printf("Too high. Try once again.\n"); } } if (randomNumber == guessNumber) { // if the random number is equal to the user's estimate printf("\nExcellent! You guessed the number!\n"); printf("\nWould you like to play again?\n\n"); scanf(" %c", &ch); // obtain yes or no from user } } while (ch == 'y'); system("pause"); render 0; } // end program // role random unsigned int random() { unsigned int theRandomNumber = ane + (rand() % 1000); return theRandomNumber; }
-
eleven-05-2015 #6
Registered User
So does your program work properly now?
If not, draw the problem and ask specific questions.
Jim
-
11-05-2015 #7
Registered User
I have now made changes to this portion of the code:
Originally Posted past jimblumberg
andCode:
char ch[ane]; // user input to continue or end program
Nonetheless, at present when the plan runs and the user guesses the correct answer, it prints "Would yous like to play again?", I enter a y for yes, it brings me to the next line, I enter a y for yes again, and information technology ends the plan. Why doesn't fgets() store y into ch every bit it is supposed to, so the do-while loop works properly? Should I utilize fgetc() instead?Code:
if (randomNumber == guessNumber) { // if the random number is equal to the user'southward gauge printf("\nExcellent! Y'all guessed the number!\north"); printf("\nWould you like to play over again?\n\northward"); scanf("%s%*c", &ch); fgets(ch, 99, stdin); } } } while (ch == "y");
-
11-05-2015 #8
Registered User
Why are you fifty-fifty using fgets()? And why are you lot using the "%s" format specifier in scanf()?
All y'all should need is:
JimCode:
if (randomNumber == guessNumber) { // if the random number is equal to the user'south guess printf("\nExcellent! You guessed the number!\n"); printf("\nWould you like to play once again?\n\n"); scanf(" %c", &ch); // Notice the space earlier the %! } } } while (ch == "y");
-
11-05-2015 #ix
Registered User
I am thinking the logic in inside this do-while loop is what is giving me trouble. Now, when the user enters the winning number and is prompted to enter 'y' to play once again, I enter 'y' once and so it goes to a new line. I enter 'y' once more and it puts me into an infinite loop. It seems to be bypassing the scanf() that comes right afterward inner while loop condition, am I correct?Code:
do { guessNumber = 0; randomNumber = random(); while (randomNumber != guessNumber) { // inner while loop for when the user input does not equal the random number scanf(" %u", &guessNumber); // // obtain user's approximate if (randomNumber > guessNumber) { // if the random number is higher than the user's guess printf("Likewise low. Try again.\north"); } else if (randomNumber < guessNumber) { // if the random number is lower than the user's guess printf("Too high. Try again.\n"); } } if (randomNumber == guessNumber) { // if the random number is equal to the user'southward guess printf("\nExcellent! You guessed the number!\n"); printf("\nWould you like to play over again? "); scanf(" %c", &ch); } } while (ch == 'y');
-
xi-05-2015 #x
Registered User
Beginning your indentation needs work, this'll make following the logic easier:
Your problem is that afterwards you enter the 'y' and press enter the program is expecting you to enter some other estimate. Since the prompt is above the loop you won't get prompted.Lawmaking:
exercise { guessNumber = 0; randomNumber = random(); while (randomNumber != guessNumber) // inner while loop for when the user input does not equal the random number { scanf(" %u", &guessNumber); // // obtain user's judge if (randomNumber > guessNumber) // if the random number is higher than the user'due south estimate { printf("Too low. Try again.\n"); } else if (randomNumber < guessNumber) // if the random number is lower than the user'southward guess { printf("Also loftier. Attempt over again.\north"); } } // This if argument is not actually needed, the only way y'all're going to go here is if the ii variables are equal. if (randomNumber == guessNumber) // if the random number is equal to the user'south guess { printf("\nExcellent! You guessed the number!\n"); printf("\nWould you like to play again? "); scanf(" %c", &ch); } }while(ch == 'y');And so move the prompt inside the loop and yous should be ready.
Jim
-
xi-05-2015 #xi
Registered User
I take succeeded in getting the programme to work in-so-far equally I want it to. This is the code:
I know my indentation needed piece of work..shall this suffice?Code:
#include "stdafx.h" // standard library #include <stdio.h> #include <math.h> #include <stdlib.h> #include <fourth dimension.h> unsigned int random(); // prototype part // kickoff program int principal() { srand(time(Nil)); // randomize random number generator without seed unsigned int guessNumber = 0; // guess input received from user unsigned int randomNumber = random(); // random number generated past part char ch = 'y'; // user input to continue or cease program do { printf("\nI have a number betwixt 1 and 1000.\nCan you gauge my number?\nPlease type your kickoff gauge.\n\north"); guessNumber = 0; while ((randomNumber != guessNumber)) // inner while loop for when the user input does non equal the random number { scanf("%u", &guessNumber); // // obtain user's guess if (randomNumber > guessNumber) // if the random number is higher than the user's gauge { printf("As well depression. Try again.\due north"); } else if (randomNumber < guessNumber) // if the random number is lower than the user'due south guess { printf("Also high. Endeavor again.\n"); } } if (randomNumber == guessNumber) // if the random number is equal to the user'due south guess { printf("\nExcellent! You guessed the number!\n"); printf("\nWould you like to play again (y or n)? "); scanf(" %c", &ch); } } while (ch == 'y'); arrangement("pause"); render 0; } // end program // function random unsigned int random() { unsigned int theRandomNumber = ane + (rand() % 1000); return theRandomNumber; }
-
xi-05-2015 #12
Registered User
Better but still a lilliputian off. Should look more than like:
JimCode:
//#include "stdafx.h" // standard library #include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> unsigned int random(); // prototype function // start plan int main() { srand(fourth dimension(NULL)); // randomize random number generator without seed unsigned int guessNumber = 0; // guess input received from user unsigned int randomNumber = random(); // random number generated by function char ch = 'y'; // user input to go on or terminate program do { printf("\nI take a number between 1 and 1000.\nCan you guess my number?\nPlease type your get-go approximate.\n\n"); guessNumber = 0; while ((randomNumber != guessNumber)) // inner while loop for when the user input does non equal the random number { scanf("%u", &guessNumber); // // obtain user's estimate if (randomNumber > guessNumber) // if the random number is higher than the user'due south judge { printf("Likewise low. Try again.\n"); } else if (randomNumber < guessNumber) // if the random number is lower than the user's guess { printf("Too loftier. Try again.\n"); } } printf("\nExcellent! You guessed the number!\n"); printf("\nWould you like to play once more (y or n)? "); scanf(" %c", &ch); }while (ch == 'y'); system("intermission"); render 0; } // terminate program // function random unsigned int random() { unsigned int theRandomNumber = i + (rand() % m); render theRandomNumber; }
Similar Threads
-
Replies: 4
Last Post: 05-23-2011, ten:04 AM
-
Replies: 1
Last Postal service: 02-23-2011, 06:52 PM
-
Replies: xvi
Concluding Post: 10-05-2007, 12:47 PM
-
Replies: 7
Final Postal service: 06-22-2002, 09:03 AM
-
Replies: viii
Last Post: 01-15-2002, 05:00 PM
mcknightmaske1955.blogspot.com
Source: https://cboard.cprogramming.com/c-programming/168614-number-guessing-game.html
Post a Comment for "Guessing Game Java Prompt to Guess Again"