# Need of Assistance :)

Printable View

• February 8th, 2014, 08:57 AM
CoriNEIT
Need of Assistance :)
First things first. I go to New England Institute of Technology for Software Engineering and Cyber Security. I'm currently in my second quarter (15th week) and I'm starting to do a little more intermediate stuff. So, I made this program that deals random cards. The code is set to make it stop at 52 cards, but if you type anything over 52, it still deals the cards randomly. Firstly, I wanted to know if I had a fault in my code that should be stopping the cards but isn't or if it's not there at all........ I'll post the source for anyone to look at it.

Secondly, I' required to make a Texas Hold Em' program and my instructor explained that it's similar to the code that I will provide to you, but I honestly don't know where to begin. My teacher isn't very good at explaining code than what he does when he's not at school teaching us kids. So I was hoping to come to a place where people are more serious to teach and explain things that might help me understand all this more.

Thank you,
Cori
Attachment 32349
• February 8th, 2014, 09:16 AM
GCDEF
Re: Need of Assistance :)
I don't see anything here that stops dealing at 52
Code:

```                for (i = 1; i <= n; i++)                         draw_a_card();```
• February 8th, 2014, 09:33 AM
CoriNEIT
Re: Need of Assistance :)
What would the code be to stop the program from dealing more than 52?

Code:

```if (cards_remaining.empty())     break;```
or
Code:

```if (cards_remaining == 0) return 0;```
• February 8th, 2014, 09:44 AM
2kaud
Re: Need of Assistance :)
One way would be like this
Code:

```for (i = 1; i <= n && cards_remaining > 0; i++)         draw_a_card();```
• February 8th, 2014, 09:48 AM
CoriNEIT
Re: Need of Assistance :)
Alright, I'll try that out.

Thank you.

_____EDIT_____

So, that code did the trick at stopping the never ending card drawing. But the code that I added to my draw_a_card function isn't doing what it's supposed to do.

It doesn't send out the cout "Reshuffling" to the screen, which it's supposed to do......... Am I just confusing myself?
Code:

```if (cards_remaining == 0) {                 cout << "Reshuffing." << endl;                 cards_remaining = 52;                 for (int i = 0; i < 52; i++)                         card_drawn[i] = false;```
This is the whole function

Code:

```void draw_a_card() {         int r; // Random index (0 thru 12) into         // ranks array         int s; // Random index (0 thru 3) into         // suits array         int n, card;         if (cards_remaining == 0) {                 cout << "Reshuffing." << endl;                 cards_remaining = 52;                 for (int i = 0; i < 52; i++)                         card_drawn[i] = false;         }         n = rand_0toN1(cards_remaining--);         card = select_next_available(n);         r = card % 13;                // r = random 0 to 12         s = card / 13;                // s = random 0 to 3         cout << ranks[r] << " of " << suits[s] << endl; }         // Select-Next-Available-Card function.         // Find the Nth element of card_drawn, skipping over         // those elements already set to true.         //         int select_next_available(int n) {         int i = -1;         n++;         // At begininning of deck, skip cards already drawn.         while (card_drawn[i])                 i++;         while (n-- > 0) {                        // Do the following n times:                 i++;                                        // Advance to next card.                 while (card_drawn[i])        // Skip past cards                         i++;                                // already drawn.         }         card_drawn[i] = true;                // Note card to be drawn         return i;                                        // Return this number.```
• February 8th, 2014, 09:55 AM
2kaud
Re: Need of Assistance :)
Quote:

But the code that I added to my draw_a_card function isn't doing what it's supposed to do.
Which is? Have you used the debugger to see where the code is deviating from what you expected? When code isn't doing what it should then tracing the code through with the debugger helps to track down the issue.
• February 8th, 2014, 09:58 AM
2kaud
Re: Need of Assistance :)
Code:

```int i = -1;         n++;         // At begininning of deck, skip cards already drawn.         while (card_drawn[i])                 i++;```
Indexes of arrays start at 0.
• February 8th, 2014, 10:06 AM
CoriNEIT
Re: Need of Assistance :)
Quote:

