簡體   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