简体   繁体   中英

Phonebook using double Linked List

The problem is that program runs fine for the first choice but it starts repeating without asking for choice and going into the addNode or SearchNode function

list *newList = new list;

int choice = 0;
while (choice != 3)
{
    printf("What would you like to do?\n");
    printf("1 - Insert something in the phonebook?\n");
    printf("2 - Search something from the phonebook?\n");
    printf("3 - Nothing at all\n");
    printf("Enter 1 through 5: ");
    scanf_s("%d", &choice);


    switch (choice) {
    case 1:
        newList->addNode();
        break;
    case 2:
        newList->searchNode();
        break;
    default:
        printf("\nThank you for using the phonebook\n");
    }
    choice = 0;
}

You set choice to 0 at the end of the loop.
The code then goes to check the value at the top of the loop.
The value at the top of the loop is 0, which is not equal to 3, so the loop continues.

If you still would like to keep 'choice=0;' in the while loop you could use something similar to this:

if(choice!=3) choice=0;

Hope this helps!

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