I can't seem to figure out why the first letter of my strings is getting cut off when it is displayed. I tried doing things like cin.ignore and cin.clear in different places and nothing worked. I tried looking it up and someone said something about a carriage return but I don't really know what that is.
#include <iostream>
#include <string>
#include <climits>
usingnamespace std;
// Write the structure declaration to hold the movie information.
struct MovieInfo
{
string movieName;
string director;
int releaseYear;
MovieInfo(string m = "unknown", string d = "unknown")
{
movieName = m;
director = d;
if (releaseYear < 1889 || releaseYear > 2025)
{
cout << "You have entered an invalid year." << endl;
}
}
};
int main(void)
{
string favMovie;
string favMdirector;
int yearReleased;
// Define the structure variable here.
MovieInfo movie;
cout << "Enter the following information about your "
<< " favorite movie.\n" << "Name: ";
// Write a statement here that lets the user enter a movie name.
// Store it in the appropriate structure member.
// cin.clear();
// cin.ignore(INT_MAX, '\n');
cin.get();
getline(cin, favMovie);
movie.movieName = favMovie;
cin.clear();
cout << "Director: ";
// Write a statement here that lets the user enter the director's
// name. Store it in the appropriate structure member.
// cin.clear();
// cin.ignore(INT_MAX, '\n');
cin.get();
getline(cin, favMdirector);
movie.director = favMdirector;
cout << "Year of Release: ";
// Write a statement here that lets the user enter the movie
// release year. Store it in the appropriate structure member.
cin >> yearReleased;
movie.releaseYear = yearReleased;
cout << "\nHere is information on your favorite movie:\n";
// Write statements here that display the information
// just entered into the structure variable.
cout << "My favorite movie is " << movie.movieName << ", the director was " << movie.director
<< " and it was released in " << movie.releaseYear << "." << endl;
return 0;
}
I would advise you to move the if statement inside the constructor to your main. The reason behind that is because constructors, when called, will print automatically whatever is inside(meaning it will always bypass your if statement). You use constructors(usually) to initialize private members with default values; not to print things.
Thank you for the great advice! It makes a lot of sense and I didn't even realize it haha But is there any chance you know the solution to my original question?
#include <iostream>
#include <string>
#include <climits>
usingnamespace std;
struct MovieInfo
{
string movieName;
string director;
int releaseYear;
MovieInfo(string m = " ", string d = " ")
{
movieName = m;
director = d;
}
};
int main(void)
{
string favMovie;
string favMdirector;
int yearReleased;
MovieInfo movie;
cout << "Enter the following information about your "
<< "favorite movie.\n" << "\nName: ";
getline(cin, favMovie);
movie.movieName = favMovie;
cout << "Director: ";
getline(cin, favMdirector);
movie.director = favMdirector;
cout << "Year of Release: ";
cin >> yearReleased;
if (yearReleased < 1889 || yearReleased > 2025)
{
cout << "You have entered an invalid year." << endl;
cin >> yearReleased;
}
movie.releaseYear = yearReleased;
cout << "\nHere is information on your favorite movie:\n";
cout << "My favorite movie is " << movie.movieName << ", the director was " << movie.director
<< " and it was released in " << movie.releaseYear << "." << endl;
return 0;
}