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