newMovie.rating; cout,c++,database,vector,C++,Database,Vector" /> newMovie.rating; cout,c++,database,vector,C++,Database,Vector" />

C++;循环错误,我的教授可以';不明白 我目前正在进行C++编程类,我正在研究一个项目,我必须创建一个相当简单的电影数据库。我的代码基本上是按预期工作的,但在某些情况下,它会导致主菜单无限循环,我不知道为什么。我把这个带给我的老师,他也无法解释。他给了我一个解决办法,但我想知道是否有人能找到问题的原因。完整代码如下: #include <cstdlib> #include <iostream> #include <vector> #include <string> using namespace std; struct MovieType { string title; string director; int year; int length; string rating; }; MovieType addMovie() { MovieType newMovie; cout << "Movie title :"; getline(cin, newMovie.title); cout << "Director :"; getline(cin, newMovie.director); cout << "Year :"; cin >> newMovie.year; cout << "Length(in minutes) :"; cin >> newMovie.length; cout << "Rating :"; cin >> newMovie.rating; cout << endl; return newMovie; } void listMovie(MovieType movie) { cout << "______________________________________" << endl; cout << "Title : " << movie.title << endl; cout << "Director : " << movie.director << endl; cout << "Released : " << movie.year << endl; cout << "MPAA Rating : " << movie.rating << endl; cout << "Running time : " << movie.length << " minutes" << endl; cout << "______________________________________" << endl; } void search(vector<MovieType> movieVector) { string strSearch; cout << endl << "Search title: "; getline(cin, strSearch); for (int c = 0; c < movieVector.size(); c++) { if (movieVector.at(c).title == strSearch) listMovie(movieVector.at(c)); } } int main() { bool quit = 0; vector<MovieType> movieVector; while (quit == 0) { char selection = 'f'; cout << "Main Menu:" << endl; cout << "'a' - Add movie" << endl; cout << "'l' - List movies" << endl; cout << "'s' - Search by movie title" << endl; cout << "'q' - Quit" << endl; cout << "Please enter one of the listed commands:"; cin >> selection; cin.ignore(); cout << endl; if (selection == 'a') movieVector.push_back(addMovie()); else if (selection == 'l') { for (int c = 0; c < movieVector.size(); c++) { listMovie(movieVector.at(c)); } } else if (selection == 's') { search(movieVector); } else if (selection == 'q') quit = 1; } return 0; } #包括 #包括 #包括 #包括 使用名称空间std; 结构电影类型 { 字符串标题; 字符串控制器; 国际年; 整数长度; 串级; }; MovieType addMovie(){ 电影类型新电影; cout newMovie.length; cout>newMovie.rating; cout

C++;循环错误,我的教授可以';不明白 我目前正在进行C++编程类,我正在研究一个项目,我必须创建一个相当简单的电影数据库。我的代码基本上是按预期工作的,但在某些情况下,它会导致主菜单无限循环,我不知道为什么。我把这个带给我的老师,他也无法解释。他给了我一个解决办法,但我想知道是否有人能找到问题的原因。完整代码如下: #include <cstdlib> #include <iostream> #include <vector> #include <string> using namespace std; struct MovieType { string title; string director; int year; int length; string rating; }; MovieType addMovie() { MovieType newMovie; cout << "Movie title :"; getline(cin, newMovie.title); cout << "Director :"; getline(cin, newMovie.director); cout << "Year :"; cin >> newMovie.year; cout << "Length(in minutes) :"; cin >> newMovie.length; cout << "Rating :"; cin >> newMovie.rating; cout << endl; return newMovie; } void listMovie(MovieType movie) { cout << "______________________________________" << endl; cout << "Title : " << movie.title << endl; cout << "Director : " << movie.director << endl; cout << "Released : " << movie.year << endl; cout << "MPAA Rating : " << movie.rating << endl; cout << "Running time : " << movie.length << " minutes" << endl; cout << "______________________________________" << endl; } void search(vector<MovieType> movieVector) { string strSearch; cout << endl << "Search title: "; getline(cin, strSearch); for (int c = 0; c < movieVector.size(); c++) { if (movieVector.at(c).title == strSearch) listMovie(movieVector.at(c)); } } int main() { bool quit = 0; vector<MovieType> movieVector; while (quit == 0) { char selection = 'f'; cout << "Main Menu:" << endl; cout << "'a' - Add movie" << endl; cout << "'l' - List movies" << endl; cout << "'s' - Search by movie title" << endl; cout << "'q' - Quit" << endl; cout << "Please enter one of the listed commands:"; cin >> selection; cin.ignore(); cout << endl; if (selection == 'a') movieVector.push_back(addMovie()); else if (selection == 'l') { for (int c = 0; c < movieVector.size(); c++) { listMovie(movieVector.at(c)); } } else if (selection == 's') { search(movieVector); } else if (selection == 'q') quit = 1; } return 0; } #包括 #包括 #包括 #包括 使用名称空间std; 结构电影类型 { 字符串标题; 字符串控制器; 国际年; 整数长度; 串级; }; MovieType addMovie(){ 电影类型新电影; cout newMovie.length; cout>newMovie.rating; cout,c++,database,vector,C++,Database,Vector,cin进入错误状态,其中cin.fail()为真。在此状态下,它将忽略所有输入操作。一种修复方法是清除错误状态,但更好的方法是,仅对cin使用getline操作,而不是格式化输入 例如,代替 cin >> newMovie.year; …做 newMovie.year = stoi( line_from( cin ) ); …其中中的行可以定义为 auto line_from( std::istream& stream ) -> std::string {

cin
进入错误状态,其中
cin.fail()
为真。在此状态下,它将忽略所有输入操作。一种修复方法是清除错误状态,但更好的方法是,仅对
cin
使用
getline
操作,而不是格式化输入

例如,代替

cin >> newMovie.year;
…做

newMovie.year = stoi( line_from( cin ) );
…其中中的
行可以定义为

auto line_from( std::istream& stream )
    -> std::string
{
    std::string result;
    if( not getline( stream, result ) )
    {
        // Throw an exception or call exit(EXIT_FAILURE).0
    }
    return result;
}

免责声明:编译器未触及代码。

cin
进入错误状态,其中
cin.fail()
为真。在此状态下,它只会忽略所有输入操作。一种修复方法是清除错误状态,但更好的方法是只对
cin
使用
getline
操作,而不是格式化输入

例如,代替

cin >> newMovie.year;
…做

newMovie.year = stoi( line_from( cin ) );
…其中
中的
行可以定义为

auto line_from( std::istream& stream )
    -> std::string
{
    std::string result;
    if( not getline( stream, result ) )
    {
        // Throw an exception or call exit(EXIT_FAILURE).0
    }
    return result;
}

免责声明:编译器未触及代码。

谢谢,这很有意义。我确实将它们全部更改为getline,并且它工作了,但现在我知道为什么它工作起来更有意义了。基于此,似乎在大多数情况下我最好使用getline。你会说这是真的吗?以及我如何清除错误状态?谢谢,that是有意义的。我确实把它们都改成了getline,它也起了作用,但现在我知道为什么它起作用了。基于这一点,在大多数情况下,似乎我最好使用getline。你会说这是真的吗?我该如何清除错误状态?