Visual如何将文件与整数一起使用到阵列C 中

visual How to use file with integer into an array C++

本文关键字:阵列 一起 整数 文件 Visual      更新时间:2023-10-16

我的任务是创建一个程序,以确保未经授权的用户无法进入系统。(这只是一个场景,而不是真实的事情)。给我一个文本文件,上面有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;
}

我的问题是我不知道如何将文本文件用作数组。而且它仅读取文件的第一个数字。这是文件中的一些数字(145014521454145614581460)和我构建的程序仅读取1450。

单独读取代码并检查用户输入。在一个循环中完成所有操作是没有意义的。

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;
    }
}

C 的更好方法是使用std::vector<int>而不是原始数组:

std::vector<int> codes;
int code;
while(fin>>code) {
    codes.push_back(code);
}

而不是您的mySequentialSearch()功能,您可以简单地使用std::vector::find()

您去这里:

我认为在这里使用unordered_set会很好。为此,请确保您使用#include <unordered_set>

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;

注意:

  1. 使用"使用命名空间std"污染全局名称空间。我更喜欢使用" std ::"
  2. 您的MySequentialSearch会导致程序的N^2运行时间。使用集将其切成n(线性)。
  3. 有关集合的更多信息:http://en.cppreference.com/w/cpp/container/unordered_set_set
  4. 有关ifstream的更多信息:http://en.cppreference.com/w/cpp/io/basic_ifstream
  5. 添加一些错误检查可能是个好主意(不存在文件,输入不良等)