使用 .open() 成员函数 C++ 的 ifstream 数组的错误访问代码
bad access code with array of ifstream using .open() member function C++
我有一个相当简单的任务,其中我有两个包含 50 个名称的文本文件,我向用户查询文本文件 "girls names" 或 "boys name" 和一个名称,然后程序检查是否用户输入的名称在他们选择的文件中。我想我已经差不多明白了,但是当我试图打开第二个文件时,我得到了一个错误的访问代码。我读过有关内存分配的问题,我认为这是我的代码的问题,但答案超出了我的理解水平,所以我被困住了。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Declare Variables
ifstream inFile[1]; // male and female file streams
int inFileArrayVar = 0; // allows switching from female and male file streams
string gender;
string name[50];
string inputName;
int popular = 0;
bool inputCondition = 1;
bool progLoop = 1;
// Welcome Message
cout << "*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o" << '\n'
<< " WELCOME TO THE GENDER BINARY MACHINE" << '\n'
<< "*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o" << '\n' << '\n'
<< "Enter a name and see if it is one of the most popular Canadian female or male names in 2015. " << '\n' << '\n';
// Open Files
inFile[0].open("GirlsNamesCanada2015.txt");
inFile[1].open("BoysNamesCanada2015.txt");
// User Query
while (inputCondition == 1){
cout << "Choose to query either the male or female database by entering 'f' or 'm' " << '\n';
cin >> gender;
if (gender == "f") {
cout << "You have selected the female name database. Enter the name you'd like to search: " << '\n';
inFileArrayVar = 0;
inputCondition = 0;
}
else if (gender == "m"){
cout << "You have selected the male name database. Enter the name you'd like to search: " << '\n';
inFileArrayVar = 1;
inputCondition = 0;
}
else
inputCondition = 1;
}
while (progLoop == 1){
cin >> inputName;
for (int i = 0; i < 50; i++) {
inFile[inFileArrayVar] >> name[i];
if (inputName.compare(name[i]) == 0) {
popular = 1;
cout << inputName << " was one of the most popular names in 2015." << '\n' << '\n';
i = 50;
}
}
if (popular == 0){
cout << inputName << " was not one of the most popular names is 2015." << '\n' << '\n';
}
cout << "If you want to contine, enter either 'f' or 'm', otherwise enter any other character. " << '\n';
cin >> gender;
if (gender == "f"){
cout << "You have selected the female name database. Enter the name you'd like to search: " << '\n';
inFileArrayVar = 0;
}
else if (gender == "m"){
inFileArrayVar = 1;
cout << "You have selected the male name database. Enter the name you'd like to search: " << '\n';
}
else
progLoop = 0;
}
cout << "merci d'avoir choisi le GENDER BINARY MACHINE ******* pce out ******** " << '\n' << '\n';
return 0;
}
ifstream inFile[1]; // male and female file streams
在这里,您声明了一个只有一个元素的数组。这个数组只有一个元素。这就是[1]的意思,这里
inFile[0].open("GirlsNamesCanada2015.txt");
inFile[1].open("BoysNamesCanada2015.txt");
然后您尝试访问数组中的元素 #0 和元素 #1,即这个元素数组中的第一个和第二个元素。可能会出什么问题?
声明数组时:
type array[n];
这意味着数组包含元素 #0 到元素 #n-1。
我有一个相当简单的任务,其中我有两个包含 50 个名称的文本文件,我向用户查询文本文件 "girls names" 或 "boys name" 和一个名称,然后程序检查是否用户输入的名称在他们选择的文件中。我想我已经差不多明白了,但是当我试图打开第二个文件时,我得到了一个错误的访问代码。我读过有关内存分配的问题,我认为这是我的代码的问题,但答案超出了我的理解水平,所以我被困住了。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Declare Variables
ifstream inFile[1]; // male and female file streams
int inFileArrayVar = 0; // allows switching from female and male file streams
string gender;
string name[50];
string inputName;
int popular = 0;
bool inputCondition = 1;
bool progLoop = 1;
// Welcome Message
cout << "*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o" << '\n'
<< " WELCOME TO THE GENDER BINARY MACHINE" << '\n'
<< "*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o" << '\n' << '\n'
<< "Enter a name and see if it is one of the most popular Canadian female or male names in 2015. " << '\n' << '\n';
// Open Files
inFile[0].open("GirlsNamesCanada2015.txt");
inFile[1].open("BoysNamesCanada2015.txt");
// User Query
while (inputCondition == 1){
cout << "Choose to query either the male or female database by entering 'f' or 'm' " << '\n';
cin >> gender;
if (gender == "f") {
cout << "You have selected the female name database. Enter the name you'd like to search: " << '\n';
inFileArrayVar = 0;
inputCondition = 0;
}
else if (gender == "m"){
cout << "You have selected the male name database. Enter the name you'd like to search: " << '\n';
inFileArrayVar = 1;
inputCondition = 0;
}
else
inputCondition = 1;
}
while (progLoop == 1){
cin >> inputName;
for (int i = 0; i < 50; i++) {
inFile[inFileArrayVar] >> name[i];
if (inputName.compare(name[i]) == 0) {
popular = 1;
cout << inputName << " was one of the most popular names in 2015." << '\n' << '\n';
i = 50;
}
}
if (popular == 0){
cout << inputName << " was not one of the most popular names is 2015." << '\n' << '\n';
}
cout << "If you want to contine, enter either 'f' or 'm', otherwise enter any other character. " << '\n';
cin >> gender;
if (gender == "f"){
cout << "You have selected the female name database. Enter the name you'd like to search: " << '\n';
inFileArrayVar = 0;
}
else if (gender == "m"){
inFileArrayVar = 1;
cout << "You have selected the male name database. Enter the name you'd like to search: " << '\n';
}
else
progLoop = 0;
}
cout << "merci d'avoir choisi le GENDER BINARY MACHINE ******* pce out ******** " << '\n' << '\n';
return 0;
}
ifstream inFile[1]; // male and female file streams
在这里,您声明了一个只有一个元素的数组。这个数组只有一个元素。这就是[1]的意思,这里
inFile[0].open("GirlsNamesCanada2015.txt");
inFile[1].open("BoysNamesCanada2015.txt");
然后您尝试访问数组中的元素 #0 和元素 #1,即这个元素数组中的第一个和第二个元素。可能会出什么问题?
声明数组时:
type array[n];
这意味着数组包含元素 #0 到元素 #n-1。