M. Axis
M. Axis

Reputation: 1

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

The program terminates after one of the cases is completed but does not for the others. I need this programs menu to loop until the user enters 'q', however, it terminates after case 'a' only, the others still loop. I included all the cases and the menu just in case the error is not inside the 'Add Song' 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;
}

Upvotes: 0

Views: 85

Answers (1)

clinomaniac
clinomaniac

Reputation: 2218

After fixing the indentation of your code:

You'll see that the broken part in the code around case 's': is not aligned with the rest of the case statements. That's because the } is missing from the previous case. I believe that is the cause of your issue.

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

Upvotes: 1

Related Questions