Originally Posted by 2kaud
Which is? Have you used the debugger to see where the code is deviating from what you expected? When code isn't doing what it should then tracing the code through with the debugger helps to track down the issue.

Code:

```if (cards_remaining == 0) {                 cout << "Reshuffing." << endl;                 cards_remaining = 52;                 for (int i = 0; i < 52; i++)                         card_drawn[i] = false;```
My instructor never went into detail about how to use the debugger. I only know how to write very simple programs and how to fix simple mistakes to a degree.
Quote:

Originally Posted by 2kaud
Code:

```int i = -1;         n++;         // At begininning of deck, skip cards already drawn.         while (card_drawn[i])                 i++;```
Indexes of arrays start at 0.

Okay, never mind. I have figured it out. I was just too blind to see it. I need to fix the spacing so that "Reshuffling" is more noticable.
• February 8th, 2014, 10:09 AM
2kaud
Re: Need of Assistance :)
Quote:

I' required to make a Texas Hold Em' program and my instructor explained that it's similar to the code that I will provide to you, but I honestly don't know where to begin.
I would suggest that the first thing you want is for draw_a_card() to actually return the card drawn.

For the Texas Hold Em' program, first you need to know the rules. Then you need to design an algorithm for the rules. Then you produce a program design, then you code the program from the design and then test/debug the code.

You have a function that gets a card. So from the rules, in English, produce the design so that someone who doesn't know anything about cards can follow these (like a computer would with a program) using this.
• February 8th, 2014, 10:11 AM
CoriNEIT
Re: Need of Assistance :)
Quote:

Originally Posted by 2kaud
I would suggest that the first thing you want is for draw_a_card() to actually return the card drawn.

For the Texas Hold Em' program, first you need to know the rules. Then you need to design an algorithm for the rules. Then you produce a program design, then you code the program from the design and then test/debug the code.

You have a function that gets a card. So from the rules, in English, produce the design so that someone who doesn't know anything about cards can follow these (like a computer would with a program) using this.

Okay, thank you.
• February 8th, 2014, 10:21 AM
2kaud
Re: Need of Assistance :)
Quote:

My instructor never went into detail about how to use the debugger. I only know how to write very simple programs and how to fix simple mistakes to a degree.
Then now's a good time to learn to use the debugger. You need to become totally familiar with it. What os/compiler are you using?

Take your current card program and use the debugger to single step through the code, examine variables, set breakpoints etc and become familiar with it using a program you know. Then when you are trying to find a problem in another program that doesn't work as expected you will be familiar with the debugger and can then just use it to help find problems in the code. That's what we do. A program is seldom 'correct' the first time it is coded. So we use the debugger to determine what's happening and where what the program is doing is at variance with the design from which it was coded.
• February 8th, 2014, 10:39 AM
CoriNEIT
Re: Need of Assistance :)
Quote:

Originally Posted by 2kaud
Then now's a good time to learn to use the debugger. You need to become totally familiar with it. What os/compiler are you using?

Take your current card program and use the debugger to single step through the code, examine variables, set breakpoints etc and become familiar with it using a program you know. Then when you are trying to find a problem in another program that doesn't work as expected you will be familiar with the debugger and can then just use it to help find problems in the code. That's what we do. A program is seldom 'correct' the first time it is coded. So we use the debugger to determine what's happening and where what the program is doing is at variance with the design from which it was coded.

I'm running Windows 8 with Visual Studio Ultimate 2013
• February 8th, 2014, 10:45 AM
2kaud
Re: Need of Assistance :)
Then you have one of the best debuggers available. A bit of time taken now to get used to it will pay dividends later.
• February 8th, 2014, 11:12 AM
CoriNEIT
Re: Need of Assistance :)
Quote:

Originally Posted by 2kaud
Then you have one of the best debuggers available. A bit of time taken now to get used to it will pay dividends later.

Alright. Thank you for your advice and help. Since I have nothing to do until 5pm tonight, I'll spend some time on the debugger.

Thanks again,
Cori