程序在其中一个案例完成后终止,但对于其他情况则不终止

Program terminates after one of the cases is completed but does not for the others

本文关键字:终止 于其他 情况 其他 在其中 一个 案例 程序      更新时间:2023-10-16

程序在其中一个案例完成后终止,但对于其他情况则不终止。我需要这个程序菜单循环直到用户输入"q",但是,它仅在案例"a"后终止,其他仍然循环。我包含了所有案例和菜单,以防万一错误不在"添加歌曲"案例中。

while (choice != 'q') {
    cout << endl;
    cout << endl;
    cout << play << " PLAYLIST MENU" << endl;
    cout << "a - Add song" << endl;
    cout << "d - Remove song" << endl;
    cout << "c - Change position of song" << endl;
    cout << "s - Output songs by specific artist" << endl;
    cout << "t - Output total time of playlist (in seconds)" << endl;
    cout << "o - Output full playlist" << endl;
    cout << "q - Quit" << endl;
    cout << endl;
    cout << "Choose an option:";
    cin >> choice;
    cout << endl;
    switch (choice) {
    case 'a':
        {
            cout << "ADD SONG" << endl;
            cout << "Enter song's unique ID: ";
            cin >> uniqueID;
            cout << endl << "Enter song's name: ";
            cin.ignore();
            getline(cin, songName);
            cout << endl << "Enter artist's name: ";
            getline(cin, artistName);
            cout << endl << "Enter song's length (in seconds): ";
            cin >> songLength;
            if (headList == NULL) {
                headList = tailList;
                tailList -> SetNext(new PlaylistNode(uniqueID, songName, artistName, songLength));
            } else {
                tailList -> SetNext(new PlaylistNode(uniqueID, songName, artistName, songLength));
                tailList = tailList -> GetNext();
            }
            break;
        }
    case 'd':
        {
            cout << "REMOVE SONG" << endl;
            cout << "Enter song's unique ID: ";
            cin >> uniqueID;
            currList = headList;
            prevList = headList;
            while (currList != NULL) {
                if (currList -> GetID() == uniqueID) {
                    if (currList == headList) {
                        headList = headList -> GetNext();
                        if (currList == tailList) {
                            tailList = NULL;
                        }
                    } else if (tailList == currList) {
                        tailList = prevList;
                        tailList -> SetNext(NULL);
                    } else {
                        prevList -> SetNext(currList -> GetNext());
                    }
                    break;
                }
                prevList = currList;
                currList = currList -> GetNext();
            }
            break;
        }
    case 'c':
        {
            cout << "CHANGE POSITION OF SONG" << endl;
            cout << "Enter song's current position: ";
            cin >> curPos;
            cout << "Enter new position for song: ";
            cin >> newPos;
            break;
        case 's':
                cout << "OUTPUT SONGS BY SPECIFIC ARTIST" << endl;
            cout << "Enter artist's name: ";
            getline(cin, artistName);
            currList = headList;
            position = 1;
            while (currList != NULL) {
                if (currList -> GetArtistName() == artistName) {
                    cout << position << "." << endl;
                    cout << "Unique ID: " << currList -> GetID() << endl;
                    cout << "Song Name: " << currList -> GetSongName() << endl;
                    cout << "Artist Name: " << currList -> GetArtistName() << endl;
                    cout << "Song Length (in seconds): " << currList -> GetSongLength() << endl;
                }
                currList = currList -> GetNext();
                position++;
            }
            break;
        }
    case 't':
        {
            cout << "OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)" << endl;
            int totalLength = 0;
            currList = headList;
            while (currList != NULL) {
                totalLength += currList -> GetSongLength();
                currList = currList -> GetNext();
            }
            cout << "Total time: " << totalLength << " seconds" << endl;
            break;
        }
    case 'o':
        {
            cout << play << " - OUTPUT FULL PLAYLIST" << endl;
            if (headList == NULL) {
                cout << "Playlist is empty" << endl;
                break;
            }
            position = 1;
            currList = headList;
            cout << position << "." << endl;
            cout << "Unique ID: " << currList -> GetID() << endl;
            cout << "Song Name: " << currList -> GetSongName() << endl;
            cout << "Artist Name: " << currList -> GetArtistName() << endl;
            cout << "Song Length (in seconds): " << currList -> GetSongLength() << endl;
            currList = currList -> GetNext();
            position++;
            break;
        }
    }
}
return 0;
}

