C++ C++;cin.get()结构

C++ C++;cin.get()结构,c++,struct,cin,C++,Struct,Cin,下面是总结: 我正在使用structs创建一个小型音乐库。该库有几个功能,其中一个功能是我应该能够向库中添加新歌曲。我必须使用cin.get()并从那里开始,但每次执行它时,它都会进入无限循环。下面是我的addsong函数的代码。整数i只是一些索引值 struct song { char title[50]; char artist[50]; char minutes[50]; char seconds[50]; char album[50]; }info

下面是总结: 我正在使用structs创建一个小型音乐库。该库有几个功能,其中一个功能是我应该能够向库中添加新歌曲。我必须使用cin.get()并从那里开始,但每次执行它时,它都会进入无限循环。下面是我的addsong函数的代码。整数i只是一些索引值

struct song {
    char title[50];
    char artist[50];
    char minutes[50];
    char seconds[50];
    char album[50];
}info[50];
void new_song(int& i)
int main(){
}
new_song(i);
{
    cin.get(info[i].title,50,'\n');
    cin.ignore(100, '\n');
    cin.get(info[i].artist, 50, '\n');
    cin.ignore(50, '\n');
    cin.get(info[i].minutes, 50, '\n');
    cin.ignore(50, '\n');
    cin.get(info[i].seconds, 50, '\n');
    cin.ignore(50, '\n');
    cin.get(info[i].album, 50, '\n');
    cin.ignore();
}

任何帮助都有帮助。

我可能会这样做,而不是使用cin.get()、C字符串和静态数组:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Song {
   string title;
   string artist;
   string minutes;
   string seconds;
   string album;
};

void add_song_from_stdin(vector<Song> &songs) {
   Song s;
   getline(cin, s.title);
   getline(cin, s.artist);
   getline(cin, s.minutes);
   getline(cin, s.seconds);
   getline(cin, s.album);
   songs.push_back(s);
}

int main() {
   vector<Song> songs;
   add_song_from_stdin(songs);
   Song &song = songs[0];
   cout << "song[0]:" << endl;
   cout << " \"" << song.title << "\"" << endl;
   cout << " \"" << song.artist << "\"" << endl;
   cout << " \"" << song.minutes << "\"" << endl;
   cout << " \"" << song.seconds << "\"" << endl;
   cout << " \"" << song.album << "\"" << endl;
}
#包括
#包括
#包括
使用名称空间std;
结构歌曲{
字符串标题;
弦乐艺术家;
字符串分钟数;
字符串秒;
弦乐专辑;
};
无效从标准添加歌曲(矢量和歌曲){
宋s;
getline(cin,s.title);
getline(cin,s.artist);
getline(cin,s.minutes);
getline(cin,秒);
getline(cin,s.album);
歌曲。向后推;
}
int main(){
矢量歌曲;
从歌曲中添加歌曲;
歌曲&歌曲=歌曲[0];

你会有很多基本语法错误吗?在
无效新歌(int&i)之后没有
。在
新歌(i)之后有额外的
new\u song
没有返回类型或参数类型。你为什么通过引用传递
i
?你从来没有在函数中修改过它。@Barmar我通过引用传递i,因为我需要保留数组的索引你在原型中有
int&i
,但在定义中有
int i
。它们需要相同。我是这样做没有向量。