这是一个相当简单的问题。我希望用户输入一个类型。在我的代码中,如果用户想要输入“科幻小说”,它就不会打印任何东西。但是,如果我将“.txt”文件从“科幻小说”改为“科幻小说”,那就会打印出来。所以,我猜它与间距有关。如何使用户也可以输入两个带空格的单词。 :
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
bool compareByTitle(struct Movie m1,struct Movie m2);
void printYear(vector<Movie> &d);
void printGenre(vector<Movie> &d);
void printDirector(vector<Movie> &d);
void printDuration(vector<Movie> &d);
char *capitalize(char* name);
void printAllMovies(vector<Movie> &d);
struct Movie{
string title;
string director;
string genre;
string year;
string duration;
} m;
int main()
{
ifstream inputFile;
string line;
vector<Movie> myMovies;
char choice, again;
inputFile.open("Movie_entries.txt");
while (getline(inputFile, line)) // reads a line from the file
{
//cout << line << endl;
stringstream lineStream(line); // transforms the line into a stream
// get fields from the string stream; fields are separated by comma
getline(lineStream, m.title, ',');
getline(lineStream, m.director, ',');
getline(lineStream, m.genre, ',');
getline(lineStream, m.year, ',');
getline(lineStream, m.duration, ',');
myMovies.push_back(m);
}
inputFile.close();
sort(myMovies.begin(),myMovies.end(), compareByTitle);
cout << "Please choose on of the following:\n\n"
<< "A - Display all movies listed\n"
<< "B - Display movies in a specific year\n"
<< "C - Display movies in a specific genre\n"
<< "D - Display movies from a specific director\n"
<< "E - Display movies in a specific duration\n"
<< "Enter your choice: ";
cin >> choice;
//Input validation
while (toupper(choice) != 'A' && toupper(choice) != 'B' && toupper(choice) != 'C' &&
toupper(choice) != 'D' && toupper(choice) != 'E'){
cout << "Choice is invalid, the choice must be one of the following: 'A', 'B', 'C', 'D' or"
<< " 'E'. \n"
<< "Please try again: ";
cin >> choice;
}
cout << endl << endl;
switch(choice)
{
case 'a':
case 'A': printAllMovies(myMovies); break;
case 'b':
case 'B': printYear(myMovies); break;
case 'c':
case 'C': printGenre(myMovies); break;
case 'd':
case 'D': printDirector(myMovies); break;
case 'e':
case 'E': printDuration(myMovies); break;
}
return 0;
}
bool compareByTitle(Movie m1, Movie m2)
{
return m1.title[0] < m2.title[0];
}
void printAllMovies(vector<Movie> &d)
{
for(int i = 0; i < d.size(); i++)
{
cout << d[i].title << ", " << d[i].director << ", " << d[i].genre << ", "
<< d[i].year << ", " << d[i].duration << endl << endl;
}
}
void printYear(vector<Movie> &d)
{
string whatYear;
cout << "Enter a specific year: ";
cin >> whatYear;
for(int i = 0; i < d.size(); i++)
{
if(whatYear==d[i].year)
{
cout << d[i].title << ", " << d[i].director << ", " << d[i].genre << ", "
<< d[i].year << ", " << d[i].duration << endl << endl;
}
}
}
void printGenre(vector<Movie> &d)
{
string whatGenre;
cout << "Enter a specific genre: ";
getline(cin, whatGenre);
cin >> whatGenre;
for(int i = 0; i < d.size(); i++)
{
if(whatGenre==d[i].genre)
{
cout << d[i].title << ", " << d[i].director << ", " << d[i].genre << ", "
<< d[i].year << ", " << d[i].duration << endl << endl;
}
}
}
void printDirector(vector<Movie> &d)
{
string whatDirector;
cout << "Enter a specific director: ";
getline(cin, whatDirector);
cin >> whatDirector;
for(int i = 0; i < d.size(); i++)
{
if(whatDirector==d[i].director)
{
cout << d[i].title << ", " << d[i].director << ", " << d[i].genre << ", "
<< d[i].year << ", " << d[i].duration << endl << endl;
}
}
}
void printDuration(vector<Movie> &d)
{
string whatDuration;
cout << "Enter a specific duration: ";
cin >> whatDuration;
for(int i = 0; i < d.size(); i++)
{
if(whatDuration==d[i].duration)
{
cout << d[i].title << ", " << d[i].director << ", " << d[i].genre << ", "
<< d[i].year << ", " << d[i].duration << endl << endl;
}
}
}
答案 0 :(得分:0)
在void printGenre(vector<Movie> &d)
getline(cin, whatGenre); << reads in genre from user
cin >> whatGenre; << reads in the genre again, but this time only one word of it.
第二次阅读whatGenre
覆盖了第一个。为什么这样做?嗯......这就是我们需要查看更多代码的原因。
回到main
cin >> choice;
读入单个字符。但要获得该角色,用户必须提供角色并按回车键。输入保留在输入流中,由
解析getline(cin, whatGenre);
并将生成的空字符串放在whatGenre
。
小心在>>
和std::getline
之间来回翻转。最好只使用一个。
解决方案:
删除
cin >> whatGenre;
并替换
cin >> choice;
带
string choicestr;
getline(cin, choicestr);
choice = choicestr[0];
或类似的东西。
你也必须为你的其他一些功能做同样的事情。