-2

I've been having trouble with coding this, but I think I have most of it. I just can't get the minutes and seconds down. Can anyone help.

Write C++ code that does the following:

•Displays a welcome message with your name in it.

•Prompts for and reads the lengths in minutes and seconds of 12 tracks of an album.

•Stores the length of each track in seconds in an array. (Note: total seconds can be computed by multiplying the minutes by 60 and adding the seconds.)

•Computes and displays the following:

–The shortest track on the album and its length.–The longest track on the album and its length.

–The total running time of the entire album.

–The average length of a track on the album.

•In the output all times should be in the standard minutes and seconds format with a colon in between. This should be done by a void function with the following prototype:

void displayTime(int totalSeconds);

The number of minutes can be calculated using the / and % operators. Note that if the number of seconds is less than 10, this function must display an extra zero after the colon. For example, a track with a total length of 185 seconds should have its length displayed as 3:05.

It's supposed to look like this.

Track 1: 3 25

Track 2: 4 56

All the way to 12.

This is what I have so far.

#include <iostream>
#include <iomanip>

using namespace std;

void displayTime(int totalSeconds);

int main()
{
   const int SIZE = 12;
   float Tracks[SIZE];
   int cnt = 0;
    int TrackHigh;
    int TrackLow;
    int totalSeconds;


    cout << "Welcome to Jalen Keller's Album Length Calculator." << endl;
    cout << "Please enter all track lengths in minutes and seconds separated by a space." << endl;

    while (Tracks[cnt] != -1 && cnt < SIZE)
    {
        cout << "Track " << cnt + 1 << ": ";
        cin >> Tracks[cnt];
        cnt++;

    }

    TrackHigh = Tracks[0];
    for (cnt = 0; cnt < SIZE; cnt++) {


        if (Tracks[cnt] > TrackHigh)
        {
            TrackHigh = Tracks[cnt];
        }
    }
    TrackLow = Tracks[0];
    for (cnt = 0; cnt < SIZE; cnt++)
        if (Tracks[cnt] < TrackHigh)
        {
            TrackHigh = Tracks[cnt];
        }

    double total = 0;
    double average;
    for (int cnt = 0; cnt < SIZE; cnt++)
    {
        total += Tracks[cnt];
        average = (total / SIZE);
    }

    cout << "The shortest track is: " << TrackHigh << endl;
    cout << "The longest track is: " << TrackLow << endl;
    cout << "The average length of a track is: " << average << endl;

    system("pause");
    return 0;
}
rayjk
  • 1
  • 1
  • 1
  • 2
    "I just can't get the minutes and seconds down." Can you show what you have tried? The code you provided does not seem to call diplayTime() nor it shows an attempt to implement that function. The rest of your code seems ok (more or less), but basically you are asking if someone can complete the last piece for you. For this reason your question may be considered off topic here. – slepic Nov 14 '19 at 07:04
  • I’ve tried I just kept deleting them when they didn’t work. I just need a push in the right direction. – rayjk Nov 14 '19 at 07:12
  • I understand, nevertheless you should include at least the implementation which got you the closest. – slepic Nov 14 '19 at 07:16

2 Answers2

0

Your task description actually tells you exactly how you should do it.

minutes and seconds format with a colon in between.

The number of minutes can be calculated using the / and % operators.

Note that if the number of seconds is less than 10, this function must display an extra zero after the colon.

void displayTime(int totalSeconds)
{
  int minutes = totalSeconds / 60;
  int seconds = totalSeconds % 60;

  std::cout << minutes << ':';
  if (seconds < 10) {
    std::cout << '0';
  }
  std::cout << seconds;
}

Btw, using namespace std is generally considered a bad practice. Why is "using namespace std;" considered bad practice?

EDIT: Ah and this:

while (Tracks[cnt] != -1 && cnt < SIZE)

I'm not exactly sure why Tracks[cnt] should be different to -1 as it is uninitialized at that moment. I think cnt < SIZE is all you need. And btw it might be better to write it is as a for(cnt=0;cnt<SIZE;++cnt). And also I think you dont have to do 4 fors, you can do all the work in the first for. And derive average out of the for as it only makes sense once your total is final (once the for ends).

Community
  • 1
  • 1
slepic
  • 641
  • 4
  • 11
0

Here is the classical copy-paste error

if (Tracks[cnt] < TrackHigh)
{
    TrackHigh = Tracks[cnt];
}

Must be TrackLow instead of TrackHigh.

if (Tracks[cnt] < TrackHigh)
{
    TrackLow = Tracks[cnt];
}

The array is uninitialized in the declaration, thus the condition Tracks[cnt] != -1 in the first loop is undefined behavior. If -1 is a user signal to stop filling an album, then the loop must be

for (cnt = 0; cnt < SIZE; ++cnt)
{
    cout << "Track " << cnt + 1 << ": ";
    cin >> Tracks[cnt];
    if (Tracks[cnt] == -1)
    {
        AlbumSize = cnt + 1;
        break;
    }
}

AlbumSize must be declared above the first loop and used in other loops instead of SIZE.

273K
  • 29,503
  • 10
  • 41
  • 64