循环中逻辑错误

发布于 12-10 08:52 字数 1353 浏览 1 评论 0原文

尝试向我的 struct Player 添加值,但操作后我的输出是垃圾:

输出如下:

    Name: Warner
    runs: 0
    not out: 0
    how out: |||||||| (symbols)

Player 是一个结构:

Player {

int not_out, innings, runs;

char pname[MAX_NAME];

char how_out[5];

}

这是我的代码:

void scan_stats (Team_t player[]) {

int i, status, runs, turns;
char out;
char string[MAX_PLYR];


FILE *inp;

inp = fopen("teamstats.txt", "r");

do 
{

    fscanf(inp, "%s" "%d" "%*c" "%c", &string, &runs, &out);

    printf("%s    %d     %c\n", string, runs, out); /*They scan perfectly*/

    for (i = 0; i < MAX_PLYR; i++) {

        player[i].innings = 0;

        player[i].runs = 0; 

        player[i].not_out = 0;

        turns = -1;

        if (status = (strcmp(player[i].pname, string)) == 0) {

            player[i].innings = player[i].innings + 1;

            player[i].runs = player[i].runs + runs;

            turns = turns + 1;

            if (out == 'n') {

                player[i].not_out = player[i].not_out + 1;

            }

            else {

                player[i].how_out[turns] = out;

            }
        }
    }
} while (!feof(inp));    /*Printing the values of player at the end of this loop 
                               produces garbage/ incorrectness*/
fclose(inp);
}

Trying to add values to my struct Player but my output for them after operation is junk:

Output looks like:

    Name: Warner
    runs: 0
    not out: 0
    how out: |||||||| (symbols)

Player is a struct:

Player {

int not_out, innings, runs;

char pname[MAX_NAME];

char how_out[5];

}

Here's my code:

void scan_stats (Team_t player[]) {

int i, status, runs, turns;
char out;
char string[MAX_PLYR];


FILE *inp;

inp = fopen("teamstats.txt", "r");

do 
{

    fscanf(inp, "%s" "%d" "%*c" "%c", &string, &runs, &out);

    printf("%s    %d     %c\n", string, runs, out); /*They scan perfectly*/

    for (i = 0; i < MAX_PLYR; i++) {

        player[i].innings = 0;

        player[i].runs = 0; 

        player[i].not_out = 0;

        turns = -1;

        if (status = (strcmp(player[i].pname, string)) == 0) {

            player[i].innings = player[i].innings + 1;

            player[i].runs = player[i].runs + runs;

            turns = turns + 1;

            if (out == 'n') {

                player[i].not_out = player[i].not_out + 1;

            }

            else {

                player[i].how_out[turns] = out;

            }
        }
    }
} while (!feof(inp));    /*Printing the values of player at the end of this loop 
                               produces garbage/ incorrectness*/
fclose(inp);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命2024-12-17 08:52:00

您永远不会在player[i].how_out 中放置结束'\0' - 您只需在其中放置char 即可。然后,您尝试将其打印为字符串。此时,printf 只是打印 if 找到的任何内容,直到它到达随机 NULL,这就是您得到的垃圾。

要解决此问题,请确保 player[i].how_out 在最后一个字符后有一个“\0”(或 NULL) - 并确保缓冲区足够大以容纳该额外的 NULL。

You never place a closing '\0' in player[i].how_out - you just place chars in it. Then, you try to print it as a string. At this point, printf just prints whatever if finds until it gets to a random NULL, and that's the garbage you get.

To fix this, make sure player[i].how_out has a '\0' (or NULL) after the last char - and make sure the buffer is large enough for that extra NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文