473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 11221
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.