468,504 Members | 1,946 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

Problem with loop in Bingo game C++

I am writing code for the game of BINGO. The code below shows a function that fills the card with random numbers. A random number in each location, 0-15 for B column; 16-30 for I column, 31-45 for N column, 46-60 for G column, and 61-75 in O column. This is a 5X5 2 D array. I am having problems with this function, it isnt working as it should.

No compile error, but output is nothing but a flashing cursor, maybe this is an infinite loop?? can someone help with this.

Here is the function code:

Expand|Select|Wrap|Line Numbers
  1. //*************************** FillCard Function *******************************
  2. void  fillCard ( int card [] [csize] )
  3. {
  4.  
  5.          for ( int col = 0; col < csize ; col++ )
  6.                { 
  7.                for ( int row = 0; row < csize ; row++ )
  8.                    {
  9.                          bool samenum = false;
  10.                          int num1 = rand ()% 15 * col;                         
  11.                    do{
  12.  
  13.  
  14.                    for(int check = 0; check < row; check++){
  15.                            if (num1 == card[check][col])
  16.                                samenum = true;
  17.  
  18.                    }}while (samenum);
  19.                            card[col][row] = num1;
  20.  
  21.  
  22.                    }
  23.  
  24. }
  25. }
  26.  
  27.  
  28. //**************************END of FILLCARD ***********************************
  29.  

Thanks for the help always - - - - - - - - - - - - - - - - - - - -
Mar 6 '08 #1
12 10565
weaknessforcats
9,207 Expert Mod 8TB
Have you tried stepping through the function using your debugger?
Mar 6 '08 #2
Have tried the debugging, but to no effect. I have also searched high and low for other solutions but still have come up with nothing.
Mar 6 '08 #3
weaknessforcats
9,207 Expert Mod 8TB
OK I see the %15 to get a random number between 0 and 14 but I don't see any other % calcuations for the other columns.

Why is that?

Then you have 3 loops. That's odd. I would have expected two. One for the rows and one for the columns.


BTW: That %15 gives a value in the range 0 to 14. Yoiu need to add 1 to get the range 1-15.
Mar 6 '08 #4
Expand|Select|Wrap|Line Numbers
  1. int num1 = rand ()% 15 * col;
  2.  
this will give %15 * 0 for first column which is the same as %15 + 1 + 0
this will give % 15 * 1 for second column which is the sameas %15 + 1 + 15

and so on,

that part is correct
Mar 6 '08 #5
MACKTEK
40
samenum has no way to become untrue once it is true. So you loop indefinitely.
Also
Your for loop is going to iterate as long as check is LESS than row.
But, your if statement:
if (num1 == card[check][col]) samenum = true;
will evaluate FOR ALL CASES during the loop until the loop is done. (it has no exit for when samenum is True)
So, basically, samenum will be true for all cases once it is true for the first time.

Try to fix that, and go thru the logic, and see if there are other problems.
Mar 6 '08 #6
Ok i have fixed some things and I have the code posted below. I was stuck in the loop, and it should have been %15 + 1 + (15 *col). Thanks for pointing that out. This function is fixed but the next one isn't working properly. The problem now is that after this function completes the displayCard function is called to display all the random numbers. Here are both functions:

