繁体   English   中英

嵌套函数或函数中的调用函数

[英]Nested function or calling function in a function

因此,我不确定如何开始,所以我将从一些代码开始:

    int main()
{
    system("cls");
    printf( "1. D4\n" );
    printf( "2. D6\n" );
    printf( "3. D8\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:            
            roll_d4();
            break;

首先,我有这个,当我选择一个选项时,它可以使我进入想要的功能。 因此,当我进入D4功能时,我有以下内容:

void roll_d4() {
system("cls");
printf("How many D4 do you want to roll?\n");
printf("Enter a number 1-20");
printf("\nSelection: ");
scanf("%d", &input);
switch (input){
    case 5:
    exit(0);
    d4_number();
}

现在,每次我尝试运行此命令时,它都不喜欢d4_number(); 并说这是一个隐式声明。 我只是想让您选择一个选项并将其转到代码的下一部分,在这种情况下,该函数实际上将滚动骰子,而不是像这样的菜单。 但是以这种方式这样做是行不通的,而且我对如何做一无所知。

编辑:代码添加。 这是完整的程序:

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include<string.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
char response;
    int sum = 0;
    time_t t;
    int result;

    int die_d4_1 = 0;
    int die_d4_2 = 0;
    int die_d4_3 = 0;
    int die_d4_4 = 0;
    int die_d4_5 = 0;
    int input;

void roll_d4() {
    system("cls");
    printf("How many D4 do you want to roll?\n");
    printf("Enter a number 1-20");
    printf("\nSelection: ");
    scanf("%d", &input);
    switch (input){
        case 5:
        exit(0);
        d4_number(0);
    }
}
void roll_d6()
{
    printf( "How many D6 do you want to roll?" );
}
void roll_d8()
{
    printf( "How many D8 do you want to roll?" );
}

void d4_number(){
    printf("How many d4 do you want to roll?");
}

int main()
{
    system("cls");
    printf( "1. D4\n" );
    printf( "2. D6\n" );
    printf( "3. D8\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:            /* Note the colon, not a semicolon */
            roll_d4();
            break;
        case 2:          
            roll_d6();
            break;
        case 3:         
            roll_d8();
            break;
        case 4:        
            printf( "Thanks for playing!\n" );
            break;
        default:            
            printf( "Bad input, quitting!\n" );
            break;
    }
    getchar();

}

这显然是很不完整的,但是我只是想在添加更多内容之前解决此问题。

您需要在调用之前给d4_number()一个原型,以便编译器知道它需要什么参数以及它返回什么。 否则,它仍然会链接(由于历史原因),并假定它返回一个int值。

如果您仔细看一下这段代码

switch (input){
    case 5:
    exit(0);
    d4_number(0);
}

您会注意到,在d4_number函数之前,有一个exit函数被调用。 在那种情况下, d4_number函数将永远无法执行,因为整个程序都会停止并以0作为返回码退出。

另一个问题是d4_number函数定义不接受任何参数,但是在语句d4_number(0)传递了一个额外的参数。

好吧,我觉得自己是个白痴。 我要做的就是移动声明:

        void d4_number(){
    printf("How many d4 do you want to roll?");
}

以上

void roll_d4() {
system("cls");
printf("How many D4 do you want to roll?\n");
printf("Enter a number 1-20");
printf("\nSelection: ");
scanf("%d", &input);
switch (input){
    case 5:
    d4_number(0);
}

所以在打电话之前就宣布了...我为此感到很愚蠢,但是感谢大家的迅速回复!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM