Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C帮助:MSVC调试器获得一个;读取字符串“”的字符时出错;错误。。。但只是有时候_C_String_Compiler Errors - Fatal编程技术网

C帮助:MSVC调试器获得一个;读取字符串“”的字符时出错;错误。。。但只是有时候

C帮助:MSVC调试器获得一个;读取字符串“”的字符时出错;错误。。。但只是有时候,c,string,compiler-errors,C,String,Compiler Errors,我正在做一个5张牌的扑克游戏,但是在调试器中运行程序时,我一直遇到一个错误 我在调试器中运行了8次这段代码,其中有5次出现错误,但没有更改任何代码。是什么原因导致它只是偶尔不正确地读取字符串?请帮我弄清楚 必要代码: -初始化 char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"}; char *face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Ei

我正在做一个5张牌的扑克游戏,但是在调试器中运行程序时,我一直遇到一个错误

我在调试器中运行了8次这段代码,其中有5次出现错误,但没有更改任何代码。是什么原因导致它只是偶尔不正确地读取字符串?请帮我弄清楚

必要代码:

-初始化

char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char *face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", 
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
Card p1_hand[5] = {{0,0,0}};
-main中的函数调用

print_hand(face, suit, p1_hand);
-头文件//卡结构中的函数

typedef struct card
{
int card_number;
int face_index;
int suit_index;
} Card;

void print_hand (char *face[4], char *suit[13], Card p1_hand[5]);
-功能本身

void print_hand (char *face[], char *suit[], Card p1_hand[5])
{
int i = 0, j = 0, k = 0;

printf("You're hand:\n");
for(i = 0; i < 5; i++)
{
    j = p1_hand[i].face_index;
    k = p1_hand[i].suit_index;
    printf("%d: %5s of %-8s\n", (i+1), face[j], suit[k]);
}
}
poker.h

#ifndef POKER_H
#define POKER_H

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <dos.h>

typedef struct card
{
    int card_number;
    int face_index;
    int suit_index;
} Card;

int print_menu (void);
int menu_choice (int menu_choice);
void press_any_key (void);
void shuffle (int wDeck[4][13]);
void deal(const int wDeck[][13], Card p_hand[5]);
void pop_num_faces (int num_faces[], Card hand[5]);
void pop_num_suit (int num_suit[4], Card hand[5]);
void print_hand (char *face[4], char *suit[13], Card p1_hand[5]);
void check_hand(Card p1_hand[5], int num_suit[], int num_faces[]);

#endif
\ifndef扑克
#定义扑克牌
#包括
#包括
#包括
#包括
#包括
typedef结构卡
{
int卡号;
int-face_指数;
int-suit_指数;
}卡片;
int print_菜单(无效);
int菜单选项(int菜单选项);
void按任意键(void);
无效洗牌(int-wDeck[4][13]);
无效交易(const int wDeck[][13],Card p_hand[5]);
无效pop_num_faces(int num_faces[],牌手[5]);
无效pop_num_套装(int num_套装[4],牌手[5]);
无效打印手(字符*面[4],字符*套装[13],卡片p1手[5]);
无效支票手(卡片p1手[5],整数套[],整数面[]);
#恩迪夫
poker.c

#include "poker.h"

int main (void)
{
//initialize arrays
char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char *face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", 
"Nine", "Ten", "Jack", "Queen", "King"};
int deck[4][13] = {0};
int num_face_player_1[13] = {0}, num_face_dealer[13] = {0};
int num_suit_player_1[4] = {0}, num_suit_dealer[4] = {0};

//Players' hands
Card p1_hand[5] = {{0,0,0}}, dealer_hand[5] = {{0,0,0}};

//Other
int count = 0;
int menu = 0, choice_menu = 0;
int invalid = 0;

srand ((unsigned) time (NULL));

//MENU
do //menu loop
{
    menu = print_menu ();
    choice_menu = 4; //so its not 0;
    choice_menu = menu_choice (menu);
} while (choice_menu ==0);
if (choice_menu == 2) //closes the game
{
    return 0;
}

system ("cls");
shuffle (deck);
deal (deck, p1_hand);

