Why my program is printing incorrect values, not the supplied values? Any help would be much appreciated.
Data
title1=q, year1=1, title2=w, year2=2
Code
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int getnumber ();
struct movies_t {
string title;
int year;
};
void printmovie (movies_t films);
int main ()
{
int z=getnumber ();
cout << "You will have to provide data for " << z << " films.\n";
//movies_t films [z];
vector<movies_t> films(z);
string mystr;
int n;
for (n=0; n<z; n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cin.ignore();
cout << "Enter year: ";
getline (cin,mystr);
cin.ignore();
stringstream(mystr) >> films[n].year;
}
cout << "\nYou have entered these movies:\n";
for (n=0; n<z; n++)
printmovie (films[n]);
return 0;
}
void printmovie (movies_t films)
{
movies_t * pmovie;
pmovie = &films;
cout << pmovie->title;
cout << " (" << films.year << ")\n";
}
int getnumber ()
{
int i;
cout << "Please enter number of films: ";
cin >> i;
return i;
}
Output (obtained; incorrect)
Please enter number of films: 2
You will have to provide data for 2 films.
Enter title: q
Enter year: 1
Enter title: w
Enter year: 2
You have entered these movies:
(0)
(0)
Output (desired)
Please enter number of films: 2
You will have to provide data for 2 films.
Enter title: q
Enter year: 1
Enter title: w
Enter year: 2
You have entered these movies:
q (1)
w (2)