【问题标题】:Using Struct for a mp3 player simulation C++ (does not need to play mp3's only display lists)使用 Struct 进行 mp3 播放器模拟 C++(不需要播放 mp3 的唯一显示列表)
【发布时间】:2017-05-03 00:58:50
【问题描述】:

我需要创建一个假的 mp3 播放器,它的菜单屏幕有六个选项((1)添加歌曲。(2)按标题排序。(3)按艺术家排序。(4)按长度排序。(5 ) 打印您的播放列表。(6) 退出) 在我开始按艺术家、长度等对歌曲进行排序之前,我需要能够使用结构输入歌曲。我从未使用过 struct 并且不确定它们如何与向量一起使用,但到目前为止我的代码如下。我有一个包含歌曲标题、艺术家和长度的结构,我相信我使用向量正确输入了信息,但是在尝试时收到错误 expected primary-expression before ' cout 结构。我不知道我的结构是否正常工作,因为我看不到它,因此非常感谢任何帮助显示结构中的歌曲或修复我的代码(如果它不正确)。

#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;
}
}

【问题讨论】:

  • 你的for(int i=0;i&lt;playlist.size();i++)循环体是空的!
  • cout&lt;&lt;SONG&lt;&lt;endl; 不能工作,因为SONG(顺便说一下,不要使用全大写的名字,它们通常用于宏)是一个结构体,也就是一个数据类型。举个例子:走double length;这一行。其中,double 是类型,length 是变量名。如果要打印,必须使用cout &lt;&lt; length;,而不是cout &lt;&lt; double;。在您的情况下,在 displayPlaylist 中,您必须遍历 playlist 的元素,并且对于每个您必须 cout 结构的每个成员。
  • 请重新缩进你的代码
  • 谢谢你的解释,现在说得通了。

标签: c++ struct


【解决方案1】:

SONG 是用户定义的类型。你不能写像 cout &lt;&lt; SONG &lt;&lt; endl

你可以试试cout &lt;&lt; playlist[i].title &lt;&lt; playlist[i].artist &lt;&lt; playlist[i].length &lt;&lt; endl;

【讨论】:

  • 我的疏忽,刚刚添加:)
  • 谢谢!我已经更改了很多代码,现在只需要弄清楚排序!
【解决方案2】:

谢谢所有帮助过的人。这是我的最终代码。所有方面都有效。

#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');
}

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多