简体   繁体   中英

Nested function or calling function in a function

So, I'm not sure how to start, so I'll start with some code:

    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;

First off, I have this, and when I pick a selection, it works to get me to the function I wanted. So when I get to the D4 function, I have this:

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();
}

Now, every time I try to run this, it doesn't like the d4_number(); and says it's an implicit declaration. I'm just trying to let you pick a selection and have it go to the next part in the code, in this case a function that will actually roll a die rather than being a menu like this one. But doing it this way isn't working, and I'm at a loss at what to do.

Edit: Code addition. Here's the full program:

#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();

}

It's obviously very incomplete, but I'm just trying to solve this problem before adding more.

You need to give d4_number() a prototype, before the call, so the compiler know what arguments it takes and what it returns. Otherwise it still links (for historical reasons) and assumes it returns an int.

If you take a closer look at this code

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

you can notice that before d4_number function there is an exit function invoked. In that case d4_number function has never a chance to execute because the whole program stops and exits with 0 as a return code.

Other issue is that d4_number function definition does not accept any arguments, but in statement d4_number(0) there is one extra parameter passed.

Okay, I feel like an idiot. All I had to do was move the declaration:

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

above

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);
}

so that it was declared before calling...I feel really dumb for this, but thanks everyone for the quick responses!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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