Changing struct into class C++ -
for homework assignment, supposed code struct collected information music albums. able easily. second part of assignment turn struct class, , i'm having trouble getting code compile. here 2 codes.
struct code:
#include <iostream> #include <vector> #include <string> using namespace std; struct date { int month; int day; int year; }; struct album { string name; string artist; vector <string> songs; date release; }; void initializealbum(album& a); void initializesongs(vector <string> & songs); void printalbum(album a); int main() { album my_album; initializealbum(my_album); printalbum(my_album); return 0; } void initializealbum(album& a) { cout << "enter album name:" << endl; getline(cin, a.name); cout << "enter artist name:" << endl; getline(cin, a.artist); cout << "enter release month (numeric format: ex. 06/10/1995):" << endl; cin >> a.release.month; cout << "enter release day:" << endl; cin >> a.release.day; cout << "enter release year:" << endl; cin >> a.release.year; initializesongs(a.songs); } void initializesongs(vector <string> & songs) { cout << "how many songs in album?" << endl; int j = 0; cin >> j; string answer; cout << "enter each song name 1 @ time:" << endl; ( int y = 0; y <= j; y++) { getline (cin, answer); songs.push_back(answer); } } void printalbum(album a) { cout << "the album name " << a.name << endl; cout << "the artist name " << a.artist << endl; cout << "the release date " << a.release.day << "/" << a.release.month << "/" << a.release.year << endl; cout << "the songs are:"; (int x = 0; x < a.songs.size(); x++ ) cout << " " << a.songs[x] << endl; }
class code:
#include <iostream> #include <vector> #include <string> using namespace std; struct date { int month; int day; int year; }; class album { public: void printalbum(); void initializealbum(); string name; string artist; vector <string> songs; date release; private: void initializesongs(); }; int main() { album a; a.initializealbum(); a.printalbum(); return 0; } void album::initializealbum( ) { cout << "enter album name:" << endl; getline(cin, name); cout << "enter artist name:" << endl; getline(cin, artist); cout << "enter release month (numeric format: ex. 06/10/1995):" << endl; cin >> release.month; cout << "enter release day:" << endl; cin >> release.day; cout << "enter release year:" << endl; cin >> release.year; initializesongs(songs); } void album::initializesongs() { cout << "how many songs in album?" << endl; int j = 0; cin >> j; string answer; cout << "enter each song name 1 @ time:" << endl; ( int y = 0; y <= j; y++) { getline (cin, answer); songs.push_back(answer); } } void album::printalbum() { cout << "the album name " << name << endl; cout << "the artist name " << artist << endl; cout << "the release date " << release.day << "/" << release.month << "/" << release.year << endl; cout << "the songs are:"; (int x = 0; x < songs.size(); x++ ) cout << " " << songs[x] << endl; }
maybe line error: initializesongs(songs);
in void album::initializealbum()
function?
follow compiler messages.
Comments
Post a Comment