C++ - ifstream 和数组函数
C++ - ifstream and array functions
我对其中包含 ifstream
和 string
的函数有疑问。
这是我的代码:
#include <iostream>
#include <time.h>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string>
const int ArrayMax = 100;
int DisplayMenu();
void LoadNames();
void ReadFile(ifstream& , ifstream& ,string[],string[]);
using namespace std;
int main()
{
ifstream FemaleFile;
ifstream MaleFile;
string Female[ArrayMax];
string Male[ArrayMax];
DisplayMenu();
ReadFile(FemaleFile, MaleFile, Female,Male );
return 0;
}
int DisplayMenu() //Displays menu and returns user selection
{
//variables
int selection;
//Headers
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << " Name Guess Game" << endl;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << " Selaect Name Category" << endl;
cout << " 1. Female Names" << endl;
cout << " 2. Male Names" << endl;
cout << " 3. Exit" << endl << endl;
cout << " Enter 1, 2 or 3: " ;
cin >> selection;
while ((selection != 1) && (selection != 2) && (selection != 3) )
{
cout << " Invaild choice, Please Enter 1, 2 or 3: " ;
cin >> selection;
}
return selection;
}
void LoadNames()//Loads name lists from data files into two arrays and returns array sizes. Uses ReadFile(…) function
{
return;
}
void ReadFile(ifstream & FemaleFile ,ifstream & MaleFile, string Female[], string Male[] )//Reads the data of the received file into the received array size and returns the array size.
{
//opening files
FemaleFile.open("female.txt");
MaleFile.open("male.txt");
//Testing files
if (!FemaleFile){
cout << "Error, cannot open this file\n";
return;}
if (!MaleFile){
cout << "Error, cannot open this file\n";
return;}
for (int i=0 ; i < ArrayMax; i++)
{
FemaleFile >> Female[i];
cout << Female[i] << endl;
}
for (int i=0 ; i < ArrayMax; i++)
{
MaleFile >> Female[i];
cout << Male[i] << endl;
}
//closeing files
FemaleFile.close();
MaleFile.close();
return;
}
它总是给我这个错误:
error C2065: 'ifstream' : undeclared identifier
error C2059: syntax error : ','
error C3861: 'ReadFile': identifier not found
你能帮我解决这个问题吗?
您在
之前输了 std::ifstream
using namespace std;
在函数声明中。
我认为您在函数声明之前放置了 using namespace std;
。我的意思是在 int DisplayMenu();
之前添加这个。那么就可以了。
我对其中包含 ifstream
和 string
的函数有疑问。
这是我的代码:
#include <iostream>
#include <time.h>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string>
const int ArrayMax = 100;
int DisplayMenu();
void LoadNames();
void ReadFile(ifstream& , ifstream& ,string[],string[]);
using namespace std;
int main()
{
ifstream FemaleFile;
ifstream MaleFile;
string Female[ArrayMax];
string Male[ArrayMax];
DisplayMenu();
ReadFile(FemaleFile, MaleFile, Female,Male );
return 0;
}
int DisplayMenu() //Displays menu and returns user selection
{
//variables
int selection;
//Headers
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << " Name Guess Game" << endl;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << " Selaect Name Category" << endl;
cout << " 1. Female Names" << endl;
cout << " 2. Male Names" << endl;
cout << " 3. Exit" << endl << endl;
cout << " Enter 1, 2 or 3: " ;
cin >> selection;
while ((selection != 1) && (selection != 2) && (selection != 3) )
{
cout << " Invaild choice, Please Enter 1, 2 or 3: " ;
cin >> selection;
}
return selection;
}
void LoadNames()//Loads name lists from data files into two arrays and returns array sizes. Uses ReadFile(…) function
{
return;
}
void ReadFile(ifstream & FemaleFile ,ifstream & MaleFile, string Female[], string Male[] )//Reads the data of the received file into the received array size and returns the array size.
{
//opening files
FemaleFile.open("female.txt");
MaleFile.open("male.txt");
//Testing files
if (!FemaleFile){
cout << "Error, cannot open this file\n";
return;}
if (!MaleFile){
cout << "Error, cannot open this file\n";
return;}
for (int i=0 ; i < ArrayMax; i++)
{
FemaleFile >> Female[i];
cout << Female[i] << endl;
}
for (int i=0 ; i < ArrayMax; i++)
{
MaleFile >> Female[i];
cout << Male[i] << endl;
}
//closeing files
FemaleFile.close();
MaleFile.close();
return;
}
它总是给我这个错误:
error C2065: 'ifstream' : undeclared identifier error C2059: syntax error : ',' error C3861: 'ReadFile': identifier not found
你能帮我解决这个问题吗?
您在
之前输了 std::ifstreamusing namespace std;
在函数声明中。
我认为您在函数声明之前放置了 using namespace std;
。我的意思是在 int DisplayMenu();
之前添加这个。那么就可以了。