我的程序出了点问题。我想再次从该站点http://www.cplusplus.com/doc/tutorial/structures/进行第二个扩展示例,但是我的代码与该示例不同。程序不显示字符串Enter year;在输入标题之前,它会在稍后执行此操作,并且在输出年份时,仅显示其中的两个数字。这是我的代码
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
using namespace std;
struct movies_t
{
string title;
int year;
} films[3];
void printmovie (movies_t movie);
int main()
{
string mystr;
int n;
do
{
for(n=0;n<3;n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}
cout<<"\n You have entered these movies:\n";
for(n=0;n<3;n++)
printmovie(films[n]);
}
while(GetAsyncKeyState(VK_ESCAPE)==0);
return 0;
}
void printmovie (movies_t movie)
{
cout<<movie.title;
cout << " (" << movie.year << ")\n";
}