Search code examples
citerationswitch-statementmain-method

Iterate through main function in C?


Here is my main function:

int main(int argc, char **argv)
{
LoadFile();

Node *temp;
char *key;

switch (GetUserInput())
{
case 1:

    temp = malloc(sizeof(Node));

    printf("\nEnter the key of the new node: ");
    scanf("%s", temp->key);

    printf("\nEnter the value of the new node: ");
    scanf("%s", temp->value);

    AddNode(temp);
    free(temp);
    break;
case 2:
    key = malloc(sizeof(char *));
    printf("Enter the key of the node you want to delete: ");
    scanf("%s", key);
    DeleteNode(key);

    free(key);
    break;
case 3:
    PrintAll();
    break;
case 4:
    SaveFile();
    break;
case 5:
    return 0;
    break;
default:
    printf("\nWrong choice!\n");
    break;
}

return 0;
}

The only problem with it is that after any case statement breaks, the program just exits out. I understand why, but I don't know how to fix it. I want the program to repeat itself each time even after the case statements. Would I just say:

main(argc, argv);

before every break statement?


Solution

  • wrap it in a while(1) { }

    eg

    while(1)
    {
      //switch... etc down to the close of the switch
    }