Thread: While loop help

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    11

    While loop help

    The while loop I have does what it is supposed to do but in a very round about way. I have a while loop(inside a function) that is supposed to call another function for two random numbers(1 - 6). It then checks to see if the sum of those numbers is equal to 7 or the point number which is determined earlier in the program. So basically it loops until it hits a 7 or point. When the loop runs if it is not a 7 or point right away it seems to display the same values for example:

    Die 1 = 1
    Die 2 = 2
    You rolled a 3!
    Die 1 = 1
    Die 2 = 2
    You rolled a 3!
    .
    .
    .
    This happens many times and then it gets new values for the dice. If they do not add up to 7 or the point value it will repeat the same message over and over. It keeps doing this until it eventually hits the 7 or point value and exits the loop. Do I need to call the rolling function differently or reset its values somehow? I am not quite sure what I am missing.
    Code:
     while (still_in != 0){                                     
                                                                               
                                  roll = rolling();  
                                  printf("Die 1 = %d\n", roll[0]);   
                                  printf("Die 2 = %d\n", roll[1]);
                                  sum = roll[0] + roll[1]; 
                                  printf("You rolled a %d!\n", sum);
                                  if (sum == 7){
                                     start = start - bet;
                                     printf("You lose. You now have $%d.\n",start);
                                     goto LOOP;}
                                  else if (sum == point){
                                     start = start + bet;
                                     printf("You win! You now have $%d.\n",start);
                                     goto LOOP;}
                                 
                                  }
    Code:
    int *rolling()
    {
      static int  r[1];
      int i;
     
      srand( (unsigned)time( NULL ) );
      for ( i = 0; i <= 1; ++i)
      {
         r[i] = rand()%6+1;
      }
    
      return r;
    }

  2. #2
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    You are reseeding the RNG everytime you call rolling. This is not necessarily. Just put it in main();

    Cheers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > static int r[1];
    ...
    > for ( i = 0; i <= 1; ++i)
    This is also a buffer overrun - there is no r[1] element.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program using while loop, swtich, and for loop
    By Tre Holland in forum C Programming
    Replies: 5
    Last Post: 11-04-2014, 12:03 AM
  2. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  3. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM