gpt4 book ai didi

c++ - 我将如何准确地获取打开的文件和文件中的名称并将它们放入我的字符串 vector 中?

转载 作者:行者123 更新时间:2023-11-28 02:30:25 29 4
gpt4 key购买 nike

#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 循环中。我不确定名称是否正在从打开的文件传输到字符串数组/vector ,我也不完全确定如何使它起作用。如果有人可以提供帮助或提出一些建议,我们将不胜感激。

最佳答案

你有一个错字:

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

那个分号使下一行成为一个正常的 block ,所以 boynamevector[counter] 超出了范围。

关于c++ - 我将如何准确地获取打开的文件和文件中的名称并将它们放入我的字符串 vector 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157113/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com