简体   繁体   English

使用Struct为mp3播放器模拟C ++(不需要播放mp3的唯一显示列表)

[英]Using Struct for a mp3 player simulation C++ (does not need to play mp3's only display lists)

I need to create a fake mp3 player that has a menu screen with six options ((1) Add a song.(2) Sort By Title.(3) Sort by artist.(4) Sort by length.(5) Print your playlist. (6) Exit) Before I start working on sorting songs by artist, length, etc I need to be able to enter songs using a struct. 我需要创建一个假的MP3播放器,其菜单屏幕有六个选项((1)添加歌曲。(2)按标题排序。(3)按艺术家排序。(4)按长度排序。(5)打印你的播放列表。(6)退出)在我开始按艺术家,长度等方式对歌曲进行排序之前,我需要能够使用结构输入歌曲。 I have never used struct and am unsure of how they operate with vectors, but my code so far is below. 我从来没有使用struct而且不确定它们如何使用向量,但到目前为止,我的代码如下。 I have a struct that has the song title, artist, and length and I believe I am entering the information correctly using a vector but am getting the error expected primary-expression before '<<' token when trying to cout the struct. 我有一个具有歌曲标题,艺术家和长度的结构,我相信我正在使用向量正确输入信息,但在尝试cout结构时,我在'<<'标记之前得到错误预期的primary-expression I don't know if my struct is working because I can't see it so any help showing the songs in the struct or fixing my code if it just isn't right would be greatly appreciated. 我不知道我的结构是否正常工作,因为我无法看到它所以任何帮助显示结构中的歌曲或修复我的代码,如果它不正确将非常感激。

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
struct SONG{
string title;
string artist;
double length;
};
void addsong(char ans, string x){
vector<SONG>playlist;
cout<<"You have chosen to add a song"<<endl;
int i=0;
do{
playlist.push_back(SONG());
cout<<"Please enter song title: ";
cin>>playlist[i].title;
cout<<"Please enter song artist: ";
cin>>playlist[i].artist;
cout<<"Please enter song length: ";
cin>>playlist[i].length;
cout<<"Enter another? (y/n)"<<endl;
cin>>ans;
i++;
}while(ans!='n');
}
void displayPlaylist(vector<SONG>playlist){

cout<<SONG<<endl;
}

void sort(){
}
int main(){
int i, num;
char ans;
string x;
vector<SONG>playlist;
struct SONG;
for(int i=0;i<playlist.size();i++){

}
cout<<"Hello, Welcome to VSPod.\nWould you like to:"<<endl;
cout<<"(1) Add a song.\n(2) Sort By Title.\n(3) Sort by artist.\n(4) Sort by length.\n(5) Print your playlist.\n(6) Exit."<<endl;
cin>>num;
if(num==1){
    addsong(ans, x);
    displayPlaylist(playlist);
}
else if(num==2){
    sort();
}
else if(num==3){

}
else if(num==4){

}
else if(num==5){

}
else if(num==6){
    return 0;
}
}

SONG is a user defined type. SONG是用户定义的类型。 You cannot write something like cout << SONG << endl 你不能写像cout << SONG << endl这样的东西

Instead, you can try cout << playlist[i].title << playlist[i].artist << playlist[i].length << endl; 相反,你可以试试cout << playlist[i].title << playlist[i].artist << playlist[i].length << endl;

Thank you, everyone who helped. 谢谢大家的帮助。 Here is my final code. 这是我的最终代码。 All aspects work. 各方面都有效。

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
struct song{
string title;
string artist;
double length;
};


void addsong(vector<song>&playlist, char ans, string x){
cout<<"You have chosen to add a song"<<endl;
int i=0;

do{
cin.ignore();
playlist.push_back(song());
cout<<"Please enter song title: ";
getline(cin,playlist[i].title);
cout<<"Please enter song artist: ";
getline(cin,playlist[i].artist);
cout<<"Please enter song length (in minutes): ";
cin>>playlist[i].length;
cout<<"Enter another? (y/n)"<<endl;
cin>>ans;
i++;
}while(ans!='n');
}
void displayPlaylist(vector<song>playlist){
for(int i=0;i<playlist.size();i++){
cout<<"Title: "<<playlist[i].title<<" | Artist: "<<playlist[i].artist<<" | Length: "<<playlist[i].length<<" minute(s)"<<endl;
}
}
bool titlesort(const song &a, const song &b){
    return a.title < b.title;
}
bool artistsort(const song &a, const song &b){
    return a.artist < b.artist;
}
bool lengthsort(const song &a, const song &b){
    return a.length < b.length;
}

int main(){
int i, num;
char ans, ans2;
string x;
vector<song>playlist;
struct song;
double temp;
do{cout<<"Hello, Welcome to Music Player.\nWould you like to:"<<endl;
cout<<"(1) Add a song.\n(2) Sort By Title.\n(3) Sort by artist.\n(4) Sort by length.\n(5) Print your playlist.\n(6) Exit."<<endl;
cin>>num;
if(num==1){
    addsong(playlist, ans, x);
}
else if(num==2){
    sort(playlist.begin(), playlist.end(), titlesort);
}
else if(num==3){
    sort(playlist.begin(), playlist.end(), artistsort);
}
else if(num==4){
    sort(playlist.begin(), playlist.end(), lengthsort);
}
else if(num==5){
    displayPlaylist(playlist);
    cout<<"Do you want to go to the main menu?";
    cin>>ans2;
}
else if(num==6){
return 0;
}
}while(ans2!='n');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM