Programming Problem You are putting together some music for a party. You\'ve arr
ID: 3631686 • Letter: P
Question
Programming ProblemYou are putting together some music for a party.
You've arranged a list of songs in the order in which you want to play them.
However, you would like to minimize the empty space left at the end
(your MP3 player has space for 45 minutes worth of songs).
So you want to figure out the total time for a group of songs and see how well
they fit. Write a C++ program to help you do this. The program should input
a reference number and a time for each song until it encounters a reference
number of 0. The time should be entered as minutes and seconds (two integer values).
The program should echo print the data for each song and the current running
time total. The last data entry (reference number 0) should not be added
to the total time. After all the data has been read, the program should print
a message indicating the remaining time. For example, your output
should be similar to the following:
Enter the song number:
1
Enter the number of minutes:
2
Enter the number of seconds:
3
Song number 1, 2 minutes and 3 seconds
Total time is 2 minutes and 3 seconds.
For the next song,
Enter the song number:
4
Enter the number of minutes:
5
Enter the number of seconds:
6
Song number 4, 5 minutes and 6 seconds
Total time is 7 minutes and 9 seconds.
For the next song,
Enter the song number:
0
There are 37 minutes and 51 seconds of space left.
Use meaningful variable names, proper indentation, and appropriate comments.
Use good prompting messages. The program should discard any invalid data
(for example, negative numbers) and print an error message indicating
the value has been discarded and what is wrong with it.
Explanation / Answer
Hi, I think this follows all the requirements you needed - I'm using classic C++ libraris and general syntax. If that's not how you need it to be written - you can send me a PM to inbox and we can fix that.
Otherwise, the code is pretty self explanatory, I used plenty of comments to explain what happens at each step, so take your time and read through the code a few times, run it with different values until you understand it well. If you still have questions about it , then again, PM me.
I hope this helps, pelase remember to rate :)
P.S. Cramster messes up indentation and any formatting in general, so you might need to redo that on your own after I post it here, sorry.
#include <iostream>
using namespace std;
/* Main exec function*/
void main()
{
/* variables declaration */
/* Song variables */
int songNum;
int songMins;
int songSecs;
/* Variables for Total music time stored and remaining */
int totalMins = 0;
int totalSecs = 0;
int remainMins = 0;
int remainSecs = 0;
cout << "" << endl;
cout << "Hello, I'm the C++ MP3 Player" << endl;
cout << "You can store up to 45 minutes worth of songs!" << endl;
/* Do-While loop for entering a song and its time mm:ss */
/* keeps looping until either total time = 45 mins */
/* OR user enters 0 for song number */
do {
/* Individual inner do-while loop */
/* for each of songNum, songMins, songSecs */
/* to check if value is valid (i.e. positive) */
/* each will continue looping and prompting user */
/* until valid value is entered */
/* otherwise loops only once and moves on */
do {
cout << "Enter the song number:" << endl;
cin >> songNum;
if(songNum < 0)
cout << "Invalid song number! Can't be negative, try again." <<endl;
}while(songNum < 0);
/* Here we check if the songNum entered is 0 */
/* If it not, then we proceed with the rest of the song info */
if(songNum != 0)
{
/* Second inner do-while loop for songMins*/
do {
cout << "Enter the number of minutes:" << endl;
cin >> songMins;
if(songMins < 0)
cout << "Invalid minutes value! Can't be negative, try again." <<endl;
}while(songMins < 0);
/* third inner do-while loop for songSecs*/
do {
cout << "Enter the number of seconds:" << endl;
cin >> songSecs;
if(songSecs < 0)
cout << "Invalid seconds value! Can't be negative, try again." <<endl;
}while(songSecs < 0);
/* Display Song information */
cout << "Song number " << songNum << ", "
<< songMins << " minutes and " << songSecs << " seconds" << endl;
/* Add these minutes and seconds to the Total count */
totalMins = totalMins + songMins;
totalSecs = totalSecs + songSecs;
/* at this point, there is a possibility that total seconds exceeds 60 */
/* in which case we need to add those 60 seconds as 1 min to the minutes */
/* and only leave the remainder in the seconds counter */
if(totalSecs >= 60)
{
totalMins = totalMins + totalSecs/60;
totalSecs = totalSecs % 60;
}
/* Display total time*/
cout << "Total time is " << totalMins << " minutes and " <<
totalSecs << " seconds " << endl;
/* Check if total minutes so far reached 45 min */
/* If not yet, then you ask for next song */
if(totalMins < 45)
{
cout << "For the next song, " << endl;
}
/* Otherwise, once the while() condition is reached */
/* The loop will stop itself */
} /*end of if singNum != 0*/
}while( (songNum != 0) && (totalMins < 45) );
/* If you're here then you've exited the loop */
/* This is where you display the remaining space left */
/* We can convert all the time to seconds for correct subtraction */
/* Then re-compute the minutes from every 60 seconds */
/* Convert totalMins to seconds and add to totalSecs */
/* 45 mintes will turn to 45*60 seconds */
totalSecs = totalSecs + totalMins*60;
remainSecs = ( 45*60 ) - totalSecs;
if(remainSecs >= 60)
{
remainMins = remainMins + remainSecs/60;
remainSecs = remainSecs % 60;
}
cout << "There are " << remainMins << " minutes and " <<
remainSecs << " seconds of space left." << endl;
} /* enf od main function */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.