0

これが私の主な機能です:

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

唯一の問題は、case ステートメントが壊れた後、プログラムが終了することです。理由はわかりましたが、それを修正する方法がわかりません。caseステートメントの後でも、プログラムが毎回繰り返されるようにします。私はただ言うでしょうか:

main(argc, argv);

すべての break ステートメントの前に?

4

2 に答える 2

2

しばらくしてラップする(1) { }

例えば

while(1)
{
  //switch... etc down to the close of the switch
}
于 2010-04-28T01:04:07.227 に答える
-1

break ステートメントに到達すると、switch ステートメントの最後で制御が再開されるため、スイッチ全体を while ループでラップすると、それが繰り返されますが、メインのループから呼び出される別の関数にします。

void GetUserInput() {
  // switch
}

int main()
{
  while (1)
    GetUserInput();
  return 0;
 }
于 2010-04-28T01:08:15.233 に答える