472,981 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

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 11174
weaknessforcats
9,208 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,208 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

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

Similar topics

33
by: Geoff Berrow | last post by:
I may have mentioned that I run an Introduction to PHP course at a local college (very basic - I'm no PHP expert). Well, one of my students was doing really well so I set him some extension work. ...
1
by: banjo123 | last post by:
Dear java.help: I just started learning java and was trying to compile some java sourc from the Javascript World Submission webpage. I know I have loaded m JDK successfully since I can compile...
2
by: Lee Garrington | last post by:
Hey, Recently I decided to learn C++ so that I could port over one of my Java programs to make it faster. Basically everything has ported over fine so far until I came up against the following...
6
by: Mike | last post by:
Hello, This game will not loop. It compiles, builds, and executes fine, but the game will not loop around no matter what I do. What is wrong with the code? What do I have to add to make it loop...
4
by: Sačo Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
7
by: koonda | last post by:
Hi all, I posted my message earliar and I got some positive feedbacks but that didn't help me to solve some programming problems. I have an Assignment due 20th of this month, next monday. The...
1
by: rsteph | last post by:
I bought a book to help me learn to use DirectX with windows programming. It's first trying to walk me through some basic windows programming and graphics before getting into DirectX. I'm trying to...
1
omerbutt
by: omerbutt | last post by:
hi every one i have a menu li and ul based the problem is when any specific category in the li is hovered the li or the sub-cat items appear but as i move my mouse over the sub-cat or level two li it...
1
by: rockchicx23 | last post by:
i have to write a program that is a guessing the number game. the user has to choose the number of games they want to play. i need help setting up the loop that will run the number of times the user...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.