-1

「じゃんけん」プログラムをコーディングしています。プレイヤーが試合に負けたり勝ったりすると、プレイヤーのスコアを追跡し、ディスクに保存します。しかし、理由はわかりません。プログラムは実行され、ビルド エラーや警告はありませんが、スコアが変わるたびにプログラムがクラッシュし、255 が返されます。

// Pedra, papel e tesoura

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

void showRes(int cho1);

int scorei;
char score[128];

int main()
{
    int ctemp,done;
    char cho1;
    FILE *sco;

    sco = fopen("score.txt","r");
    if(sco == NULL)
        scorei = 0;
    else
    {
        fread(score,128,1,sco);
        scorei = atoi(score);
    }
    fclose(sco);
    sco = fopen("score.txt","w");

    srand((unsigned)time(NULL));
    done = 0;
    printf("Pedra, Papel e Tesoura\n");
    do
    {
        printf("\n Score: %08d\n",scorei);
        printf("\nDigite '1' para 'Pedra', '2' para 'Papel', '3' para 'Tesoura' ou 'Q' para sair: ");
        cho1 = toupper(getchar());
        fflush(stdin);
        ctemp = cho1;
        switch(ctemp)
        {
            case 49:
            case 50:
            case 51:
                showRes(ctemp);
                break;
            case 81:
                done = 1;
                break;
            default:
                printf("Escolha Inv%clida!\n",160);
                break;
        }
        fwrite(scorei,sizeof(int),1,sco);
        printf("\nsalvo com sucesso\n\n");
    }
    while(!done);
    fclose(sco);

    return(0);
}

void showRes(int cho)
{
    int ia;

    ia = rand() % 3 + 1;

    if(cho == 49)
    {
        printf("Voc%c escolheu Pedra!\n",136);
        switch(ia)
        {
            case 1:
                printf("O Computador escolheu Pedra!\n");
                printf("Empate!");
                break;
            case 2:
                printf("O Computador escolheu Papel!\n");
                printf("Voc%c perdeu!",136);
                scorei -= 100;
                break;
            case 3:
                printf("O Computador escolheu Tesoura!\n");
                printf("Voc%c venceu!\n",136);
                scorei += 100;
                break;
            default:
                break;
        }
    }
    if(cho == 50)
    {
        printf("Voc%c escolheu Papel!\n",136);
        switch(ia)
        {
            case 1:
                printf("O Computador escolheu Pedra!\n");
                printf("Voc%c venceu!\n",136);
                scorei += 100;
                break;
            case 2:
                printf("O Computador escolheu Papel!\n");
                printf("Empate!\n");
                break;
            case 3:
                printf("O Computador escolheu Tesoura!\n");
                printf("Voc%c perdeu!\n",136);
                scorei -= 100;
                break;
            default:
                break;
        }
    }
    if(cho == 51)
    {
        printf("Voc%c escolheu Tesoura!\n",136);
        switch(ia)
        {
            case 1:
                printf("O Computador escolheu pedra!\n");
                printf("Voc%c perdeu!\n",136);
                scorei -= 100;
                break;
            case 2:
                printf("O Computador escolheu Papel!\n");
                printf("Voc%c venceu!\n",136);
                scorei += 100;
                break;
            case 3:
                printf("O Computador escolheu Tesoura!\n");
                printf("Empate!\n");
            default:
                break;
        }
    }
}
4

1 に答える 1

0

必ずしもクラッシュの原因ではありませんが、ファイルにテキストが含まれているかのようにファイルを読み取っていますが、バイナリ値を書き込んでいます。さらに、連続する値がファイルに追加されていますが、1 つしか読み取っていません。

于 2013-10-19T22:30:09.510 に答える