Help with smaller programs within a larger program 
Author Message
 Help with smaller programs within a larger program

Hi everyone!
I am a total newbie to C programming and have been struggling with a
problem for awhile now.  In my main program I simply ask the user to
chose from a number of games, then the code for the games is handled in
separate programs. The program that I'm having troubles with is the
blackjack counter program.  This program is supposed to prompt the user
to enter the number of cards he would like in his hand and then ask him
to input a value for each of these cards.  The program outputs all
possible values for the hand he has entered. Numbered cards 2-10, (10
is entered as 't' or 'T') are worth face value.  Face cards (jack,
queen, king) are worth 10 and an ace is worth either 1 or 11.  For
example a hand of 3 cards, 2-4's and one ace would either be 9 or 19.  
That is what should be output. The problem I'm having is when the
program begins execution it asks the user for the number of cards
properly.  But when asking for the values, it skipps every other card,
asking only for the 2, 4, 6th, etc. Example: Enter number of cards in
the hand:  5

Enter Card 1 Enter Card 2 4
Enter Card 3 Enter Card 4 4
Enter Card 5

It only allows for card 2, 4 to be entered.
If anyone can help me figure out what my program is doing, i would be
greatly appreciated.  I have studied this for hours and can't seem to
get anywhere on it. Thanks, Michelle

Here is the code:
blackjack.c
#include <stdio.h>

void play_blackjack(void)
{
        /*Declare variables*/
        int num, count, total, aces, possible;
        char value;
        count=1;
        /*Ask user to enter the number of cards */
        printf("\nEnter the number of cards in the hand:  ");
        scanf("%d", &num);
        /*Tell user to enter the card values */
        while (num>0){
                printf("Enter card #%d", count);
                scanf("%c", &value);
                /*Decide the value of the card entered*/
                /*Add the value of card to the running total */
                switch (value) {
                        case '2':
                          total=total+2;
                        break;
                        case '3':
                          total=total+3;
                        break;
                        case '4':
                          total=total+4;
                        break;
                        case '5':
                          total=total+5;
                        break;
                        case '6':
                          total=total+6;
                        break;
                        case '7':
                          total=total+7;
                        break;
                        case '8':
                          total=total+8;
                        break;
                        case '9':
                          total=total+9;
                        break;
                        case 't':
                        case 'T':
                        case 'j':
                        case 'J':
                        case 'q':
                        case 'Q':
                        case 'k':
                        case 'K':
                          total=total+10;
                        break;
                        /*Keep the number of aces seperate */
                        case 'a':
                        case 'A':
                          aces=aces+1;
                        break;
                }
                /*Reinitialize card so it will go back into the loop */

                value='l';
                num--;
                count++;
        }
        /*Compute and print out the possible values */
        printf("The possible values of this hand are ");
        possible=total+aces;
        num=0;
        while (aces >=0){
                printf("%d", possible+(num*10));
                aces--;
                num++;
        }

Quote:
}



Tue, 06 Apr 2004 12:45:56 GMT  
 Help with smaller programs within a larger program

Quote:

>    The problem I'm having is when the
>program begins execution it asks the user for the number of cards
>properly.

Nitpick:  This isn't a whole program, just a different function that
ends up getting linked into the same program as whatever calls it.
(This doesn't affect your question, but computers are extremely
pedantic about many things, so being extremely pedantic about
everything is a Good Thing to do when you're trying to talk to or about
computers.)

Quote:
>     But when asking for the values, it skipps every other card,
>asking only for the 2, 4, 6th, etc. Example: Enter number of cards in
>the hand:  5

>Enter Card 1 Enter Card 2 4
>Enter Card 3 Enter Card 4 4
>Enter Card 5

>It only allows for card 2, 4 to be entered.
>If anyone can help me figure out what my program is doing, i would be
>greatly appreciated.  I have studied this for hours and can't seem to
>get anywhere on it. Thanks, Michelle
>Here is the code:
>blackjack.c
>#include <stdio.h>

>void play_blackjack(void)
>{
>    /*Declare variables*/
>    int num, count, total, aces, possible;
>    char value;
>    count=1;
>    /*Ask user to enter the number of cards */
>    printf("\nEnter the number of cards in the hand:  ");
>    scanf("%d", &num);
>    /*Tell user to enter the card values */
>    while (num>0){
>            printf("Enter card #%d", count);
>            scanf("%c", &value);

The problem is that you're using scanf; Don't Do That.  scanf is Evil[1].

What's happening is that when you tell scanf to ask for a number (just
after you prompt for the number of cards), scanf waits until the
terminal driver (or m{*filter*}equivalent thereof) gives it input, and then
tries to find a number in that input; however, that input will probably
include a newline character (since the terminal driver won't pass on
the input until it fgets one), and scanf doesn't eat that input so it
fgets left in the stdio library's input buffer.  Then, when you tell
scanf to get a character after prompting for the card, it sees that
there's already a character in the input buffer (the newline) and grabs
that instead of waiting for the user to input another character.

What you should do instead is use fgets to get an entire line
(including the newline), and then use sscanf (or strtol and its
friends) to get the number out of that line:
--------
char buf[80];

fgets(buf,sizeof buf,stdin);
if(sscanf(buf,"%d",&num)==0)
{
        /*The user didn't input a valid number, deal with it*/

Quote:
}

--------
You can do something similar and only look at buf[0] instead of asking
scanf for a single character value.

A few other notes on your code:

Your prompts don't end with a newline, which means that if stdout is
line-buffered (which is far from impossible, and not even uncommon) the
prompts won't show up before the program starts waiting for input.  To
avoid confusing the user, you should add:
--------
fflush(stdout);
--------
between printing the prompt and asking for input to make sure the
prompt appears.

You don't initialize total and aces before you use them.  Your compiler
should warn you about that; if it doesn't, it may be that you don't
have the warning level set high enough.  Check your compiler
documentation to find out how to make the compiler give you as many
warnings as possible - paying attention to the warnings it gives you
can help you find a lot of bugs with much less effort than grovelling
over the code to find errors.

You also don't check your input to make sure it's valid.  If this is
for a class (especially an introductory class), you can probably get
away with that (and the people marking your work may even prefer it),
but making sure you're getting the kind of input you're expecting (and
dealing with it sanely if you're not) is a good habit to get into.  (It
can also make debugging easier; checking the card names to make sure
they're valid (for example, complaining if it doesn't match any of the
kinds of card you were expecting) would have turned up that you were
getting newlines as card names, which would have given you a clue that
your bug had something to do with the way input was being handled.)

dave

[1] It is possible to use scanf safely, so it's not quite as Evil as
    gets; however, it's very difficult to use scanf in a safe and
    robust manner, so it's best to avoid it.

--

Potable? Is this something to do with the comp.lang.c drinking game? :-)
                                     --Richard Heathfield in comp.lang.c



Tue, 06 Apr 2004 13:14:45 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Small TSR to load larger program

2. HELP : EXECUTE a .exe program within a c or c++ program

3. .inf files and dsklayt program and makink small setup programs

4. How do I Call a dos program from within my program

5. calling another program from within a program

6. killing a program from within another C-program

7. Calling external programs from within MSVC++ programs on win955

8. Program within a program

9. Run a program from within a MFC program?

10. Starting Another Program From Within a C/C++ Program

11. Executing program within an other program

12. Run a program from within a MFC program?

 

 
Powered by phpBB® Forum Software