程序在其中一个案例完成后终止,但对于其他情况则不终止
Program terminates after one of the cases is completed but does not for the others
程序在其中一个案例完成后终止,但对于其他情况则不终止。我需要这个程序菜单循环直到用户输入"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;
}
相关文章:
- E/libc++abi:终止于类型为google::protobuf::FatalException的未捕获异常
- 相同的 for 循环适用于其他项目,但不适用于此项目。为什么?
- 依赖于其他模板参数的模板参数
- QT 无法识别 UTF-8 编码,适用于其他所有位置
- 如何根据用户输入"weighted"随机数生成,使某些数字优先于其他数字?
- deadline_timer::wait 是否让位于其他任务
- 依赖于其他静态库的静态库
- C++/VIM相对于其他IDE的怪异EOF行为
- 为什么我的简单C++项目在我可以看到之前就终止于日食?我如何让它停止
- 如何强制转换优先于其他转换
- 如何设置依赖于其他参数的参数默认值
- 如何相对于其他元素过滤矢量元素
- 是否有等效于其他语言中C++的类型特征
- c++:应用于向量的STL算法也适用于其他容器对象
- 将专用基指针强制转换为专用于其他模板参数的派生指针("adding on"专用化)
- 依赖于其他DLL的DLL
- 在OpenMP中屈服于其他线程/任务
- c++使对象依赖于其他对象是一种好的设计
- 依赖于其他 slib 的静态库是否需要它们的实际'code'才能工作?
- 删除std::lock_guard相对于其他堆栈分配对象的顺序/速度