Question

In: Computer Science

Write code to read a list of song names and durations from input. Input first receives...

Write code to read a list of song names and durations from input. Input first receives a song name, then the duration of that song. Input example: Time 424 Money 383 quit.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Song {
public:
void SetNameAndDuration(string songName, int songDuration) {
name = songName;
duration = songDuration;
}
void PrintSong() const {
cout << name << " - " << duration << endl;
}
string GetName() const { return name; }
int GetDuration() const { return duration; }

private:
string name;
int duration;
};

int main() {
vector<Song> songPlaylist;
Song currentSong;
string currentName;
int currentDuration;
unsigned int i;

cin >> currentName;
while (currentName != "quit") {

/* Your code goes here */

}

for (i = 0; i < songPlaylist.size(); ++i) {
currentSong = songPlaylist.at(i);
currentSong.PrintSong();
}

return 0;
}

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Song {
public:
    void SetNameAndDuration(string songName, int songDuration) {
        name = songName;
        duration = songDuration;
    }
    void PrintSong() const {
        cout << name << " - " << duration << endl;
    }
    string GetName() const { return name; }
    int GetDuration() const { return duration; }

private:
    string name;
    int duration;
};

int main() {
    vector<Song> songPlaylist;
    Song currentSong;
    string currentName;
    int currentDuration;
    unsigned int i;

    cin >> currentName;
    while (currentName != "quit") {
        cin >> currentDuration;
        currentSong.SetNameAndDuration(currentName, currentDuration);
        songPlaylist.push_back(currentSong);
        cin >> currentName;
    }

    for (i = 0; i < songPlaylist.size(); ++i) {
        currentSong = songPlaylist.at(i);
        currentSong.PrintSong();
    }
    return 0;
}

Related Solutions

C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
Write a program that first gets a list of integers from input. The input begins with...
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain fewer than 20 integers. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). For coding simplicity, follow each output integer by a space,...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
Write a function that takes a person’s first and last names as input and (a) uses...
Write a function that takes a person’s first and last names as input and (a) uses lists to return a list of the common letters in the first and last names (the intersection). (b) uses sets to return a set that is the intersection of the characters in the first and last names. (c) uses sets to return the set that is the symmetric difference between the first and last names. please write in python program
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
PHTHON 3 Write code that generates a list of the names of all the senders of...
PHTHON 3 Write code that generates a list of the names of all the senders of the messages in fb_data. Store it in a variable called senders. fb_data = { "data": [ { "id": "2253324325325123432madeup", "from": { "id": "23243152523425madeup", "name": "Jane Smith" }, "to": { "data": [ { "name": "Your Facebook Group", "id": "432542543635453245madeup" } ] }, "message": "Understand. Extract. Repeat.", "type": "status", "created_time": "2014-10-03T02:07:19+0000", "updated_time": "2014-10-03T02:07:19+0000" }, { "id": "2359739457974250975madeup", "from": { "id": "4363684063madeup", "name": "John Smythe" }, "to":...
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a program that first gets a list of 5 integers from input. Then, get another...
Write a program that first gets a list of 5 integers from input. Then, get another value from the input, and output all integers less than or equal to that value. Ex: If the input is 50 60 140 200 75 100, the output is: 50 60 75 For coding simplicity, follow every output value by a space, including the last one. Then, output a newline. Such functionality is common on sites like Amazon, where a user can filter results....
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT