The problem is not that you aen't able to print out strings. The real problem is that the strings you are printing are simply blank to begin with.
When main()
calls getinfo()
, a new moviedata
object is created, populated, and then destroyed. When main()
then calls showresult()
, a new moviedata
object is created, which is not populated, but is printed out. The strings in that second object have been default-constructed and not had any values assigned to them.
What you need to do is persist a single moviedata
object between getinfo()
and showresult()
so you don't lose your data, for example:
#include <iostream>
#include <string>
using namespace std;
struct moviedata
{
string title;
string director;
int year_released;
double run_time;
};
void getinfo(moviedata&);
void showresult(const moviedata&);
int main()
{
moviedata m;
getinfo(m);
showresult(m);
return 0;
}
void getinfo(moviedata &m)
{
cout << "What is title of movie?" << endl;
cin >> m.title;
cout << "Who is the director of " << m.title << " ?" << endl;
cin >> m.director;
cout << "What year was it released?" << endl;
cin >> m.year_released;
cout << "What is the run time of " << m.title << " ?" << endl;
cin >> m.run_time;
}
void showresult(const moviedata &m)
{
cout << "You selected " << m.title << " directed by " << m.director;
cout << " which was released in " << m.year_released << " and is " << m.run_time << " hours long" << endl;
}
You can then take that a step further by moving getinfo()
and showresult()
into moviedata
itself, for example:
#include <iostream>
#include <string>
using namespace std;
struct moviedata
{
string title;
string director;
int year_released;
double run_time;
void getinfo();
void showresult() const;
};
int main()
{
moviedata m;
m.getinfo();
m.showresult();
return 0;
}
void moviedata::getinfo()
{
cout << "What is title of movie?" << endl;
cin >> title;
cout << "Who is the director of " << title << " ?" << endl;
cin >> director;
cout << "What year was it released?" << endl;
cin >> year_released;
cout << "What is the run time of " << title << " ?" << endl;
cin >> run_time;
}
void moviedata::showresult() const
{
cout << "You selected " << title << " directed by " << director;
cout << " which was released in " << year_released << " and is " << run_time << " hours long" << endl;
}