-4

Ok so i made a program in c that is asking the user to choose 1 of 4 stages he would like to play in. After the user chooses the stage, the program is generating a secret code that built out of 4 chars and its between 1-6 (for example- 3516). But i wanted to make the chars non-duplicated, so there will be no 2 numbers the same (for example- 1642 its good but 6632 its bad code). After i made a few "if"s in my code and i ran it, it just stacked. This is my code:

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

int getStages();
int randCode();

int main()
{
    getStage();

    system("PAUSE");
}

int getStage()
{

    int choice= 0;

    printf("What stage would you like to choose? Choose Wisely: ");
    scanf("%d", &choice);

    randCode();
}

int randCode()
{
    srand(time(NULL));

    int randFirst= rand() % 6 + 1;
    int randSecond= rand() % 6 + 1;
    int randThird= rand() % 6 + 1;
    int randFourth= rand() % 6 + 1;

     while(randFirst = (randSecond || randThird || randFourth))
     {
         int randFirst= rand() % 6 + 1;
     }

     while(randSecond = (randFirst || randThird || randFourth))
     {
         int randSecond= rand() % 6 + 1;
     }

     while(randThird = (randFirst || randSecond || randFourth))
     {
         int randThird= rand() % 6 + 1;
     }

     while(randFourth = (randFirst || randSecond || randThird))
     {
         int randFourth= rand() % 6 + 1;
     }

    char firstNumber= randFirst;
    char secondNumber= randSecond;
    char thirdNumber= randThird;
    char fourthNumber= randFourth;

    printf("%d %d %d %d\n", firstNumber, secondNumber, thirdNumber, fourthNumber);


}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Ofek Ezon
  • 29
  • 1
  • 5

3 Answers3

0
  1. = is not same as ==.
    a = b means assign b to a. a == b means check whether a equals to b or not.

  2. To check an variable for equivalence to more than one variable form expression as

    ((foo == var1) || (foo == var2) || (foo == var3) ...)


while(randFirst = (randSecond || randThird || randFourth))

This is causing the problem.

What this expression does is to assign 1 to randFirst if any of randSecond, randThird or randFourth is non-zero. Otherwise assign 0 to randFirst (When all other remaining 3 variables are 0)

And then if randFirst got assigned 1 (which is most probable) then, while is always true since in loop too randFirst gets assigned to non-zero number (1 to 6). So an infinite loop.

Similar thing happens with all other remaining while loops.

Instead use:

while((randFirst == randSecond) || (randFirst == randThird) || (randFirst == randFourth))  

in all subsequent while expressions.

rootkea
  • 1,474
  • 2
  • 12
  • 32
0

You declare the variables randFirst to randFourth locally inside the loops, shadowing the other variables by the same name you have. The values you assign within the loop are only assigned to these loop-local variables.

This of course have an effect on the loop conditions (once you fix them) as the loop condition will use the function-local variables. If you enter a loop, that loop will be infinite.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The condition in the while looop is always true and hence it goes infinite time because (a==b) is comparision where as a=b is assignment which is always true.