修复代码缩进后:

您将看到围绕case 's':的代码中的损坏部分与 case 语句的其余部分不一致。那是因为前case中缺少}。我相信这就是你问题的原因。

while (choice != 'q') {
    cout << endl;
    cout << endl;
    cout << play << " PLAYLIST MENU" << endl;
    cout << "a - Add song" << endl;
    cout << "d - Remove song" << endl;
    cout << "c - Change position of song" << endl;
    cout << "s - Output songs by specific artist" << endl;
    cout << "t - Output total time of playlist (in seconds)" << endl;
    cout << "o - Output full playlist" << endl;
    cout << "q - Quit" << endl;
    cout << endl;
    cout << "Choose an option:";
    cin >> choice;
    cout << endl;
    switch (choice) {
    case 'a':
        {
            cout << "ADD SONG" << endl;
            cout << "Enter song's unique ID: ";
            cin >> uniqueID;
            cout << endl << "Enter song's name: ";
            cin.ignore();
            getline(cin, songName);
            cout << endl << "Enter artist's name: ";
            getline(cin, artistName);
            cout << endl << "Enter song's length (in seconds): ";
            cin >> songLength;
            if (headList == NULL) {
                headList = tailList;
                tailList - > SetNext(new PlaylistNode(uniqueID, songName, artistName, songLength));
            } else {
                tailList - > SetNext(new PlaylistNode(uniqueID, songName, artistName, songLength));
                tailList = tailList - > GetNext();
            }
            break;
        }
    case 'd':
        {
            cout << "REMOVE SONG" << endl;
            cout << "Enter song's unique ID: ";
            cin >> uniqueID;
            currList = headList;
            prevList = headList;
            while (currList != NULL) {
                if (currList - > GetID() == uniqueID) {
                    if (currList == headList) {
                        headList = headList - > GetNext();
                        if (currList == tailList) {
                            tailList = NULL;
                        }
                    } else if (tailList == currList) {
                        tailList = prevList;
                        tailList - > SetNext(NULL);
                    } else {
                        prevList - > SetNext(currList - > GetNext());
                    }
                    break;
                }
                prevList = currList;
                currList = currList - > GetNext();
            }
            break;
        }
    case 'c':
        {
            cout << "CHANGE POSITION OF SONG" << endl;
            cout << "Enter song's current position: ";
            cin >> curPos;
            cout << "Enter new position for song: ";
            cin >> newPos;
            break;
        case 's':
                cout << "OUTPUT SONGS BY SPECIFIC ARTIST" << endl;
            cout << "Enter artist's name: ";
            getline(cin, artistName);
            currList = headList;
            position = 1;
            while (currList != NULL) {
                if (currList - > GetArtistName() == artistName) {
                    cout << position << "." << endl;
                    cout << "Unique ID: " << currList - > GetID() << endl;
                    cout << "Song Name: " << currList - > GetSongName() << endl;
                    cout << "Artist Name: " << currList - > GetArtistName() << endl;
                    cout << "Song Length (in seconds): " << currList - > GetSongLength() << endl;
                }
                currList = currList - > GetNext();
                position++;
            }
            break;
        }
    case 't':
        {
            cout << "OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)" << endl;
            int totalLength = 0;
            currList = headList;
            while (currList != NULL) {
                totalLength += currList - > GetSongLength();
                currList = currList - > GetNext();
            }
            cout << "Total time: " << totalLength << " seconds" << endl;
            break;
        }
    case 'o':
        {
            cout << play << " - OUTPUT FULL PLAYLIST" << endl;
            if (headList == NULL) {
                cout << "Playlist is empty" << endl;
                break;
            }
            position = 1;
            currList = headList;
            cout << position << "." << endl;
            cout << "Unique ID: " << currList - > GetID() << endl;
            cout << "Song Name: " << currList - > GetSongName() << endl;
            cout << "Artist Name: " << currList - > GetArtistName() << endl;
            cout << "Song Length (in seconds): " << currList - > GetSongLength() << endl;
            currList = currList - > GetNext();
            position++;
            break;
        }
    }
}
return 0;
}