[英]How exactly would I take the open file and the names that are in the files and put them into my 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循環中。 我不確定名稱是否已從打開文件傳輸到字符串數組/向量,並且我不確定如何使它工作。 如果有人可以幫助或提出一些建議,將不勝感激。
您有錯字:
for (counter = 0; counter < numberofnames; counter++);
該分號使下一行成為普通塊,因此boynamevector[counter]
超出范圍。
首先,在進入循環之前,您需要正確讀取所有男孩和女孩的名字,這樣您才不會一次又一次地重新讀取名字:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.