C++ - ifstream 和数组函数

C++ - ifstream and array functions

本文关键字:数组 函数 ifstream C++      更新时间:2023-10-16

我对一个包含ifstreamstring的函数有问题。这是我的代码:

#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

使用命名空间标准;

在函数声明中。