无法让我的程序在文本文件中搜索超过1行`

时间:2014-01-06 11:06:21

标签: c++

我试着为学校项目建立一个小型数据库,但我遇到了一些问题。

我的程序工作正常,它可以为我的数据库添加一个新主题,但我的问题是,当我搜索除了我的txt文档的第一行上的其他内容之外,它将无法正常工作。

示例:

如果我添加,toby和sara到列表,它将只能找到toby。

我该怎么办?

P.s:我知道有很多错误的东西,在这段代码中抱歉。

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

void searchname();
void add();
void exit();

int main()
{
    system("color 0E");
    std::cout << "Velkommen til den store database over spillet World og Warcraft" << "\n";
    std::cout << "Tryk venligst på en vilkårlig tast for at fortsaette" << "\n";
    std::cout << "" << "\n";
    std::cin.get();

    cout << "Vaelg venligst et emne (skriv nummer fra emne liste):" << "\n";
    int valg;
    cout << "1. Add" << "\n";
    cout << "2. Shearch" << "\n";
    cout << "3. List of all Races and Classes" << "\n";
    cout << "4. Afslut program" << "\n";
    cin >> valg;
    system ("CLS");


    switch (valg) {
    case 1:
        add();
        break;
    case 2:
        searchname();
        break;
    case 3:

        int a = 0;
        cout << "Classes" << endl;
        cout << "----------------------------------" << endl;
        cout << "Warrior" << endl;
        cout << "Paladin" << endl;
        cout << "Hunter" << endl;
        cout << "Rogue" << endl;
        cout << "Priest" << endl;
        cout << "Death Knight" << endl;
        cout << "Shaman" << endl;
        cout << "Mage" << endl;
        cout << "Warlock" << endl;
        cout << "Monk" << endl;
        cout << "Druid" << endl;
        cout << "" << endl;
        cout << "" << endl;
        cout << "Races" << endl;
        cout << "Alliance       Horde" << endl;
        cout << "----------------------------------" << endl;
        cout << "Pandaren       Pandaren" << endl;
        cout << "Worgen         Goblin" << endl;
        cout << "Draenei            Blood Elf" << endl;
        cout << "Dwarf          Orc" << endl;
        cout << "Gnome          Tauraen" << endl;
        cout << "Human          Troll" << endl;
        cout << "Night Elf      Undead" << endl;
        cout << "" << endl;
        cout << "" << endl;
        cout << "Want to search info? write 1" << endl;
        cout << "Want to return to menu? write 2" << endl;
        cin >> a;


        while (1) {
            if (a == 1) {
                searchname();
                system("pause");
            }
            else {
                system("CLS");
                main();
            }
        }

    }
    system("pause");
    main();
}

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" << endl;
    cin >> faction;

    cout << "Enter the information" << endl;
    cin >> info;

    wowdatabase << name << ' ' << race << ' ' << classe << ' ' << faction << ' ' << 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();




        }

        if (!(find == name )) {
            system("CLS");
            cout << "Charecter found" << endl;
            cout << "---------------------------------------------------------" << endl;
            cout << "" << endl;
            cout << "No charecter was found with that name, sure you got it right?" << endl;
            cout << "" << endl;
            cout << "Press enter to return to menu" << endl;
            system("pause");
            system("CLS");
            main();
        }

    }



    system("pause");
    cin.get();
    main();

}

void search() {

    int valg2;
    system("CLS");

    cout << "1. Search by name" << endl;
    cin >> valg2;

    switch (valg2) {
    case 1:
        searchname();
        break;

    }

}


void exit() {
    system("pause");
}

2 个答案:

答案 0 :(得分:1)

您可以逐个查找并修复错误。如有必要,添加日志记录或使用调试器。

这是一个帮助你的人:

这段代码:

   if (!(find == name )) {
   ...

应该在这段代码之外:

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

你把它放在块中,导致函数调用main(如果第一行不匹配,你为什么要这样做?!)。

此外,你的逻辑在任何地方都非常糟糕。例如:

case 2:
    searchname();
    break;

searchname函数永远不会返回。那么break做什么呢?

答案 1 :(得分:0)

wowdatabase << name << ' ' << race << ' ' << classe << ' ' << faction << ' ' << info << endl;

推送以换行符结尾的单个字符串。

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

以换行符结尾的第一个字符串转到name,第二个转到派系,第三个转到classe,依此类推。

您可以通过以下方式解决此问题:

wowdatabase << name << endl << faction << endl << classe << endl << race << endl << info << endl;