【问题标题】:strcat() concatenating to two stringsstrcat() 连接两个字符串
【发布时间】:2020-08-19 17:30:11
【问题描述】:

我正在尝试将卡片 ex."10" "1" "A" 连接到字符串 "pile"。这是 Blackjack 的一个非常简化的版本,因此它不关心牌的花色,一次发一张牌。

但是当我运行它时,它会将卡片添加到未传递给方法的堆字符串中。

我对这个问题做了一个最小的重现

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

int placeCard(char *pile, char *card)
{
    //removed code for minimal recreation of problem
    //no piles finished
        strcat(pile, card);
        return 0;
}

int main()
{    
    char card[4];

    char pile1[] = "";
    char pile2[] = "";
    char pile3[] = "";
    char pile4[] = "";
    char pile5[] = "";

    char faces[13][4] = {" 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", " 10", " J", " Q", " K", " A"};

    while(1)
    {
        // print current state of game
        printf("Pile (1): %-20s \n",    pile1);
        printf("Pile (2): %-20s \n",    pile2);
        printf("Pile (3): %-20s \n",    pile3);
        printf("Pile (4): %-20s \n",    pile4);
        printf("Pile (5): %-20s \n\n",  pile5);

        //get random card
        int j = rand() % 52;
        //convert to string
        strcpy(card, faces[j/4]);

        printf("Drawn Card: %s\n\n", card);

        printf("Which pile to place card in? ");
        //assume valid input (1-5) for minmal reproduction of error
        int userPileChoice;
        scanf("%d", &userPileChoice);
        switch (userPileChoice)
        {
            case 1:
                placeCard(pile1, card);
                break;
            case 2:
                placeCard(pile2, card);
                break;
            case 3:
                placeCard(pile3, card);
                break;
            case 4:
                placeCard(pile4, card);
                break;
            case 5:
                placeCard(pile5, card);
                break;
            default:
                break;
        }

    }
}

这是输出

Pile (1):                      
Pile (2):                      
Pile (3):                      
Pile (4):                      
Pile (5):                      

Drawn Card:  J

Which pile to place card in? 1
Pile (1):  J                   
Pile (2): J                    
Pile (3):                      
Pile (4):                      
Pile (5):                      

Drawn Card:  7

Which pile to place card in? 

我在想它可能是 while 循环中的开关盒,而 break 以某种方式把它搞砸了,但我尝试研究它并没有发现任何问题。感谢您的帮助。

【问题讨论】:

  • 您需要在定义桩 1 到 5 的地方发布代码
  • 这不足以重现问题,而且您似乎还没有尝试删除不需要的所有内容。请创建一个minimal reproducible example。并将输出作为文本而不是图片发布。
  • memset(pile, 0, sizeof(pile)); 看起来很可疑。 sizeof(pile) 可能不是你想的那样。
  • 明白应该先这样做,我的道歉现在会写,完成后编辑帖子
  • 请在您的问题中提供纯文本输出作为文本。无需提供图形并让人们关注一些链接。

标签: c string while-loop switch-statement strcat


【解决方案1】:

这个:char pile1[] = ""; 会分配一个大小为 1 的数组,只够 NUL 终止符使用,也就是说它本质上是没用的。对代码做如下修改:

#define SIZE 100
int main()
{
    char card[4];

    char pile1[SIZE] = "";
    // Same for the rest

这将解决您的问题。但我建议使用strcat 阅读有关危险的信息。这是一个相关的帖子:strcat Vs strncat - When should which function be used? 它会起作用的

【讨论】:

  • 谢谢,这就是解决方案。我确实想知道当我传递一个大小为 0 的字符串时会发生什么,以及 strcat 如何以不同的方法修改原始字符串旁边的其他字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 2011-08-29
相关资源
最近更新 更多