Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何让用户合并一个空间_C++ - Fatal编程技术网

C++ 如何让用户合并一个空间

C++ 如何让用户合并一个空间,c++,C++,这是一个相当简单的问题。我希望用户输入一个流派。在我的代码中,如果用户想要输入“科幻小说”,它不会打印任何东西。但是如果我把.txt文件从“科幻小说”改成“科幻小说”一个字,它就会打印出来。所以,我猜这和间距有关。如何使用户也可以输入两个带空格的单词: #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <algorithm&

这是一个相当简单的问题。我希望用户输入一个流派。在我的代码中,如果用户想要输入“科幻小说”,它不会打印任何东西。但是如果我把.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;
        }
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
bool comparebytle(结构电影m1、结构电影m2);
一年(矢量和d);
void printgree(矢量与d);
行政总监(向量及发展);;
无效打印持续时间(矢量和d);
字符*大写(字符*名称);
void printalmovies(矢量和d);
结构电影{
字符串标题;
字符串控制器;
弦乐体裁;
弦年;
字符串持续时间;
}m;
int main()
{
ifstream输入文件;
弦线;
矢量电影;
再一次,选择;
打开(“Movie_entries.txt”);
while(getline(inputFile,line))//从文件中读取一行
{
//在<代码>无效打印体裁(矢量和d)

读入一个字符。但要获得该字符,用户必须提供该字符并按enter键。输入流中保留的enter键由

getline(cin, whatGenre);
并将生成的空字符串放入
whatGenre

小心在
>
std::getline
之间来回翻转。最好只使用一个

解决方案:

除去

cin >> whatGenre;
替换

cin >> choice;

或者类似的东西


你也必须为你的一些其他函数做同样的事情。

信息不足。低挂水果:你读过<代码>类型> <代码>使用<代码> >代码> @ USER481301?我刚刚发布了我的整个代码以澄清更多的问题:考虑替换<代码>(int i=0;i < d siz();i++)<代码> > <代码> >(电影和M:D)并相应地调整回路主体。
cin >> whatGenre;
cin >> choice;
string choicestr;
getline(cin, choicestr);
choice = choicestr[0];