Im은 앨범 프로젝트에 대한 C++ 연습을하려고합니다. 기본적으로 Duration, Track (길이 개체 + 트랙 제목), Album (아티스트 이름, 앨범 제목 및 Track 개체 컬렉션) 및 AlbumCollection (Album 개체 컬렉션 만)의 네 가지 클래스가 필요합니다. 변수입니다..eof() 메서드가있는 무한 루프
키 보드에서 값이나 지정된 값을 할당하여 모든 클래스를 테스트했으며 모든 클래스가 작동합니다. 그러나, 나는 그들이 txt 파일을 읽을 것을 요청하려고 할 때. 그냥 앨범 collection.app에 반복되어 계속 멈추지 않습니다. 나는 내 문제가 >> 연산자에 있고 내가 failbit에 문제가 있는지 알고있다. 그러나, 나는 정말로 이것을 해결하기 위해 무엇을해야하는지 모른다.
듀레이션 개체 내 >> 연산자
inline std::istream& operator >> (std::istream& is, Duration& f)
{
char c;
int h,m,s;
if (is >> h >> c >> m >> c >> s){
if (c==':'){
f = Duration (h,m,s);
}
else{
is.clear(std::ios_base::failbit);
}
} else{
is.clear(std::ios_base::failbit);
}
return is;
}
이 트랙 개체 내 >> 연산자
istream& operator>>(istream& is, Track& t){
Duration duration;
char trackTitle[256];
char c1;
if (is>>duration>>c1){
is.getline(trackTitle,256);
t = Track(duration,trackTitle);
}
else{
is.clear(ios_base::failbit);
}
return is;
}
이 내를이다 이것은> > 연산자의 클래스
istream& operator>>(istream& is, Album& album){
char artistName[256];
char albumTitle [256];
vector<Track> trackCollection;
Track track;
is.getline(artistName, 256, ':');
is.getline(albumTitle, 256);
while ((is>>track) && (!is.fail())){
trackCollection.push_back(track);
}
album = Album(artistName,albumTitle,trackCollection);
if (is.eof()){
is.clear(ios_base::failbit);
is.ignore(256,'\n');
}
else{
is.clear();
}
return is;
}
이는 AlbumCollection 클래스
std::istream& operator>>(std::istream& is,AlbumCollection& albumCollection){
Album album;
vector<Album>albums;
while (is>>album) {
albumCollection.addAlbum(album);
}
return is;
}
and the format of the input file .txt is:
The Jimi Hendrix Experience: Are you Experienced?
0:03:22 - Foxy Lady
0:03:32 - Highway Chile
Pink Floyd: Dark Side of the Moon
0:01:30 - Speak to Me
0:02:43 - Breathe
이 유가에 좀 도와 주 시겠어요 내 >> 연산자? 내가 해결하기 위해 최선을 다할 않았다하지만 여전히 나는이 작업을 수행 할 수 없습니다 : ((((
당신에게 정말 감사드립니다 (
'while (is >> track)'이면 충분하고'&&! is.fail()'이 필요 없습니다. – chris
코드를 실제 문제로 줄이면 대부분의 사람들은 전체 게시물을 읽지 않고 downvote합니다. –
정말 고마워요. –