Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have written this C++ program. When you enter artist, song and song length it

ID: 3740516 • Letter: I

Question

I have written this C++ program. When you enter artist, song and song length it asks if I want to enter another, so i enter y for yes and I enter the same information. Now when it asks if i want to add another song i hit no and select display play list option. It shows the playlist but the name of the artists from the play list is missing

please help.

all information for the items entered shoud be displayed.

// week 3 with menu.cpp : Defines the entry point for the console application.

//Richard Wooten

//Week 4 program added loop and started arrays

//

#include "stdafx.h"

# include <iostream>

# include <string>

# include <cstdlib>

using namespace std;

struct music

{

string title;

string artist;

float len;

};

int main()

{

music arr_str[20];

string artist1, songTitle1, artist2, songTitle2;

float totalLength = 0;

char again;

int ch, i = -1;

// get data

while (1)

{

cout << "1. Add songs" << endl;

cout << "2. Display songs" << endl;

cout << "3. Exit" << endl;

cout << "Enter your choice : ";

cin >> ch;

switch (ch)

{

case 1:

while (1)

{

++i;

cin.clear();

cout << "Insert the artist name of the first song: ";

getline(cin, arr_str[i].artist);

cin.ignore(1000, ' ');

cout << "Insert the title of the first song: ";

getline(cin, arr_str[i].title);

cout << "Insert the length of the first song: ";

cin >> arr_str[i].len;

totalLength = totalLength + arr_str[i].len;

cout << " Would you like to add another? y/n : ";

cin >> again;

if (again == 'n' || again == 'N')

break;

}

break;

case 2:

if (i == -1)

{

cout << "No songs found" << endl;

}

else

{

for (int k = 0; k <= i; k++)

{

cout << " Artist: " << arr_str[k].artist << " ";

cout << " Title: " << arr_str[k].title << " ";

cout << " Length: " << arr_str[k].len << " ";

}

cout << "The playlist has a total length of " << totalLength << " minutes. ";

}

break;

case 3:

cout << " *** System will now exit ***" << endl;

exit(0);

}

}

system("pause");

return 0;

}

Explanation / Answer

Hi

I have modified the code and highlighted the code changes below

# include <iostream>

# include <string>

# include <cstdlib>

using namespace std;

struct music

{

string title;

string artist;

float len;

};

int main()

{

music arr_str[20];

string artist1, songTitle1, artist2, songTitle2;

float totalLength = 0;

char again;

int ch, i = -1;

// get data

while (1)

{

cout << "1. Add songs" << endl;

cout << "2. Display songs" << endl;

cout << "3. Exit" << endl;

cout << "Enter your choice : ";

cin >> ch;

switch (ch)

{

case 1:

while (1)

{

++i;

cin.clear();

cin.ignore(1000, ' ');

cout << "Insert the artist name of the first song: ";

getline(cin, arr_str[i].artist);

cout << "Insert the title of the first song: ";

getline(cin, arr_str[i].title);

cout << "Insert the length of the first song: ";

cin >> arr_str[i].len;

totalLength = totalLength + arr_str[i].len;

cout << " Would you like to add another? y/n : ";

cin >> again;

if (again == 'n' || again == 'N')

break;

}

break;

case 2:

if (i == -1)

{

cout << "No songs found" << endl;

}

else

{

for (int k = 0; k <= i; k++)

{

cout << " Artist: " << arr_str[k].artist << " ";

cout << " Title: " << arr_str[k].title << " ";

cout << " Length: " << arr_str[k].len << " ";

}

cout << "The playlist has a total length of " << totalLength << " minutes. ";

}

break;

case 3:

cout << " *** System will now exit ***" << endl;

exit(0);

}

}

//system("pause");

return 0;

}

Output: