Hi, I have to make a little program, well it's sort of a game of chance that generates random numbers and you have to guess the number...well I need to nest a while loop but I'm not having much luck with the code I have...I need it to continue with the game after the user guesses the correct number and after they choose 'y' here's what I have so far;
int main()
{
int randomNo;
int guess=0;
char play='y';
srand(time(NULL)); /* Loops program when user selects 'y'*/
while (play == 'y')
{
/* Generate a random number 1 - 100 */
randomNo = 1 + (rand() % 100);
/* Loops program while condition is true*/
while (guess != randomNo)
{ /* Prompt for and read user's guess */
printf_s("Please enter your guess:\n");
scanf_s("%d", &guess);
/* Determine whether user guessed correctly */
if (guess==randomNo)
printf_s("\nWell done - you guessed it!\n");
else if (guess < randomNo)
printf_s("Too low!\n");
else if (guess > randomNo)
printf_s("Too High!\n");
}
printf("Would you like to play again? 'y' or 'n' \n");
scanf("%d",'y');
}
hi coder777, I changed the code like you showed me, and it asks if I want to play again 'y' or 'n' but it also asks me to press any key to continue... and then it ends.
well i use cout for an output function as opposed to printf which is a c function but whatever works for you is fine
but i dont see why this would happen
press any key to continue...
you dont have thesystem("PAUSE");
anywhere so if it closes out of the loop it should automatically close the program, do you mind if i see your updated code?
int main()
{
int randomNo;
int guess=0;
char play='y';
srand(time(NULL)); /* Loops program while user selects 'y'*/
while (play == 'y')
{
/* Generate a random number 1 - 100 */
randomNo = 1 + (rand() % 100);
/* Loops program while condition is true*/
while (guess != randomNo)
{ /* Prompt for and read user's guess */
printf_s("Please enter your guess:\n");
scanf_s("%d", &guess);
/* Determine whether user guessed correctly */
if (guess==randomNo)
printf_s("\nWell done - you guessed it!\n");
else if (guess < randomNo)
printf_s("Too low!\n");
else if (guess > randomNo)
printf_s("Too High!\n");
}
printf_s("Would you like to play again? 'y' or 'n' \n");
scanf_s("%c",&play);
i changed your code to c++ and it works just fine for me ill try to find an error but it is kind off hard because i dont use scanf_s for variable input but ill try to study it a little more other than that your code seems to work fine
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int argc, char *argv[])
{
//must declare when generating random number
srand(time(0));
//declare variables
int randomNo;
int guess=0;
char play='y';
while (play=='y')
{
// Generate a random number 1 - 100
randomNo = 1 + (rand() % 100);
// Loops program while condition is true
while (guess != randomNo)
{
//this is c++ code for outputting a string
cout <<"Guess the random number" << endl;
cin >> guess;
if(guess==randomNo)
{
cout << "youre correct!" << endl;
}
elseif(guess<randomNo)
{
cout << "too low" << endl;
}
elseif(guess>randomNo)
{
cout << "too high" << endl;
}
}
cout << "Would you like to play again(y/n)?" << endl;
cin >> play;
//clear the screen
system("cls");
}
return 0;
}
yes, it works fine in c++ but it does not appear to work in c...am i missing something? Below is an updated version...the problem is when you guess the correct number it outputs 2 "would you like to guess again" y/n for the output and nothing happens when you select either 'y' or 'n'...Any help would be appreciated as I've spent way too much time on this already
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int randomNo;
int guess=0;
char play='y';
srand(time(NULL));
/* Generate a random number 1 - 100 */
randomNo = 1 + (rand() % 100);
/* Loops program while condition is true*/
/* Prompt for and read user's guess */
while (play=='y')
{
printf_s("Please enter your guess:\n");
scanf_s("%d", &guess);
while (guess != randomNo)
{
/* Determine whether user guessed correctly */
if (guess < randomNo)
printf_s("Too low !\n");
else if (guess > randomNo)
printf_s("Too High!\n");break;
}
while(guess == randomNo)
{
printf_s("Well done - you guessed it!\n");
printf_s("would you like to guess again? y/n \n");
scanf_s("%c", &play);
}
}
if(play!='y');
printf_s("thanks for playing");
return 0;
}