我究竟如何获取打开的文件和文件中的名称并将它们放入我的字符串向量中?

时间:2015-03-19 23:56:26

标签: c++ arrays string vector

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
    char response = 'y';
    while (response == 'y')
        do
        {
        const int numberofnames = 200;
        vector <string> boynamevector(numberofnames);
        vector <string> girlnamevector(numberofnames);
        ifstream readboy;
        ifstream readgirl;
        string boy;
        string girl; 
        int choice;
        readboy.open("BoyNames.txt");
        readgirl.open("GirlNames.txt");
        bool check = true;
        int boychoice = 1;
        int girlchoice = 2;
        int bothchoice = 3;
        for (int counter = 0; counter < numberofnames; counter++)
        {
            readboy >> boynamevector[counter];
            readgirl >> girlnamevector[counter];
        }
        cout << "\t\Popular Boy or Girl or Both Choices\n\n"
            << "1. Popular Boy Name\n"
            << "2. Popular Girl Name\n"
            << "3. Popular name for both Boy and Girl\n"
            << "Enter choice: ";
        cin >> choice;
        if (choice == boychoice)
            cout << "Enter a boy's name: ";
        cin >> boy;
        check = false;
        int counter;
        for (counter = 0; counter < numberofnames; counter++);
        {
            if (boy == boynamevector[counter])
                check = true; 
        }
        if (check == true)
            cout << "The name is popular\n";
        else
            cout << "The name is not popular\n";
            cout << "Would you like to run the program again? \n"
            << "Enter y for yes or n for no: ";
        cin >> response;
        } while (response == 'Y' || response == 'y');
    return 0;
}

我认为问题出现在第一个for循环中。我不确定名称是从打开的文件传输到字符串数组/向量,而且我不完全确定如何使其工作。如果有人可以帮助或提出一些非常感谢的建议。

2 个答案:

答案 0 :(得分:2)

你有一个错字:

for (counter = 0; counter < numberofnames; counter++);

该分号使下一行成为普通块,因此boynamevector[counter]超出范围。

答案 1 :(得分:0)

首先,您需要在进入循环之前正确阅读所有男孩和女孩的名字,这样您就不会一遍又一遍地重读这些名字:

string response = "y";
ifstream readboy;
ifstream readgirl;

//Open the files
readboy.open("BoyNames.txt");
readgirl.open("GirlNames.txt");

if(!readboy.is_open())
    return 0;
if(!readgirl.is_open())
    return 0;

vector <string> boynamevector;
vector <string> girlnamevector;
string szName;
while(getline(readboy, szName))
    boynamevector.push_back(szName);
while(getline(readgirl, szName))
    girlnamevector.push_back(szName);

readboy.close();
readgirl.close();

然后反复比较字符串

do
{
    string boy;
    string girl; 
    int choice;
    bool bExists = true;
    int boychoice = 1;
    int girlchoice = 2;
    int bothchoice = 3;

    cout << "\tPopular Boy or Girl or Both Choices\n\n"
        << "1. Popular Boy Name\n"
        << "2. Popular Girl Name\n"
        << "3. Popular name for both Boy and Girl\n"
        << "4. Exit\n"
        << "Enter choice: ";
    cin >> choice;
    if(choice == 4)
        break;

    if (choice == boychoice)
        cout << "Enter a boy's name: ";
    if (choice == girlchoice)
        cout << "Enter a girls's name: ";

    cin >> boy;
    bExists = false;

    //Find the name
    if(choice == 3 || choice == 1)
    {
        for (unsigned int i = 0; i < boy.size(); i++)
        {
            if (boy == boynamevector[i])
                bExists = true; 
        }
    }
    else if(choice == 3 || choice == 1)
    {
        for (unsigned int i = 0; i < girlnamevector.size(); i++)
        {
            if (boy == girlnamevector[i])
                bExists = true; 
        }
    }

    if (bExists == true)
        cout << "The name is popular\n";
    else
        cout << "The name is not popular\n";
    cout << "Would you like to run the program again? \n"
        << "Enter y for yes or n for no: ";

    cin >> response;
} while (response == "Y" || response == "y");


return 0;