2013-11-04 30 views
0

我的代碼應該將txt文件中的信息傳遞給字符串向量,然後詢問用戶輸入並將其與字符串向量進行比較以查看是否存在任何匹配。由於某些原因,當我在輸入文件中輸入一行時,它與字符串向量不匹配。這裏是我的代碼C++將文件行傳遞給字符串向量

預先感謝您

/*You are going to keep track of user majors using a vector. 

First read in the file: 

http://www.freerschool.com/pluginfile.php/9623/mod_resource/content/1/MajorsFull.txt 

Enter each major into a vector of strings. 

Ask the user to keep entering in possible majors until they enter "quit" or "Quit". 

Create a function: 

bool checkMajor(string userInput) 

that takes in the major from the user (as a string) and returns true if the major is in the list of possible majors and a false if the major is not there. 

Display to the screen whether or not the major they entered is available in the file of majors. 

Hint: 

for(string line; getline(input, line);) 
{ 
//Read in the line into the vector! 
}*/ 
#include <iostream> 
#include <fstream> 
#include <vector> 

using namespace std; 

bool checkMajor(string userInput, vector<string>majorsFull){ 
    bool answer = true; 
    string major; 
    for(int i = 0; i < majorsFull.size(); i++){ 
     if (majorsFull[i] == userInput){ 
       answer = true; 
       break; 
     } 
     else answer = false; 
    } 
    return answer; 
} 

int main() 
{ 
    ifstream infile; 
    infile.open ("MajorsFull.txt"); 

    vector<string> majorsFull; 
    string userInput; 

    for (string majors; getline(infile, majors);){ 
     majorsFull.push_back(majors); 
    } 

    do 

    { 

    getline(cin, userInput); 

    if (userInput != "Quit" && userInput != "quit"){ 


    if (checkMajor(userInput, majorsFull)) 
    { 
      cout << "Yes" << endl; 
    } 

    else cout << "No" << endl; 

    } 

    else break; 

    } 


    while (userInput != "Quit" && userInput != "quit"); 

    infile.close(); 

    return 0; 
} 

這裏是什麼樣的文件中包含的幾行:

Accounting 
Accounting 
Actuarial Science 
Advertising 
Advertising 
African American and African Studies 
African American and African Studies 
Agribusiness Management 
"Agricultural, Food and Resource Economics" 
"Agricultural, Food and Resource Economics" 
Animal Science 
Animal Science 
Animal Science 
Animal Science-Environmental Toxicology 
Anthropology 
Anthropology 
Applied Engineering Sciences 
Applied Mathematics 
Applied Mathematics 
Applied Spanish Linguistics 
Applied Statistics 
Arabic 
Art Education 
Art History and Visual Culture 
Arts and Humanities 
Astrophysics 
Astrophysics and Astronomy 
Astrophysics and Astronomy 
+0

不匹配甚至儘管我把什麼確切的txt文件 –

+0

你可以發佈什麼樣的文件包含了一些簡單的線什麼? – jrd1

+0

剛從txt發佈了幾行 –

回答

0

的問題是,在checkMajor()你繼續走了過來可能即使在您發現用戶輸入了文件中存在的答案之後,答案也是如此,因此您的for循環應爲:

for(int i = 0; i < majorsFull.size(); i++){ 
    if (majorsFull[i] == userInput) return true; 
} 

return false; 
+0

它仍然不起作用 –

0

你的問題是在下面的函數中,我已經修復了它。在你的代碼中,如果最後一行不匹配,它總是返回false。

bool checkMajor(string userInput, vector<string>majorsFull){ 
    bool answer; 
    string major; 
    for(int i = 0; i < majorsFull.size(); i++){ 
     if (majorsFull[i] == userInput) return answer = true; 
     //else answer = false; 
    } 
    return false; 
} 
+0

出於某種原因,它仍然給我相同的答案 –

+0

我也嘗試了最後一行txt文件,它不起作用 –

+0

但是,當我嘗試使用最後一行和正確的代碼時,它會起作用。哦,這不是問題。當for循環找到它時,它就會停止。 – liaotonglang

相關問題