Why is the random number I'm generating not changing within a loop? C -


Here's my current code I'm trying to create a game from which the computer generates a random number and the user thinks Is that it is more or less than the number shown. Although I can not get a random number to change it. Always

How can I get to change the random number so that the user is guessed rather than trapped in a loop: high or less than 6? More than 0 or less than 6? 0

#include & lt; Stdio.h & gt; Int main () {int rdmnum; Int rdm2; Int estimate; Int x = 1; Srand (time (faucet)); Rdmnum = rand ()% 9; Printf ("Enter 1 to make more estimation, enter lower number. \ N"); Printf ("more or less than% d? \ N", rdmnum); Scanf ("% d", and estimates); Do {if (estimate == 1 & amp;; & amp;; = = rdmnum) {printf ("% d or greater than? \", Rdmnum); Scanf ("% d", and estimates); X = 1; } And {x = 0; } If (Estimate == 0 & amp; amp; & amp; amp; estimated & lt; = rdmnum) {printf ("more or less than% d? \", Rdmnum); Scanf ("% d", and estimates); X = 1; } And {x = 0; }} While (x == 1); If (x == 0) {printf ("wrong number% d, finish game! \ N", rdmnum); Return 0; }}

Why the answer is rdmnum isn ' Change T:

Try this:

  // code do {rdmnum = rand ()% 9; // Keep this line in mind! If (Estimate == 1 & amp;; & amp;;; = = rdmnum) {printf ("More or less than% d? \", Rdmnum); // The rest of the code code  

You were running the rand () function, only once outside the loop. Therefore, you were only calculating random numbers once and used it every time.

By taking it into the loop, it repeats the loop every time, then you generate a new random number.

Edit:

After your comment, you have major problems with algorithms.

Two random numbers from your declaration and some extra points question is that I understand that your problem does not change with just rdmnum .

If your if s is also needed; Your second if overwrites previous results

You can try:

  if (guess == 1 & amp; amp; & amp; estimate> gt; Rdmnum) {printf ( "% D or greater than? \ N", rdmnum); Scanf ("% d", and estimates); X = 1; } And if (guess == 0 & amp; amp; amp; & amp; estimate> lt; = rdmnum) {printf ("more or less than% d? \", Rdmnum); Scanf ("% d", and estimates); X = 1; } And {x = 0; }  

In this way, if player estimates 1 (and it gets correct) then second if will not set x 0 Each time addition, you are testing range 0-1 against a number in the range 0-1. It returns results of too often input 0 , but 1 will be very few - just when your question will be output:

  More than or less than 0?  

I have taken my program to write again with simple logic. I have also added the use of another random number so that you are assessing that is the random number # 1 is greater than the random number # 2

< Pre> int main () {int target; Int display; Int estimate; Difference difference; Srand (time (faucet)); Printf ("Enter 1 to make more estimation, enter lower number. \ N"); While (true) {// we can compare it by comparing it to the number and hidden number displayed to it // to ensure that they are not equal {target = rand ()% 9; Displayed = rand ()% 9; } While asking (display == targets) // ask questions: printf ("more or less than% d? \", Displayed); Scanf ("% d", and estimates); If (estimates == 0 = estimates == 1) {// now see if they are correct = (target - displayed) & gt; 0; If (estimate == difference) {// they were the correct printf ("correct number% d! \ N", was the goal); } Else {// They were wrong - Exit Loop Print F ("wrong number% d, game over! \ N", target); break; }} // Bad input - just loop printf ("wrong input! \ N"); } Return 0; Also, fyi is usually in a nutshell: rnd not rdm < / Code>. No big deal, but when they read your code, helps others


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -