class MovieData
{
private:
string Title;
string Director;
int Year;
int Time;
public:
MovieData();
void setTitle(string Title);
void setDirector(string Director);
void setYear(int Year);
void setTime(int Time);
string getTitle();
string getDirector();
int getYear();
int getTime();
};
MovieData::MovieData()
{
Title = "";
Director = "";
Year = 0;
Time = 0;
}
void MovieData::setTitle(string title)
{
Title = title;
}
void MovieData::setDirector(string director)
{
Director = director;
}
void MovieData::setYear(int year)
{
Year = year;
}
void MovieData::setTime(int time)
{
Time = time;
}
string MovieData::getTitle()
{
return Title;
}
string MovieData::getDirector()
{
return Director;
}
int MovieData::getYear()
{
return Year;
}
int MovieData::getTime()
{
return Time;
}
void readMovieData(MovieData *, int);
void MovieDatastats(MovieData *, int);
这是我的类,定义和函数原型......它们都在同一个main.cpp中 这是我的身体
int main()
{
int amount = 0;
cout << "How many movies did you watch last month? ";
cin >> amount;
MovieData *pMovie;
pMovie = new MovieData[amount];
cout << "Please enter the information of your Movies!" << endl;
readMovieData(pMovie, amount);
cout << "\nHere are the information with your Movies!" << endl;
MovieDatastats(pMovie, amount);
}
对于之前一直在帮助我的人,我通过原型功能修复了它。我没有(MovieData * pMovie,int const size)作为参数。非常感谢帮助! 我不需要两次调用同一个对象,它们相互冲突。 而现在,这是我的功能,我一直坚持,我不知道为什么......
void readMovieData(MovieData *pMovie, int const size)
{
string title;
string director;
int year;
int time;
for(int i = 0; i < size; i++)
{
cout << "\nPlease enter the Title of the movie: ";
getline(cin,title);
pMovie[i].setTitle(title);
cin.ignore();
cout << "Please enter the Director name of the movie: ";
getline(cin,director);
pMovie[i].setDirector(director);
cin.ignore();
cout << "Please enter the year it was released: ";
cin >> year;
if(year >= 1900 && year <= 2004)
pMovie[i].setYear(year);
else
{
cout << "\nPlease enter a year between 1900 and 2004." << endl;
year = 0;
system("PAUSE");
break;
}
cout << "Please enter the time the movie last: ";
cin >> time;
if(time > 0 && time < 14400)
pMovie[i].setTime(time);
else
{
time = 0;
cout << "\nPlease enter a time between 0 and 14400 in minutes." << endl;
system("PAUSE");
break;
}
}
}
void MovieDatastats(MovieData *pMovie, int const size)
{
float sumTime = 0.0;
float averageTime = 0.0;
for (int i = 0; i < size; i++)
{
sumTime +=pMovie[i].getTime();
}
averageTime = sumTime/size;
cout << "\nYour average time of all your movie is: " << averageTime << endl;
int oldYear = 0;
int count = 0;
int recentYear = 0;
int counter = 0;
oldYear = pMovie[0].getYear();
recentYear = pMovie[0].getYear();
for (int j = 1; j < size; j++)
{
if(pMovie[j].getYear() < oldYear)
{
oldYear = pMovie[j].getYear();
count = j;
}
else if(pMovie[j].getYear() > recentYear)
{
recentYear = pMovie[j].getYear();
counter = j;
}
}
cout << "\nThe oldest Movie you have watched is called..." << endl;
cout << "Title: " << pMovie[count].getTitle() << endl;
cout << "Director: " << pMovie[count].getDirector() << endl;
cout << "Release Year: " << pMovie[count].getYear() << endl;
cout << "\nThe most newest Movie you have watched is called..." << endl;
cout << "Title: " << pMovie[counter].getTitle() << endl;
cout << "Director: " << pMovie[counter].getDirector() << endl;
cout << "Release Year: " << pMovie[counter].getYear() << endl;
}
一旦我运行程序并输入信息,我就可以输入标题和导演的名字,但是当我到达年份时,程序将不再接受任何内容并完成。正如我们从下面的输出中看到的那样,它跳过了标题,并取消了单词的第一个字母。我该如何解决这个问题?
这是我的输出。
上个月你看了多少部电影? 2
请输入您的电影信息!
请输入电影的标题:鲨鱼袭击
请输入影片的导演名称:随机名称
请输入发布年份:
请输入1900年至2004年之间的一年。
按任意键继续。 。
以下是您的电影的信息!
你所有电影的平均时间是:0
你看过的最古老的电影叫做......
名称:
导演:hark Attack发行年份:0
您观看过的最新电影名为......
名称:
导演:hark Attack发行年份:0
进程返回0(0x0)执行时间:19.781秒按任意键 继续
答案 0 :(得分:0)
在cin >> amount;
之后,您在流中仍然有一个新行,因此在调用title
时,新行将分配给getline(cin,title)
,title
将是空。然后使用默认参数调用ignore()
:streamsize n = 1, int_type delim = traits_type::eof()
如果程序未收到EOF,将丢弃您输入的一个字符。您无需在ignore()
之后致电getline()
删除所有cin.ignore();
并在cin.ignore(A_BIG_NUMBER, '\n');
上方添加以下行:getline(cin, title);
,它将有效。
答案 1 :(得分:0)
您正在使用系统后中断(“暂停”)
打破for循环