0

私のコードは、txtファイルの情報を文字列ベクトルに渡し、ユーザーに入力を求め、それを文字列ベクトルと比較して一致するかどうかを確認することになっています。何らかの理由で、入力ファイルにある行を入力すると、文字列ベクトルと一致しません。これが私のコードです

前もって感謝します

/*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
4

2 に答える 2

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;
}
于 2013-11-04T05:32:55.500 に答える
0

問題はcheckMajor()、ユーザーがファイルに存在する回答を入力したことがわかった後でも、可能な回答を調べ続けることです。そのため、forループは次のようになります。

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

return false;
于 2013-11-04T05:21:38.393 に答える