do //checks to make sure the dealer doesnt have any of the same cards
{
    deal (deck, dealer_hand);
    invalid = 0;
    for(count = 0; count < 5; count++)
    {
        if ((dealer_hand[count].card_number == p1_hand[count].card_number) && (dealer_hand[count].face_index == p1_hand[count].face_index) && (dealer_hand[count].suit_index == p1_hand[count].suit_index))
        {
            invalid = 1;
        }
    }
} while (invalid == 1);

for(count = 0; count < 4; count++)
{
    printf("%s\n", *suit[count]);
}
for(count = 0; count < 13; count++)
{
    printf("%s\n", *face[count]);
}
print_hand(face, suit, p1_hand);

//these populate the number of facecards / suits arrays in order to see what hand you got
pop_num_faces(num_face_player_1, p1_hand);
pop_num_faces(num_face_dealer, dealer_hand);
pop_num_suit(num_suit_player_1, p1_hand);
pop_num_suit(num_suit_dealer, dealer_hand);

return 0;
}
#include "poker.h"

//prints menu
int print_menu (void)


//decides what to do with the menu choice
int menu_choice (int menu_choice)
//Shuffles the deck
void shuffle (int wDeck[4][13])
{
int row = 0;
int column = 0;
int card = 0;

for (card = 1; card <= 52; card++)
{
    do
    {
        row = rand () % 4;
        column = rand () % 13;
    } while (wDeck[row][column] != 0);

    wDeck[row][column] = card;
}
}

//deals the deck to the player
void deal(const int wDeck[][13], Card p_hand[5])
{
int row = 0;
int column = 0;
int card = 0;
int index = 0;

for(card = 1; card < 52; card++)
{
    for (row = 0; row <4; row++)
    {
        for (column = 0; column < 13; column++)
        {
            if(wDeck[row][column] == card)
            {
                p_hand[index].card_number = card;
                p_hand[index].face_index = column;
                p_hand[index].suit_index = row;
            }
        }   
    }
    index++;
}
}

//populates num faces
void pop_num_faces (int num_faces[], Card hand[5])
{
int i = 0;

//program keeps populating num_faces with random numbers, so i have to  re-initialize here
    num_faces[0] = 0;
    num_faces[1] = 0;
    num_faces[2] = 0;
    num_faces[3] = 0;
    num_faces[4] = 0;
    num_faces[5] = 0;
    num_faces[6] = 0;
    num_faces[7] = 0;
    num_faces[8] = 0;
    num_faces[9] = 0;
    num_faces[10] = 0;
    num_faces[11] = 0;
    num_faces[12] = 0;

for(i = 0; i < 5; i++)
{
    switch(hand[i].face_index)
    {
    case 0: num_faces[0] ++;
        break;
    case 1: num_faces[1] ++;
        break;
    case 2: num_faces[2] ++;
        break;
    case 3: num_faces[3] ++;
        break;
    case 4: num_faces[4] ++;
        break;
    case 5: num_faces[5] ++;
        break;
    case 6: num_faces[6] ++;
        break;
    case 7: num_faces[7] ++;
        break;
    case 8: num_faces[8] ++;
        break;
    case 9: num_faces[9] ++;
        break;
    case 10: num_faces[10] ++;
        break;
    case 11: num_faces[11] ++;
        break;
    case 12: num_faces[12] ++;
        break;
    }
}
}

//populates num_suit
void pop_num_suit (int num_suit[4], Card hand[5])
{
int i = 0;
num_suit[0] = 0;
num_suit[1] = 0;
num_suit[2] = 0;
num_suit[3] = 0;

for(i = 0; i < 5; i++)
{
    switch(hand[i].suit_index)
    {
    case 0: num_suit[0] ++;
        break;
    case 1: num_suit[1] ++;
        break;
    case 2: num_suit[2] ++;
        break;
    case 3: num_suit[3] ++;
        break;
    }
}
}

