#include <iostream>
#include <string>
usingnamespace std;
//prototypes
void displaySData(MovieData, MovieData);
struct MovieData
{
string Title;
string Director;
string YearReleased;
string RunTime;
};
int main()
{
MovieData data1;
MovieData data2;
// Get input for struct var 1
cout<<"Enter the title of the movie:\n";
cin>>data1.Title;
cout<<"Enter the director of the movie:\n";
cin>>data1.Director;
// Get input for struct var 2
cout<<"Enter the release date (year) of the movie:\n";
cin>>data2.YearReleased;
cout<<"Enter the run time (minutes) of the movie:\n";
cin>>data2.RunTime;
// Function for display
displaySData(data1, data2);
system("Pause");
return 0;
}
void displaySData(MovieData d1, MovieData d2)
{
cout<<d1.Title<<"\n";
cout<<d1.Director<<"\n";
cout<<d2.YearReleased<<"\n";
cout<<d2.RunTime<<"\n";
}
I modified your code a little bit. Instead of cin, I am using getline(). That way it reads an entire line and not just the first string. Also I am not sure why you using data2 to store year and length, so I removed it completely from the code and swapped the struct with function header. It compiles and runs as expected.
#include <iostream>
#include <string>
usingnamespace std;
//prototypes
struct MovieData
{
string Title;
string Director;
string YearReleased;
string RunTime;
};
void displaySData(MovieData);
int main()
{
MovieData data1;
// Get input for struct var 1
cout << "Enter the title of the movie:\n";
getline(cin, data1.Title);
cout << "Enter the director of the movie:\n";
getline(cin, data1.Director);
cout << "Enter the release date (year) of the movie:\n";
getline(cin, data1.YearReleased);
cout << "Enter the run time (minutes) of the movie:\n";
getline(cin, data1.RunTime);
// Function for display
displaySData(data1);
system("Pause");
return 0;
}
void displaySData(MovieData d1)
{
cout << d1.Title << "\n";
cout << d1.Director << "\n";
cout << d1.YearReleased << "\n";
cout << d1.RunTime << "\n";
}
@ art1986
Yeah, my instructions are to use 2 variables. Also, what is with the getline()? My book would basically tell me to use after each input (after the first):
cin.ignore();
getline(cin, data1.Director);
I literally started learning this chapter 3 hours ago so forgive my ignorance
Lets say the movie title is Harry Potter. When you first cin, it will assign only Harry because it reads until it encounters whitespace, Potter will remain in buffer until next cin. getline() on the other hand will read an entire line.
cin.ignore() will clear out your cin buffer of any left over user input.
cin.ignore(1000, '\n') - will clear out 1000 characters or until it reaches whitespace.