2014-01-06 29 views
-1

即時嘗試爲學校項目製作一個小型數據庫,但我遇到了索姆問題。無法讓我的程序在文本文件中搜索超過1行?

我的程序工作正常,它可以添加一個新的主題到我的數據庫,但我的問題是,當我搜索的任何東西比站在我的txt文檔的第一行,它不會工作。

例子:

如果我添加,託比和Sara到列表中,也只會是能夠找到託比。

我該怎麼辦? P:我知道有很多錯誤的東西,在這段代碼中對不起。

#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"); 
} 
+1

爲什麼你一直叫'main':

你可以解決這個問題? –

回答

1

您可以逐個查找並修復錯誤。添加日誌記錄或在必要時使用調試器。

這裏有一個能幫助你走好:

的代碼塊:

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

應的代碼塊外:

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

你把它放在塊內部,造成如果第一行不匹配,可以調用main(爲什麼你會這樣做呢?!)函數。

此外,你的邏輯在任何地方都是可怕的。例如:

case 2: 
    searchname(); 
    break; 

searchname函數永不返回。那麼break在做什麼?

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

推送以換行符結尾的單個字符串。

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

以換行符結束的第一個字符串去名,第二個去派,第三個進入CLASSE,等等。

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