Help with while loop please

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;

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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');
}

return 0;
}
almost: scanf("%d",'y'); needs to be scanf("%c",&play);
ok first off are you programming in C or C++
I'm doing a c++ course
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?
no of course not, here it is;

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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);

}

return 0;
}
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdlib>
#include <iostream>

using namespace 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;                 
     }
     else if(guess<randomNo)
     {
      cout << "too low" << endl;     
     }
     else if(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;
	}
Last edited on
ok in the code
1
2
3
if(play!='y');
	printf_s("thanks for playing"); 
	return 0;

you might want to change 'y' to 'n' just a suggestion if that doesnt work tell me and ill lokk for some more possibilities
Topic archived. No new replies allowed.