C++ 如何将带整数的文件放入数组C++;

C++ 如何将带整数的文件放入数组C++;,c++,visual-c++,C++,Visual C++,我的任务是创建一个程序,以确保未经授权的用户不能进入系统。(这只是一个场景,不是真实情况)。我得到了一个上面有300个数字的文本文件。用户必须键入号码,如果该号码未包含在文本文件中,则访问将被拒绝。如果它包含在文本文件中,则将授予访问权限。其余部分将显示在下面。 到目前为止,这就是我所做的 #include <iostream> #include <fstream> using namespace std; bool mySequentialSearch(int data

我的任务是创建一个程序,以确保未经授权的用户不能进入系统。(这只是一个场景,不是真实情况)。我得到了一个上面有300个数字的文本文件。用户必须键入号码,如果该号码未包含在文本文件中,则访问将被拒绝。如果它包含在文本文件中,则将授予访问权限。其余部分将显示在下面。 到目前为止,这就是我所做的

#include <iostream>
#include <fstream>
using namespace std;
bool mySequentialSearch(int data[300], int key, int size)
{
for(int i=0; i<size; i++)
{
    if (data[i] == key)
        return true;
}

return false;
}

int main()

{
int codes;
string line;
ifstream fin ("SystemAccessCodes.txt");
while (fin>>codes)
{
for(int i=0; i<3; i++)
{
        cout<<"\nAttempt "<< i+1 << "/3 : ENTER 4 DIGIT CODE: ";
        int ans;
        cin>>ans;

    if(mySequentialSearch(&codes, ans, 300))
    {
        cout<<"===================="<<endl;
        cout<<"   Access Granted   "<<endl;
        cout<<"      Welcome       "<<endl;
        cout<<"===================="<<endl;
        system ("pause");
        return 0;
    }
    else
    {
        cout<<"\nNot matching! Try Again"<<endl;
    }
fin.close();
}
}

system ("pause");
return 0;
}
#包括
#包括
使用名称空间std;
bool mySequentialSearch(int数据[300],int键,int大小)
{
对于(int i=0;i>代码)
{

对于(inti=0;i而言,分别读取代码和检查用户输入。在单个循环中执行所有这些操作是没有意义的

int codes[300];
for(int i = 0; i < 300 && fin; ++i) {
    fin>>codes[i];
}
fin.close();

for(int i=0; i<3; i++)
{
    cout<<"\nAttempt "<< i+1 << "/3 : ENTER 4 DIGIT CODE: ";
    int ans;
    cin>>ans;

    if(mySequentialSearch(codes, ans, 300))
    {
        cout<<"===================="<<endl;
        cout<<"   Access Granted   "<<endl;
        cout<<"      Welcome       "<<endl;
        cout<<"===================="<<endl;
        system ("pause");
        return 0;
    }
    else
    {
        cout<<"\nNot matching! Try Again"<<endl;
    }
}
int代码[300];
对于(int i=0;i<300&&fin;++i){
fin>>代码[i];
}
fin.close();
对于(int i=0;i,给你:

我认为在这里使用无序的集合会很好。要得到它,请确保使用
#include

int码;
std::字符串行;
std::ifstream-fin(“SystemAccessCodes.txt”);
//让我们用一套。
//集合不能包含重复项。如果该数字在集合中,则它是有效代码。
//使用字符串而不是整数作为密码可能是更好的选择
//(大量可能导致问题)
std::无序_集代码集;
//填充集合
while(fin>>代码)
{
代码集。插入(代码);
}
fin.close();
//现在运行这个迭代
对于(int i=0;i<3;i++)
{

std::不欢迎使用堆栈溢出。请花时间阅读并参考“您可以在此处询问的内容和方式”中的资料。@现在可以详细说明哪些内容与您的代码不符。@πάνταῥεῖ 请帮帮我!@May您的代码有几种不同的方式。首先
bool mySequentialSearch(int data[300],int key,int size)
需要一个整数数组作为参数,但您在这里传递的是单个整数的地址:
mySequentialSearch(&code,ans,300)
。我建议您将从文件中读取的值存储在
std::vector
中,并将其用作函数的参数。首先读取文件中的所有ID,并检查循环外部的用户输入。@我希望这对您的帮助足够了。非常感谢!我担心您的攻击目标过大了。我怀疑OP能否解决他们的问题使用
std::unordered\u集执行任务,他们的教授接受了这一点。不过,您的解决方案是正确的。
std::vector<int> codes;
int code;
while(fin>>code) {
    codes.push_back(code);
}
int codes;
std::string line;
std::ifstream fin("SystemAccessCodes.txt");
//lets use a set.  
//Sets cannot contain duplicates. If the number is in the set, it is a valid code.
//it might be a better option to use strings rather than ints as passwords 
//(large numbers could cause problems)

std::unordered_set<int> codeset;

//populate the set
while (fin >> codes)
{
    codeset.insert(codes);
}

fin.close();

//now run this iteration
    for (int i = 0; i < 3; i++)
    {
        std::cout << "\nAttempt " << i + 1 << "/3 : ENTER 4 DIGIT CODE: ";
        int ans;
        std::cin >> ans;

        //count returns either 0 or 1: 0 if the ans is not in it, 1 if it is
        if(codeset.count(ans))
        {
            std::cout << "====================\n";
            std::cout << "   Access Granted   \n";
            std::cout << "      Welcome       \n";
            std::cout << "====================\n";
            system("pause");
            return 0;
        }
        else
        {
            std::cout << "\nNot matching! Try Again" << std::endl;
        }
    }

system("pause");
return 0;