编辑:非常感谢(大家!)的评论!我能够在您的指导下修复获胜百分比和不稳定的数组数字。但是,我还没有确定赢或输所需的平均投注次数。我更新了我的代码,现在在下面。

开始原帖:

提前感谢任何阅读本文或提供建议的人!

所以我一直在进行刻板的赌博,直到你赢了,否则编程入门类(class)就全部结束了。代码效果很好,除非我实际添加了正面或反面部分 - 然后我的包含投注数量的数组被搞砸了。当我使用笔记来隐藏实际的抛硬币部分时,数组工作正常。当我用抛硬币来运行它时,betArray 存储了非常不稳定的数字(负整数,以十亿计的整数)。

这是到目前为止的代码:

#include<stdio.h>
#include<stdlib.h> /*Enables use of rand()*/

int main () {

    setvbuf(stdout, NULL, _IONBF, 0); /*Allow me to use repl.it*/

    /*Enter Gambler's name*/

    char gambleName[15] = "";
    printf("Enter gambler's name:\n");
    scanf("%s", gambleName);
    printf("\nWelcome, ");
    printf("%s! \n", gambleName);

    /*Enter Stakes*/
    int availableFunds;
    int goalFunds;
    printf("We'll be betting $1 per bet. Enter your available funds:\n");
    scanf("%d", &availableFunds); /* Saves stakes as availableFunds */
    int seedVal=4;
    srand(seedVal);
    /*Butter the gamblers up*/
    if(availableFunds>=1) {
        printf("%d? Wow, %s - that's a great start!\n",availableFunds, gambleName);
        /*Enter Goal*/
        printf("How much do you want to win today? Enter a value up to 10000 with no commas, decimals or spaces:\n"); /*Saves goal as goalFunds*/
        scanf("%d",&goalFunds);
        /*Recognize ambitious gamblers*/
        if (goalFunds > 10*availableFunds) {
            printf("Wow, ambitious! Let's get started.\n");
        }
        else {
            printf("OK, let's get started!\n");
        }
        printf("\n");
        /*begin gambling problem*/
        int betArray[1000]={0};
        int game = 0;
        int bet=0;
        float wins = 0;
        int losses = 0;
        for (game=0 ; game<1000; game++) {
            if (availableFunds>0 && availableFunds<goalFunds) {
                int toss = rand()%2;
                bet+=1;
                /*losing bet*/
                if (toss == 0) {
                    availableFunds -= 1;
                    losses += 1;
                }
                /*winning bet*/
                else {
                    availableFunds += 1;
                    wins += 1;
                }
                betArray[game+1] = bet;
            }
            else {
                break;
            }
        }
        int sumBet = 0;
        for (game=0;game<1000;game++) {
            sumBet+=betArray[game];
        }
        float betAverage = sumBet/1000;
        float winOutOfGames = wins/1000;
        float winPercent = winOutOfGames*100;
        /*print totals*/
        printf("%d games played with:\n",game);
        printf("%.f goals reached\n",wins);
        printf("%d down-and-out losses.\n",losses);
        printf("You won ~%.1f%% of your games.\n",winPercent);
        printf("On average, it took you %.f bets to win or go broke.\n",betAverage);
        printf("\n");
        printf("Thanks for playing!\n");
        for (game = 1; game <= 50; game++) {
            printf("Bets in game [%d] = %d\n",game,betArray[game]);
        }
    }
    /* Send the broke guys packing*/
    else {
        printf("$%d...? You may need to stop at an ATM... ¯\\_(ツ)_/¯ See you next     time!\n", availableFunds);
    }
    return 0;
}

对不起,如果代码有点乱,我添加了一些东西来尝试并想发送最新版本。

谢谢!

最佳答案

像您一样编译良好而没有任何警告的 C 仍然可能包含许多内存问题,这些问题将导致奇怪且难以重现的行为。要找到它们,请使用内存检查器,例如 Valgrind

用 Valgrind 运行你的游戏会发现一个问题......

...
Bets in game [27] = 28
==27110== Conditional jump or move depends on uninitialised value(s)
==27110==    at 0x1001F08A7: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1002166C0: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x100216952: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001EC381: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001EA21B: printf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x100000CAB: main (test.c:75)
==27110==
==27110== Conditional jump or move depends on uninitialised value(s)
==27110==    at 0x1001F0E90: __ultoa (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001EE364: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1002166C0: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x100216952: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001EC381: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001EA21B: printf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x100000CAB: main (test.c:75)
==27110==
==27110== Syscall param write(buf) points to uninitialised byte(s)
==27110==    at 0x1002F7612: write$NOCANCEL (in /usr/lib/system/libsystem_kernel.dylib)
==27110==    by 0x1001EB1F9: _swrite (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001E3724: __sflush (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x100216966: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001EC381: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x1001EA21B: printf (in /usr/lib/system/libsystem_c.dylib)
==27110==    by 0x100000CAB: main (test.c:75)
==27110==  Address 0x104800e14 is on thread 1's stack
==27110==  in frame #3, created by __xvprintf (???:)
==27110==
Bets in game [28] = 0
...

这些都是堆栈跟踪。您通常从下往上阅读它们以找出问题的根源所在。 Valgrind 检测到您在第 75 行将未初始化的值传递给 printf

test.c 第 74-76 行是这样的:
for (game = 0; game < 50; game++) {
    printf("Bets in game [%d] = %d\n",game,betArray[game]);
}

问题是 betArray 。查看它的初始化位置揭示了问题。
int betArray[1000];

这只会分配内存,但不会初始化它。 betArray 包含该内存位置中发生的任何垃圾。因此,您的不稳定数字。你必须将它初始化为一些东西。下面的 for 循环应该设置每个元素,但它没有。
for (game=0 ; game<1000 ; game++) {
    if (availableFunds>0 && availableFunds<goalFunds) {
        ...blah blah blah...
        betArray[game] = bet;
    }
    else if (availableFunds == 0 || availableFunds >= goalFunds) {
    }
}

但是您的 for 循环只是有时会设置 betArray 初始化。其他时候没有。看起来您打算填写更多代码,但决定尝试一下。所以你只剩下一个部分初始化的 betArray

简单的解决方案是在声明 betArray 后立即初始化它。
int betArray[1000] = {0};

This will initialize all elements to 0 并解决您的不稳定数字问题。

关于c - C 中的 For 循环和数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39864749/

10-12 20:48