Getline输出不起作用?

时间:2014-01-06 15:19:53

标签: c++

我有这个代码,字符串信息不能正常工作。

我有一个getline,其中信息在里面,它保存了一个空洞句子,但它的输出不会超过第一个单词。

示例:

“Hello world!”

我只会告诉我“你好”

以下是代码的一部分:

 void add() {
 string name;
 string faction;
 string classe;
 string race;
 string info;
 ofstream wowdatabase("wowdatabase.txt", ios::app);
 cout << "Add a race or class" << endl;
 cout << "---------------------------------------------------------" << endl;
 cout << "" << endl;
 cout << "Enter the name of the race or class (only small letters!):" << endl;
 cin >> name;

 cout << "Enter Race (Type -, if writen in name section):" << endl;
 cin >> race;

 cout << "Enter Class (Type -, if writen in name section):" << endl;
 cin >> classe;


 cout << "Enter faction (Alliance/Horde):" << endl;
 cin >> faction;
 cin.ignore( numeric_limits<streamsize>::max(), '\n' );

 cout << "Enter the information:" << endl;
 getline(cin, info);

 cout << info;
 system("pause");
 wowdatabase << name << ' ' << faction << ' ' << classe << ' ' << race << ' ' << info      << endl;
 wowdatabase.close();
 system("CLS");
 main();
 }
 void searchname() {


 ifstream charecter("wowdatabase.txt");
 string name;
 string find;
 string faction;
 string classe;
 string race;
 string info;
 int i = 0;


 system("CLS");
 cout << "Search for a race or class" << endl;
 cout << "---------------------------------------------------------" << endl;
 cout << "" << endl;
 cout << "Please enter name:";
 cin >> find;

 while (charecter >> name >> faction >> classe >> race >> info ) {
       if (find == name) {
               system("CLS");
               cout << "Charecter found" << endl;
               cout << "---------------------------------------------------------" << endl;
               cout << "" << endl;
               cout << "Name of race/class: " << name << endl;
               cout << "Race: " << race << endl;
               cout << "Class: " << classe << endl;
               cout << "Faction: " << faction << endl;
               cout << "Info: " << info << endl;
               cout << "" << endl;
               cout << "Press enter to return to menu" << endl;
               system("pause");
               system("CLS");
               main();
       }
}

1 个答案:

答案 0 :(得分:0)

您在多个功能中使用变量信息。例如,在函数searchname()中,将其输入为

while (charecter >> name >> faction >> classe >> race >> info )

在此功能中,您不使用getline。此外,您的代码无效,因为您可能无法在C ++中递归调用main。