1

i have two problems first on the compile time warning : warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement] MenuFunction fp;

and second i am always end to "Error: buffer overflow. Please try again, entering less data" what i am doing wrong i have no idea , please help me to figure this out.

typedef void (*MenuFunction)(System*);

int main(int argc, char ** argv)
{
    ...
    /* While loop for my menu */ 
    while(1)
    {
        printf("Main Menu\n");
        printf("%s\n", menu[0].text);
        printf("%s\n", menu[1].text);
        printf("%s\n", menu[2].text);
        printf("Select your option (1-3): ");

        MenuFunction fp; 
        fp = getMenuChoice(menu);
        if(fp == NULL){
          fprintf(stderr, "invalid choice\n");
        }
        else{
          (*fp)(&system);
        }
    }
}

/* Function that points to the menu function */
MenuFunction getMenuChoice(MenuItem * menu)
{
    MenuFunction function = NULL;
    char select[50];
    fgets(select, 50, stdin);
    if(select[strlen(select)-1] == '\n')
    {      
        switch(select[0])
        {
            case '1':
                function = menu[0].function;
                break;
            case '2':
                function = menu[1].function;
                break;
            case '3':    
                function = menu[2].function;
                exit(0);
                break;
            default:
                printf("Invalid option\n");
        }
    }
    else
    {
        readRestOfLine();
        printf("Error: buffer overflow. Please try again, entering less data");
    }
    return function;
}
  • 1
    Posting input used and output seen/expected aids in debugging. – chux - Reinstate Monica May 24 '18 at 22:56
  • Do you have other input code in the `...` part? – chux - Reinstate Monica May 24 '18 at 22:58
  • @chuk : "..." upper part of the code related and chain with linked other files too , if i paste that here i need to add more functions here. my main concern is about : fgets(select, 50, stdin); , its null when i am enter something. please help me in this – Rajesh Kummar May 24 '18 at 23:03
  • For question 1, you can move the `MenuFunction fp;` declaration to the top of the while scope like: `while(1) { MenuFunction fp; printf("Main Menu\n"); ... }` See this duplicate question [/iso-c90-forbids-mixed-declarations-and-code-in-c](https://stackoverflow.com/questions/13291353/iso-c90-forbids-mixed-declarations-and-code-in-c). For question 2, I don't see anything wrong. What is your input? – MFisherKDX May 24 '18 at 23:06
  • Invoke the compiler in modern mode, with switch `-std=c11` – M.M May 24 '18 at 23:12
  • @MFisherKDX : Thank you :) , first error solved , now the problem is fgets(selectedInput, strlen(selectedInput), stdin); is not waiting for input , i mean its display nothing when i am trying to output the code – Rajesh Kummar May 24 '18 at 23:15
  • Regarding your second question: you **have to post your input** and the remaining code you left out. There is no way for us to tell what is wrong until you do so. – MFisherKDX May 24 '18 at 23:36
  • `strlen(selectedInput)` only works *after* a successful input. To get the maximum size of the input buffer, use `sizeof(selectedInput)` . – Bo Persson May 25 '18 at 10:58
  • Thank you Everyone @BoPersson solved the issue :) – Rajesh Kummar May 25 '18 at 17:27

0 Answers0