我正在嘗試完成我的介紹給C++類的任務,並且我已經陷入僵局!該節目應該是一個VHS視頻管理器,其中電影存儲在結構中。這些電影是從源文件夾中的.txt文件獲得的,並且包含電影的標題以及年份。在文本文件中讀取數據後,初始輸出應該是這樣的:結構,陣列和白色空間
Initializing Video Collection:
What file should I use? movies.txt
A New Hope (1977)
Empire Strikes Back (1980)
Flight of the Navigator (1986)
Goonies (1985)
Last Crusade (1989)
Raiders of the Lost Ark (1981)
Return of the Jedi (1983)
Temple of Doom (1984)
War Games (1983)
的視頻存儲在看起來像這樣的結構:
struct Video
{
string title; //the name of the video
int year; // the year the movie was released
int stars; // a rating out of five stars - this will be zero until you set it
bool watched; // starts as false until you watch the movie and flip it to true
};
看來,我不知道如何正確讀取我的文件,以便將標題和年份放置在它們各自的陣列位置中。下面是我對這個目的的功能:
void initialize(Video video_array[tapes_max], Video data)
{
ifstream videofile;
videofile.open("movies.txt");
if(videofile.fail())
{
cout << "Could not open states file for reading!" << endl;
exit(1);
}
for(int i = 0; i < tapes_max; i++)
{
getline(videofile, video_array[i].title);
}
videofile.close();
for (int i = 0; i < tapes_max; i++)
{
cout << video_array[i].title << " " << video_array[i].year << endl;
}
cout << endl << endl;
}
這裏是鏈接到已分配給我的PDF,也許你們能比我做的更好有意義嗎?在此先感謝您的幫助!
https://docs.google.com/open?id=0Bwr7dC-H4CCZUkkyUGNTRzRZdk0
到目前爲止,您已將整個輸入行讀取爲一個字符串。現在你需要_parse_那個字符串 - 分成'title'和'year'部分。對?之後,你最好將年份從字符串轉換爲int。 – Vlad
沒有函數可以將一行直接放入結構中,您必須知道它的外觀,然後相應地解析它。 – SinisterMJ
你大約是家庭的1/3。您打開數據文件,讀取行,並將其填入集合中。現在您需要爲電影標題名稱和年份解析每行,並在結構中正確存儲。排名和觀看狀態完全取決於你。 – WhozCraig