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 filen";
return;}
if (!MaleFile){
cout << "Error, cannot open this filen";
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;
}
它总是给我这个错误:
错误 C2065:"ifstream":未声明的标识符 错误 C2059:语法错误:"," 错误 C3861:"读取文件":找不到标识符
你能帮我这个吗?
我认为你把using namespace std;
放在函数声明之前。我的意思是在int DisplayMenu();
之前添加这个.那就没事了。
你之前丢失了std::ifstream
使用命名空间标准;
在函数声明中。
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 打印 2D 动态数组 c++ 函数
- C++数组函数
- 在数组函数指针中调用void
- 数组函数似乎没有找到最大值
- 使用数组函数和循环求解伪元编码|C
- 在头文件中的函数原型中获取指针数组:函数声明为 void
- 数组函数错误输出
- 读取文件,然后创建数组函数C++
- 指针数组函数参数-奇怪的行为
- 二维数组 - 函数
- "return"没有从数组函数返回正确的值
- 数组函数问题
- 数组函数的大小返回 int 的大小而不是数组
- 从数组函数中删除元素
- 如何从C++中最好地调用具有C99风格数组函数签名的函数
- 追加/打印/插入数组c++函数问题
- 如何将数组函数转换为指针函数
- 多维数组函数输出垃圾
- C++:数组函数的递归最大值