【问题标题】:C function crash in debug (no error given)C 函数在调试中崩溃(没有给出错误)
【发布时间】:2014-03-02 16:05:38
【问题描述】:

你好,还在学习 C,这个程序应该可以工作!但它一直在void cheack(int *guess, int num)崩溃,我只是不知道该怎么做才能修复它。有人有建议吗?谢谢。此外,该程序应该生成一个随机数 1-20,用户应该猜测它,如果五次尝试打印出高或低,直到用户猜对或输了。

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

int ranNumGen();
void Right(int num);  // prompt that you guessed right
void cheack(int* guess, int num);  // if else's to tell you to high to low...
void sorry(int num); // prompt for not guessing it right


int main(void)
{
    // Local declarations 
    int num;
    int guess;





    // Sets variables  
    num = ranNumGen();



    // Prompt
    printf("\nLets play a game");
    printf("\nCan you guess the number I am thinking of?");
    printf("\nI will give you a hint its a number between (1 and 20)");
    printf("\nI am also going to going to give you five tries.");
    printf("\nHave a guess! ");
    printf("\nShows number to win %d\n", num); // shows number to win to check if it works
    scanf_s("%d", &guess);


    // Check function
    cheack(&guess, num);




    // End promts (will be its own function later)
    if (guess == num)
    {
        printf("\nGoodbye\n");
    }
    else
    {
        sorry(num);
    }
    return 0;
}

int ranNumGen()
{
    int range;
    srand(time(NULL));
    range = 20;
    range = (rand() % range + 1);
    return range;
}

void Right(int num)
{
    printf("\n\t*******************************************");
    printf("\n\t* Wow you guessed it! Yep its %d alright. *", num);
    printf("\n\t*    You won't get it next time! :)       *");
    printf("\n\t*******************************************");
}

void cheack(int *guess, int num)
{
    int a;
    a = 0;
    while (a < 4)
    {
        if ((*guess) == num)
        {
            Right(num);
            break;
        }
        if ((*guess) > num)
        {
            printf("\nYour guess is to high.", *guess);
            printf("\nTry again: ");
            scanf_s("%d", &guess);
        }
        if ((*guess) < num)
        {
            printf("\nYour guess is to low.", *guess);
            printf("\nTry again: ");
            scanf_s("%d", &guess);
        }

    }

return;
}

void sorry(int num)
{
    printf("\nSorry buddy, you didn't guess it...");
    printf("\nThe number was %d better luck next time.\n", num);
    return;
}

【问题讨论】:

  • 顺便说一句,void 函数的末尾不需要 return;
  • 谢谢我错过了。你也没有“拥有”。据我所知,void 函数不需要返回。但它的“好习惯”添加一个。我不知道为什么...

标签: c function debugging crash


【解决方案1】:

scanf 需要一个指针,因此这里不需要地址运算符:

  scanf_s("%d", guess);

【讨论】:

  • 天啊,这些小东西。太感谢了。我需要在指针的概念上做得更好。
猜你喜欢
  • 2022-10-21
  • 2012-05-22
  • 2015-05-09
  • 2020-11-15
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多