Expand|Select|Wrap|Line Numbers
  1. //*************************** FillCard Function *******************************
  2. void  fillCard ( int card [] [csize] )
  3. {
  4.       int num1;
  5.       bool samenum;
  6.          for ( int col = 0; col < csize ; col++ )
  7.                { 
  8.                for ( int row = 0; row < csize ; row++ )
  9.                    {
  10.  
  11.  
  12.                    do{
  13.                            samenum = false;
  14.                            num1 = rand ()% 15 +  1 + (15 * col);
  15.  
  16.                    for(int check = 0; check < row; check++){
  17.                            if (num1 == card[check][col]){
  18.                                  samenum = true;
  19.                                  }}
  20.  
  21.                    }while (samenum);
  22.                            card[col][row] = num1;
  23.                            card[2][2] = 0;
  24.  
  25.                    }
  26.  
  27. }
  28. }
  29.  
  30.  
  31. //**************************END of FILLCARD ***********************************
  32.  
  33. //************************ DisplayCard Function *******************************
  34. void  displayCard ( int card [][csize] )
  35. {
  36.       for ( int col = 0 ; col < csize ; col++ )
  37. {
  38.           for ( int row = 0; row < csize ; row++ )
  39.           {
  40.           cout  <<setw(5)<<card[col][row];
  41.           cout <<endl;
  42.  
  43.  
  44. }
  45. }      
  46.  
  47.  

Here is the output I am getting. This should be outputting the numbers in the 5X5 2D Array, but instead it puts them on one line after another. What should I change to output the numbers correctly??

(output)
11
1
8
3
5
23
19
18
29
26
31
42
0
42
43
55
59
51
46
47
72
75
62
74
66


These numers are correct they are just output in the wrong way.
the first five numbers are for column 0, the next five for col 1, and so on.
How can i fix this
Mar 6 '08 #7
MACKTEK
40
  1. int num1 = rand ()% 15 * col;
  2. do{
  3. for(int check = 0; check < row; check++){
  4. if (num1 == card[check][col])
  5. samenum = true;
  6. }}while (samenum);
I am still trying to figure out this code.
It seems to me that once samenum is TRUE, it will never exit the loop.
The only reason you have not recieved this error, is random luck.

In regards to your ouput,
remove the endline command to outside your first nesting.
Mar 6 '08 #8
  1. int num1 = rand ()% 15 * col;
  2. do{
  3. for(int check = 0; check < row; check++){
  4. if (num1 == card[check][col])
  5. samenum = true;
  6. }}while (samenum);
I am still trying to figure out this code.
It seems to me that once samenum is TRUE, it will never exit the loop.
The only reason you have not recieved this error, is random luck.

In regards to your ouput,
remove the endline command to outside your first nesting.

Take a look at the most recent code - (e.g. the last post i made before this one)
samenum is initialized to false..........

it only becomes true once a truly random number is found, therefore...
the code after the while in the do while loop executes if there isn't
a single other # like the number picked (or samenum is true, which means a good number). and with this loop if samenum isnt true, its false, remember i initialized it to false, so at the end it will turn false and we fall out of the loop
Keep in mind that a do while loop has to execute at least once..


Swapping the loops doesnt help any either, you could just swap the variables for columns(col) and rows(row).
Mar 6 '08 #9
MACKTEK
40
What happens if samenum is true?
Then it will loop infinitely.
Mar 6 '08 #10
will not loop infinitely, my point is CLEARLY stated above that in the end
samenum will evaluate to false, and fall out of the loop. That should answer your question, but if not review how loops of this sort work.
Mar 6 '08 #11
MACKTEK
40
Ok, remember I am here to help... so please help me understand some things as we progress...

You have these lines in your program:if (num1 == card[check][col]) samenum = true;

what is the point of these lines?
Does card[][] have values in it before you do these checks?

In regards to output:
try moving this line: cout <<endl;
Outside of the inner loop.
Mar 6 '08 #12
I fixed the problem with the loop and have code posed below. It was inside the inner loop, thanks. I was rushing through when I wrote the loop structure and left brackets in the inner loop...

Expand|Select|Wrap|Line Numbers
  1. //************************ DisplayCard Function *******************************
  2. void  displayCard ( int card [][csize] )
  3. {
  4.       for ( int col = 0 ; col < csize ; col++ )
  5. {
  6.           for ( int row = 0; row < csize ; row++ )
  7.  
  8.           cout  <<setw(5)<<card[row][col];
  9.           cout<<endl;
  10.  
  11.  
  12.  
  13. }   cout<<endl;       
  14. }   
  15. //****************************** END of DISPLAYCARD ****************************
  16.  
Mar 7 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

33 posts views Thread by Geoff Berrow | last post: by
1 post views Thread by banjo123 | last post: by
2 posts views Thread by Lee Garrington | last post: by
6 posts views Thread by Mike | last post: by
4 posts views Thread by Sačo Zagoranski | last post: by
7 posts views Thread by koonda | last post: by
1 post views Thread by rsteph | last post: by
omerbutt
1 post views Thread by omerbutt | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.