My program keeps running an infinite loop and skipping the first question. #incl
ID: 669486 • Letter: M
Question
My program keeps running an infinite loop and skipping the first question.
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int MEMORY_SIZE = 50;
const int NUM_SONGS = 25;
const int ADD_SUCCESS = 0;
const int MEM_ERROR = -1;
const int OTHER_ERROR = -2;
int counter = 0; //global counter variable to store the number of songs
// Global constants for menu options //
const int OPTION_1 = 1,
OPTION_2 = 2,
OPTION_3 = 3,
OPTION_4 = 4,
OPTION_5 = 5,
OPTION_6 = 6,
OPTION_QUIT = 7;
// global stucture definition to store song
struct SongInfo
{
string title = "";
string artist = "";
string lyric = "";
int songSize = 0;
};
//FIX ME
// array of structures may not be global
SongInfo song[NUM_SONGS];
int addSong (string newTitle, string newArtist, string newLyric, int songSize);
int removeSong (string title);
int getRemainingMemory();
int showSongList(string title, string artist, string lyric, int songSize);
void showMenu();
void songMetadata(string title);
void playSong(string play);
int main() {
string title;
string artist;
string lyric;
int songSize = 0;
int menuChoice;
showMenu();
cin >> menuChoice;
//do {
//while (menuChoice >= OPTION_1 || menuChoice <= OPTION_6) {
if(menuChoice >= OPTION_1 || menuChoice <= OPTION_6){
if( menuChoice == OPTION_1)
addSong(title, artist, lyric, songSize);
if( menuChoice == OPTION_2)
playSong(title);
}
/* switch (menuChoice) {
case OPTION_1:
addSong(title, artist, lyric, songSize);
break;
case OPTION_2:
removeSong(title);
break;
case OPTION_3:
playSong(title);
break;
case OPTION_4:
songMetadata(title);
break;
case OPTION_5:
showSongList(title,artist,lyric,songSize);
break;
case OPTION_6:
getRemainingMemory();
break;
} */
//}
//} while (menuChoice != OPTION_QUIT);
return 0;
}
// ========================================================
// FUNCTION - showMenu
// This function offers the user the following menu options
// ========================================================
void showMenu() {
cout << " 1. Add a new song to the player (prompt the user for input validation) "
<< " 2. Remove a song from the player (by title) "
<< " 3. Play a song(by title) "
<< " 4. Display a song's metadata (given its title) "
<< " 5. Display the full playlist ordered alphabetically by title "
<< " 6. Display the players remaining storage capacity (in MB) "
<< " 7. Quit Program "
<< " Enter your choice : " ;
}
// ========================================================
// FUNCTION - int getRemainingMemory
// this function stores the remaining memory and displays
// the remaining memory avaliable
// ========================================================
int getRemainingMemory() {
int tempsize = 0;
for(int i = 0; i < counter; i++) {
tempsize += song[i].songSize;
}
int remainingMem = MEMORY_SIZE - tempsize;
return remainingMem;
}
// ========================================================
// FUNCTION - int addSong
// Prompts user to enter a song title, artist song size and line of lyrics.
// Tests that music player is not full and that there is enough storage.
// returns ADD_SUCESS if successful
// returns memory error if not enough memory to add the song
// returns other error for any other error (such as a blank title or artist)
// ========================================================
int addSong(string newTitle, string newArtist, string newLyric, int songSize) {
cout << " Enter the song Title : ";
getline(cin, newTitle);
cout << endl;
cout << " Enter the Artist : ";
getline(cin,newArtist);
cout << endl;
cout << " Enter the Size : ";
cin >> songSize;
cout << " Enter the song lyric :" ;
getline(cin, newLyric);
//testing for not enough memory and too many songs
if (getRemainingMemory() < 0 or counter > NUM_SONGS)
cout << MEM_ERROR;
//checking for blank title or blank artist and song size
else if(newTitle == "" or newArtist == "" or songSize <= 0 or newLyric == "")
cout << OTHER_ERROR;
else {
song[counter].title = newTitle;
song[counter].artist = newArtist;
song[counter].songSize = songSize;
song[counter].lyric = newLyric;
}
counter++;
return ADD_SUCCESS;
}
// ========================================================
// FUNCTION - void showSongList
// prints the current list of songs in order from first to last to standard output
// format - slot #, Title, Artist, size in MB (one song per line)
// print "Empty" for any slots that do not contain a song */
// ========================================================
int showSongList(string, string, string, int) {
for (int i = 0; i < counter; i++) {
cout << "Title" << i+1 << ": " << song[i].title << " ";
cout << setw(8) << "Artist: " << left << song[i].artist << " ";
cout << "Size: " << song[i].songSize << " MB " << endl;
cout << "Lyrics : " << song[i].lyric << endl;
}
return 0;
}
// ========================================================
// FUNCTION - int removeSong
// Prompts user to enter a song title to remove
// returns 0 if successful
// returns OTHER_ERROR if not successful
// ========================================================
int removeSong(string title) {
bool found = false;
int i = 0;
cout << "what song would you like to be removed?" << endl;
cin >> title;
if(title != song[i].title)
cout << "ERROR"<< OTHER_ERROR << endl;
//testing for song found/ not found to remove song
// while(!found && i < counter) {
// if (title == song[i].title) {
cout << "You have removed a song: " << endl;
// found == true;
song[i].title = "";
song[i].artist = "";
song[i].songSize = 0;
//}
//else i++;
//}
return 0;
}
// ========================================================
// FUNCTION - playSong
// Prompts user to enter a song title to play.
// Increments the play count of the song
// Outputs the song name and artist followed by lyrics
// returns OTHER_ERROR if song not found
// ========================================================
void playSong(string play) {
bool found = false;
int i = 0;
int playCount= 0;
cout << " what song would you like to play? " << endl;
cin >> play;
if(play != song[i].title)
cout << "ERROR" << OTHER_ERROR << endl;
//while(!found && i < counter) {
if (play == song[i].title) {
found == true;
cout << "Now playing song: " << endl;
cout << song[i].title << "By " << song[i].artist ;
playCount++;
cout << " play count : " << playCount << endl;
}
//}
}
// ========================================================
// FUNCTION - songMetadata
// Prompts user to enter a song to display
// outputs the title, artist, file size and play count of song
// if song is not found, prints error message
// ========================================================
void songMetadata(string title) {
int i = 0;
cout << " Enter the title of the song to get the size : " ;
cin >> title;
if ( title != song[i].title)
cout << " song title not found" ;
else
cout << " The metadata is : " << song[i].songSize << endl;
}
Explanation / Answer
code modified now and working
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int MEMORY_SIZE = 50;
const int NUM_SONGS = 25;
const int ADD_SUCCESS = 0;
const int MEM_ERROR = -1;
const int OTHER_ERROR = -2;
int counter = 0; //global counter variable to store the number of songs
// Global constants for menu options //
const int OPTION_1 = 1,
OPTION_2 = 2,
OPTION_3 = 3,
OPTION_4 = 4,
OPTION_5 = 5,
OPTION_6 = 6,
OPTION_QUIT = 7;
// global stucture definition to store song
struct SongInfo
{
string title;
string artist;
string lyric ;
int songSize ;
};
//FIX ME
// array of structures may not be global
SongInfo song[NUM_SONGS];
int addSong (string newTitle, string newArtist, string newLyric, int songSize);
int removeSong (string title);
int getRemainingMemory();
int showSongList(string title, string artist, string lyric, int songSize);
void showMenu();
void songMetadata(string title);
void playSong(string play);
int main() {
string title;
string artist;
string lyric;
int songSize = 0;
int menuChoice;
showMenu();
cin >> menuChoice;
//do {
//while (menuChoice >= OPTION_1 || menuChoice <= OPTION_6) {
if(menuChoice >= OPTION_1 || menuChoice <= OPTION_6){
if( menuChoice == OPTION_1)
addSong(title, artist, lyric, songSize);
if( menuChoice == OPTION_2)
playSong(title);
}
/* switch (menuChoice) {
case OPTION_1:
addSong(title, artist, lyric, songSize);
break;
case OPTION_2:
removeSong(title);
break;
case OPTION_3:
playSong(title);
break;
case OPTION_4:
songMetadata(title);
break;
case OPTION_5:
showSongList(title,artist,lyric,songSize);
break;
case OPTION_6:
getRemainingMemory();
break;
} */
//}
//} while (menuChoice != OPTION_QUIT);
return 0;
}
// ========================================================
// FUNCTION - showMenu
// This function offers the user the following menu options
// ========================================================
void showMenu() {
cout << " 1. Add a new song to the player (prompt the user for input validation) "
<< " 2. Remove a song from the player (by title) "
<< " 3. Play a song(by title) "
<< " 4. Display a song's metadata (given its title) "
<< " 5. Display the full playlist ordered alphabetically by title "
<< " 6. Display the players remaining storage capacity (in MB) "
<< " 7. Quit Program "
<< " Enter your choice : " ;
}
// ========================================================
// FUNCTION - int getRemainingMemory
// this function stores the remaining memory and displays
// the remaining memory avaliable
// ========================================================
int getRemainingMemory() {
int tempsize = 0;
for(int i = 0; i < counter; i++) {
tempsize += song[i].songSize;
}
int remainingMem = MEMORY_SIZE - tempsize;
return remainingMem;
}
// ========================================================
// FUNCTION - int addSong
// Prompts user to enter a song title, artist song size and line of lyrics.
// Tests that music player is not full and that there is enough storage.
// returns ADD_SUCESS if successful
// returns memory error if not enough memory to add the song
// returns other error for any other error (such as a blank title or artist)
// ========================================================
int addSong(string newTitle, string newArtist, string newLyric, int songSize) {
cout << " Enter the song Title : ";
getline(cin, newTitle);
cout << endl;
cout << " Enter the Artist : ";
getline(cin,newArtist);
cout << endl;
cout << " Enter the Size : ";
cin >> songSize;
cout << " Enter the song lyric :" ;
getline(cin, newLyric);
//testing for not enough memory and too many songs
if (getRemainingMemory() < 0 || counter > NUM_SONGS)
cout << MEM_ERROR;
//checking for blank title or blank artist and song size
else if(newTitle == "" || newArtist == "" || songSize <= 0 || newLyric == "")
cout << OTHER_ERROR;
else {
song[counter].title = newTitle;
song[counter].artist = newArtist;
song[counter].songSize = songSize;
song[counter].lyric = newLyric;
}
counter++;
return ADD_SUCCESS;
}
// ========================================================
// FUNCTION - void showSongList
// prints the current list of songs in order from first to last to standard output
// format - slot #, Title, Artist, size in MB (one song per line)
// print "Empty" for any slots that do not contain a song */
// ========================================================
int showSongList(string, string, string, int) {
for (int i = 0; i < counter; i++) {
cout << "Title" << i+1 << ": " << song[i].title << " ";
cout << setw(8) << "Artist: " << song[i].artist << " ";
cout << "Size: " << song[i].songSize << " MB " << endl;
cout << "Lyrics : " << song[i].lyric << endl;
}
return 0;
}
// ========================================================
// FUNCTION - int removeSong
// Prompts user to enter a song title to remove
// returns 0 if successful
// returns OTHER_ERROR if not successful
// ========================================================
int removeSong(string title) {
bool found = false;
int i = 0;
cout << "what song would you like to be removed?" << endl;
cin >> title;
if(title != song[i].title)
cout << "ERROR"<< OTHER_ERROR << endl;
//testing for song found/ not found to remove song
// while(!found && i < counter) {
// if (title == song[i].title) {
cout << "You have removed a song: " << endl;
// found == true;
song[i].title = "";
song[i].artist = "";
song[i].songSize = 0;
//}
//else i++;
//}
return 0;
}
// ========================================================
// FUNCTION - playSong
// Prompts user to enter a song title to play.
// Increments the play count of the song
// Outputs the song name and artist followed by lyrics
// returns OTHER_ERROR if song not found
// ========================================================
void playSong(string play) {
bool found = false;
int i = 0;
int playCount= 0;
cout << " what song would you like to play? " << endl;
cin >> play;
if(play != song[i].title)
cout << "ERROR" << OTHER_ERROR << endl;
//while(!found && i < counter) {
if (play == song[i].title) {
found == true;
cout << "Now playing song: " << endl;
cout << song[i].title << "By " << song[i].artist ;
playCount++;
cout << " play count : " << playCount << endl;
}
//}
}
// ========================================================
// FUNCTION - songMetadata
// Prompts user to enter a song to display
// outputs the title, artist, file size and play count of song
// if song is not found, prints error message
// ========================================================
void songMetadata(string title) {
int i = 0;
cout << " Enter the title of the song to get the size : " ;
cin >> title;
if ( title != song[i].title)
cout << " song title not found" ;
else
cout << " The metadata is : " << song[i].songSize << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.