I wanna do a program about blackjack game, but i am trying of a different way, but i don't know if is possible. The code is compiled but when start the program, it closes:
blackjack.exe has stopped working A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.
#include <stdio.h>
#include <stdlib.h>
#define NUMBER_PLAYERS 6
#define NUMBER_SUITS 4
#define NUMBER_RANKS 13
#define NUMBER_CARDS (NUMBER_SUITS * NUMBER_RANKS)
typedef unsigned int uint;
typedef struct
{
uint suit;
uint rank;
} Card;
typedef struct card
{
Card card;
struct card *next;
} card;
typedef struct match
{
card *list;
struct match *next;
} Match;
typedef struct
{
char *name;
uint wins;
uint losses;
Match *match;
} Player;
void main(void)
{
Player player[NUMBER_PLAYERS];
uint i, j, count = 0;
char *op;
for(i = 0; i < NUMBER_PLAYERS; i++)
{
printf("Name: ");
scanf("%[^\n]s%*c", &player[i].name);
printf("\n\n");
player[i].losses = 0;
player[i].wins = 0;
player[i].match->next = NULL;
player[i].match->list->next = NULL;
}
getchar();
}
Q: Why not just Player player[NUMBER_PLAYERS];
? Why a pointer to an array, instead of just an array?
SUGGESTION: Change to scanf("s%", player[i]->name);
I assume all you want is to input the name, correct?
SUGGESTION: Create one or more "init_xxx)" functions to initialize your structure (including substructures and/or link pointers) as needed.
Q: What is player[i]->name;
supposed to do? Why not just delete that line?
Q: What do you mean by "when start the program, it close: This program has stop working..."? I notice you have a "getchar()" - does it skip right past getchar()" and terminate? Does the behavior change when you change the "scanf()"? Do you get an error message in any case?
Q: What compiler are you using? Are you on Windows, Linux or "something else"? Are you using an IDE (like Eclipse/CDT, or Visual Studio)?
Player *player[NUMBER_PLAYERS]; // it's an array of pointers,
-> Any item in the array needs to point to a Player before you use it
typedef struct match
{
card *list; // it's a pointer
struct match *next;
} Match;
-> card *list is also needs to point to an initialized card
#include <stdio.h>
#include <stdlib.h>
#define NUMBER_PLAYERS 6
typedef unsigned int uint;
typedef struct
{
uint suit;
uint rank;
} Card;
typedef struct card
{
Card card;
struct card *next;
} card;
typedef struct match
{
card *list;
struct match *next;
} Match;
typedef struct
{
char *name;
uint wins;
uint losses;
Match *match;
} Player;
void main(void)
{
Player player[NUMBER_PLAYERS];
card cardList[NUMBER_PLAYERS];
uint i;
for(i = 0; i < NUMBER_PLAYERS; i++)
{
player[i] = NULL;
printf("Name: ");
scanf("%[^\n]s%*c", &player[i].name);
printf("\n\n");
player[i].name;
player[i].losses = 0;
player[i].wins = 0;
player[i].match->next = NULL;
player[i].match.list = &cardList[i];
player[i].match.list->next = NULL;
}
getchar();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.