Thread: ___Please Help :)___

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    6

    ___Please Help :)___

    Right this is a game of 42 -- similar to blackjack
    i have a 3 problems

    1. it only loops once
    2. when the user doesn't want another number as its close to 42 the game goes on until it hits the results that's fine but then when a user want to play again it goes to the results straight away
    3. when user is asked if they want to play a game of 42 if they press 'n' as in "No" the game continues as normal doesn't exit.

    It will be really helpful if someone could fix it.
    Thanks in advance

    here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    int main(void)
    {
        srand(time(NULL));
        int cardone, cardtwo, cardthree, cardfour, total1, total2;
        
        char user_reply;
        char keep_playing;
        
            keep_playing = 'y';
            user_reply = 'y';
            while((keep_playing)=='y'||(keep_playing=='y'))
            {
                printf("Would you like to play the game of 42? [y or n]");
                scanf("%c",&keep_playing);
                
            if (keep_playing=='n')
            {
                return 0;
            }
            else 
            
            total1=0;
            total2=0;
            
            while (getchar() != '\n');
            printf("\nYour score is 0 press any button to continue");
            while((user_reply=='y')||(user_reply =='y'))
            {
                if (total1>42)
                {
                    printf("\nBusted!");
                    goto label;
                }
                while(getchar()!= '\n');
                cardone=(rand()%21+1);
                cardtwo=(rand()%21+1);
                printf("\nWould you like another number? [y or n]");
                scanf("%c",&user_reply);
                if((user_reply == 'y')|| (user_reply=='y'))
                {
                    total1=(total1+(cardone+cardtwo));
                    printf("\nYour score is %d", total1);
                }
                else if ((user_reply=='n')||(user_reply=='n'))
                
                {
                    printf("\nNow it is computers go.");
                    while((total2<=32))
                    {
                        cardthree=(rand()%21+1);
                        cardfour=(rand()%21+1);
                        
                        total2=total2+(cardthree+cardfour);
                        printf("\nComputers score is %d",total2);
                        if (total2>42)
                        {
                            printf("Computer is bust");
                        }
                    }
                }
            }
            {label: 
            printf("\nResults");
            printf("\nYour score is %d",total1);
            printf("\nComputers score is %d",total2);
            
                if(total1>total2 && total1<=42||total2>42)
                {
                    printf("\nCongratulations! You won!\n");
                    
                }    
                else
                
                printf("\nUnlucky, Computer wins!\n");
        }
            }
        return 0;
        
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is good that you made some effort at indentation, but you really need to be consistent. For example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    int main(void)
    {
        srand(time(NULL));
        int cardone, cardtwo, cardthree, cardfour, total1, total2;
    
        char user_reply;
        char keep_playing;
    
        keep_playing = 'y';
        user_reply = 'y';
        while ((keep_playing) == 'y' || (keep_playing == 'y'))
        {
            printf("Would you like to play the game of 42? [y or n]");
            scanf("%c", &keep_playing);
    
            if (keep_playing == 'n')
            {
                return 0;
            }
            else
                total1 = 0;
            total2 = 0;
    
            while (getchar() != '\n');
    
            printf("\nYour score is 0 press any button to continue");
            while ((user_reply == 'y') || (user_reply == 'y'))
            {
                if (total1 > 42)
                {
                    printf("\nBusted!");
                    goto label;
                }
    
                while (getchar() != '\n');
    
                cardone = (rand() % 21 + 1);
                cardtwo = (rand() % 21 + 1);
                printf("\nWould you like another number? [y or n]");
                scanf("%c", &user_reply);
                if ((user_reply == 'y') || (user_reply == 'y'))
                {
                    total1 = (total1 + (cardone + cardtwo));
                    printf("\nYour score is %d", total1);
                }
                else if ((user_reply == 'n') || (user_reply == 'n'))
                {
                    printf("\nNow it is computers go.");
                    while ((total2 <= 32))
                    {
                        cardthree = (rand() % 21 + 1);
                        cardfour = (rand() % 21 + 1);
    
                        total2 = total2 + (cardthree + cardfour);
                        printf("\nComputers score is %d", total2);
                        if (total2 > 42)
                        {
                            printf("Computer is bust");
                        }
                    }
                }
            }
    
            {
    label:
                printf("\nResults");
                printf("\nYour score is %d", total1);
                printf("\nComputers score is %d", total2);
    
                if (total1 > total2 && total1 <= 42 || total2 > 42)
                {
                    printf("\nCongratulations! You won!\n");
                }
                else
                    printf("\nUnlucky, Computer wins!\n");
            }
        }
    
        return 0;
    }
    Anyway, I suggest that you split up your rather long main function into smaller functions that do one thing and do it well. For example, I suggest that you write a function to ask the "Would you like to play the game of 42?" question and return the answer in lowercase:
    Code:
    char checkIfKeepPlaying(void);
    This way, you can write your loop as:
    Code:
    while (checkIfKeepPlaying() == 'y')
    totally eliminating the "if (keep_playing == 'n')" check.

    You do not need to use goto and a label: a break will suffice. It is also unnecessary to introduce an extra code block, though you could consider moving that piece of code into a separate function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    6
    Thanks for that
    I'll try to make changes now

Popular pages Recent additions subscribe to a feed