void print_hand (char *face[4], char *suit[13], Card p1_hand[5])
{
printf("You're hand:\n");
for(i = 0; i < 5; i++)
{
    j = p1_hand[i].face_index;
    k = p1_hand[i].suit_index;
    printf("--DEBUG--: %d %d %d\n", p1_hand[i].card_number, j, k);
    printf("%d: %5s of %-8s\n", (i+1), face[j], suit[k]);
}
}
#包括“poker.h”
//打印菜单
int print_菜单(无效)
//决定如何处理菜单选项
整数菜单选项(整数菜单选项)
//洗牌
无效洗牌(int-wDeck[4][13])
{
int行=0;
int列=0;
int卡=0;

对于(card=1;card来说,评论有点大,因此答案是CW

void print_hand(char *face[], char *suit[], Card p1_hand[5])
{
    printf("Your hand:\n");
    for (int i = 0; i < 5; i++)
    {
        int j = p1_hand[i].face_index;
        int k = p1_hand[i].suit_index;
        printf("--DEBUG--: %d %d %d\n", p1_hand[i].card_number, j, k);
        printf("%d: %5s of %-8s\n", (i+1), face[j], suit[k]);
    }
}

SSCCE代码 这几乎是一个SSCCE—在Unix上崩溃的代码行数刚刚超过100行,与在Windows上崩溃的代码行数相同,在
print\u hand()
函数中

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

typedef struct card
{
    int card_number;
    int face_index;
    int suit_index;
} Card;

void shuffle(int wDeck[4][13]);
void deal(int wDeck[][13], Card p_hand[5]);
void print_hand(char *face[4], char *suit[13], Card p1_hand[5]);

static void print_deck(int deck[4][13]);

int main (void)
{
    char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    char *face[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King"};
    int deck[4][13] = { { 0 } };
    Card p1_hand[5] = {{0,0,0}};

    srand ((unsigned) time (NULL));

    printf("Shuffle\n");
    shuffle(deck);
    print_deck(deck);

    printf("Deal player\n");
    deal(deck, p1_hand);

    printf("Print player's hand\n");
    print_hand(face, suit, p1_hand);

    return 0;
}

static void print_deck(int deck[4][13])
{
    for (int x = 0; x < 4; x++)
    {
        for (int z = 0; z < 13; z++)
            printf(" %2d", deck[x][z]);
        putchar('\n');
    }
}

void shuffle(int wDeck[4][13])
{
    int row = 0;
    int column = 0;
    int card = 0;

    for (card = 1; card <= 52; card++)
    {
        do
        {
            row = rand () % 4;
            column = rand () % 13;
        } while (wDeck[row][column] != 0);

        wDeck[row][column] = card;
    }
}

void deal(int wDeck[][13], Card p_hand[5])
{
    int row = 0;
    int column = 0;
    int card = 0;
    int index = 0;

    for (card = 1; card < 52; card++)
    {
        for (row = 0; row <4; row++)
        {
            for (column = 0; column < 13; column++)
            {
                if (wDeck[row][column] == card)
                {
                    p_hand[index].card_number = card;
                    p_hand[index].face_index = column;
                    p_hand[index].suit_index = row;
                }
            }
        }
        index++;
    }
}

void print_hand(char *face[4], char *suit[13], Card p1_hand[5])
{
    int i = 0, j = 0, k = 0;

    printf("Your hand:\n");
    for (i = 0; i < 5; i++)
    {
        j = p1_hand[i].face_index;
        k = p1_hand[i].suit_index;
        printf("--DEBUG--: %d %d %d\n", p1_hand[i].card_number, j, k);
        printf("%d: %5s of %-8s\n", (i+1), face[j], suit[k]);
    }
}
#包括
#包括
#包括
typedef结构卡
{
int卡号;
int-face_指数;
int-suit_指数;
}卡片;
无效洗牌(int-wDeck[4][13]);
无效交易(int wDeck[][13],卡片p_手[5]);
无效打印手(字符*面[4],字符*套装[13],卡片p1手[5]);
静态空白打印组(内部组[4][13]);
内部主(空)
{
char*suit[4]={“红心”、“钻石”、“梅花”、“黑桃”};
char*face[13]={“A”、“2”、“3”、“4”、“5”、“6”、“7”、“8”,
“九”、“十”、“杰克”、“女王”、“国王”};
int deck[4][13]={{0};
卡片p1_hand[5]={{0,0,0};
srand((无符号)时间(NULL));
printf(“Shuffle\n”);
洗牌(牌组);
印刷甲板(甲板);
printf(“交易玩家”);
交易(甲板、p1_手);
printf(“打印玩家的手”);
打印手(脸、西装、p1手);
返回0;
}
静态空白打印组(内部组[4][13])
{
对于(int x=0;x<4;x++)
{
对于(intz=0;z<13;z++)
printf(“%2d”,甲板[x][z]);
putchar('\n');
}
}
无效洗牌(int-wDeck[4][13])
{
int行=0;
int列=0;
int卡=0;

对于(card=1;card而言,问题出在
deal()
函数中。下面是它的调试版本:

void deal(int wDeck[][13], Card p_hand[5])
{
    int row = 0;
    int column = 0;
    int card = 0;
    int index = 0;

    for (card = 1; card < 52; card++)
    {
        for (row = 0; row < 4; row++)
        {
            for (column = 0; column < 13; column++)
            {
                if (wDeck[row][column] == card)
                {
                    p_hand[index].card_number = card;
                    p_hand[index].face_index = column;
                    p_hand[index].suit_index = row;
                    printf("Assign %d\n", index);
                }
            }
        }
        index++;
        printf("Index: %d\n", index);
    }
}
应该是:

void pop_num_suit(int num_suit[4], Card hand[5])
{
    int i;
    for (i = 0; i < 4; i++)
        num_suit[i] = 0;
    for (i = 0; i < 5; i++)
        num_suit[hand[i].suit_index]++;
}
void pop\u num\u suit(int num\u suit[4],手牌[5])
{
int i;
对于(i=0;i<4;i++)
num_suit[i]=0;
对于(i=0;i<5;i++)
数字西服[手[i]。西服索引]++;
}

pop_num_faces()中节省的空间
甚至更具戏剧性。

我还忘了提到将它们更改为const char*对问题没有帮助卡片定义在哪里?您还没有展示传递给代码的
p1\u hand
数组是如何初始化的。问题可能是某些形状或形式的未初始化变量。我相信有5个心a的手是没有被初始化的在扑克游戏中与其他玩家进行ar。错误发生在执行过程中,而不是在编译时。我认为问题不在于p1_手…当我调试它的脸[4]和西装[13]数组偶尔会出现读取错误。p1_hand没有使用这些数组中的任何数据,并且读取错误发生在使用任何数据之前。(调试时,错误发生在第一次printf之前),因此问题几乎肯定不在
print_hand()中
函数。也许你应该在函数项中打印数组的地址;它们每次都应该是相同的。但是如果你在调用
print_hand()
的代码中遇到问题,那么你需要显示出来。目前,我认为它更可能出现在调用代码(或调用代码调用的其他代码)中在编译时启用了最大编译器警告,而代码中没有警告,不是吗?这可能意味着指针失控。您可能踩到了
suit
数组中的非指针数据。因为您没有显示
main()
function,我们无能为力。由于您使用的是MSV,您无法轻松地将
valgrind
应用到工作中;这通常会有所帮助。我猜您可能会遇到类似的情况:
char*buffer;fgets(buffer,200,stdin)
在您的代码中,数据正在被读取到随机内存中。这可能没有那么明显,但是……好吧,这是基于非常有限的数据的猜测。如果您不显示代码,您将得不到帮助。非常感谢您帮助我完成此任务。IMGUR这次没有给我下载可读图像的机会。您不能吗y并将代码粘贴到您的问题中?我们不需要打印屏幕;我们只需要C代码(纯文本,合理缩进)。也许您需要了解如何创建SSCCE()。将问题减少到最小程度通常会指出您的错误。(您使用的是VCS-版本控制系统-因此您可以对代码进行更改,并自信地返回到原来的位置,不是吗?)
,这很好-除非您需要稳定性。对于崩溃时的测试,请注释掉
srand()
,以便
void pop_num_suit (int num_suit[4], Card hand[5])
{
    int i = 0;
    num_suit[0] = 0;
    num_suit[1] = 0;
    num_suit[2] = 0;
    num_suit[3] = 0;

    for(i = 0; i < 5; i++)
    {
        switch(hand[i].suit_index)
        {
        case 0: num_suit[0] ++;
            break;
        case 1: num_suit[1] ++;
            break;
        case 2: num_suit[2] ++;
            break;
        case 3: num_suit[3] ++;
            break;
        }
    }
}
void pop_num_suit(int num_suit[4], Card hand[5])
{
    int i;
    for (i = 0; i < 4; i++)
        num_suit[i] = 0;
    for (i = 0; i < 5; i++)
        num_suit[hand[i].suit_index]++;
}