Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:26:27+00:00 2026-05-25T21:26:27+00:00

Im trying to make a simple game, http://pastebin.com/BxEBB7Z6 , in c. The goal is

  • 0

Im trying to make a simple game, http://pastebin.com/BxEBB7Z6, in c. The goal is to beat the computer by getting as close to 21 as possible by getting random numbers.

For each round the players name and sum is presented, but for some reasons it only works that first time? Something like this:

Player John has sum 0.
Player has sum 9.
Player has sum 11.

And so on.

Why does the the player’s name get showed once, but not any other prints after that? I dont do a reassign somewhere 🙂

I use the function void PrintPlayerSum(struct Player *p) to print it out, it works the first time, but only that.

#include <stdio.h>
#include <stdlib.h>

#include <time.h>

struct Player
{
    char name[256];
    int sum;
};

void PrintPlayerSum(struct Player *p)
{
     printf("Player %s has sum %d\n", p->name, p->sum);
}

void wait ( int seconds )
{
    clock_t endwait;
    endwait = clock () + seconds * CLOCKS_PER_SEC ;
    while (clock() < endwait) {}
}

int main()
{
    struct Player *player = malloc(sizeof(*player));
    strcpy( player->name, "John");
    player->sum = 0;

    while(1)
    {
        PrintPlayerSum(player);

        printf("Do you want another number? (y/n, q for quit) ");
        char ch;

        scanf("%s", &ch);

        if( ch == 'q' )
            break;

        if( ch == 'y' )
        {
            srand(time(NULL));

            int rnd = rand() % 13 + 1;
            player->sum += rnd;

            printf("Player got %d\n", rnd);
        }

        if( ch == 'n' || player->sum > 21)
        {
            if( player->sum > 21 )
            {
                printf("\n*** You lost the game, please try again... ***");
            }
            else
            {
                printf("\nCPU's turn\n");

                int cpusum = 0;

                while( 1 )
                {
                       if( cpusum > 21 )
                       {
                           printf("\n*** CPU lost the game with the score %d, you win! ***", cpusum);
                           break;
                       }

                       if( cpusum > player->sum )
                       {
                           printf("\n*** CPU won the game with the score %d, please try again ***", cpusum);
                           break;
                       }

                       wait(1);
                       srand(time(NULL));
                       int rnd = rand() % 13 + 1;
                       cpusum += rnd;

                       printf("CPU got %d, sum is %d\n", rnd, cpusum);
                }
            }

            break;
        }

        printf("\n\n");
    }

    /* Cleanup ******************/
    free(player);
    /****************************/

    printf("\n\n\n");
    system("PAUSE");
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:26:28+00:00Added an answer on May 25, 2026 at 9:26 pm

    I suspect the problem is your use of scanf. You say you want to read a zero-terminated string, but you stuff it into a single char. The way the variables are laid out on the stack causes the terminating zero-byte to end up as the first char in player->name.

    Try typing “buffer overflow” instead of “y”, and you should get “player uffer overflow go …”.

    If you want to stick with scanf, you want to make sure you pass it a proper string and set a limit on the size of the target buffer. For reading one char, try fgetc.

    Edit:
    The above is of course not quite right… It is a buffer overflow, but it is the pointer of the player struct that is being overwritten. By lucky coincidence you get to a valid address that points to a zero-byte. By typing more, you will most likely get a crash instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm, trying to make a simple 3D game for Android, and I want to
I'm trying to make a simple HTTP post a endpoint with ONLY url arguments.
I have just started trying to make a simple game with XNA 3.1 to
I've been trying to make a little simple game just to test my logics,
So I'm working on a simple iPhone game and am trying to make a
im trying to make a simple game on cocos2d box2d, its basically got two
For quite awhile I have been trying to make a simple game in Java
I am trying to make a simple guess my number game in c++ but
I have a simple question, I'm trying to make an huge game for Windows
i am trying to make a very simple game where i have 7 positions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.