samedi 12 décembre 2020

How to call a function using if statement and then print the value inside the function

I'm new to C programming language and when I'm practicing using struct, I cannot print the struct value inside a function using an if statement, it prints the memory address(I think). But when I tried to call the function without an if statement it works just fine. This is the code using an if statement

#include <stdio.h>
typedef struct{
    int age[10];
    int input;
}data;

int inputData(){
    data inputData;
    int i;
    printf("Total Data :");
    scanf("%d", &inputData.input);
    for(i=1;i<=inputData.input;i++){
        printf("Input your age :");
        scanf("%d", &inputData.age[i]);
    }
    return main();
}

void viewData(){
    data viewData;
    int i;
    for(i=1;i<=viewData.input;i++){
        printf("%d", viewData.age[i]);
    }
    
}
int main(){
    int menu;
    printf("Input menu :");
    scanf("%d", &menu);
    if(menu==1){
        inputData();
    }else if(menu==2){
        viewData();
    }
}

And this is the output

Input menu :1
Total Data :2
Input your age :21
Input your age :31
Input menu :2
0853839645327661046072584004611648742404199977042107010648742008541535363276664803040725840046264875200419984104210701064874800854153536327666480368002131327661046

However when I delete the return main() inside the inputData function and change the int main to:

int main(){
    inputData();
    viewData();
}

It works just fine. Output:

Total Data :2
Input your age :21
Input your age :31
2131

Please help me to find a way to make the if statement code works. Any help would be appreciated, Thank you.

Aucun commentaire:

Enregistrer un commentaire