Visual如何将文件与整数一起使用到阵列C 中
visual How to use file with integer into an array C++
我的任务是创建一个程序,以确保未经授权的用户无法进入系统。(这只是一个场景,而不是真实的事情)。给我一个文本文件,上面有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;
注意:
- 使用"使用命名空间std"污染全局名称空间。我更喜欢使用" std ::"
- 您的MySequentialSearch会导致程序的N^2运行时间。使用集将其切成n(线性)。
- 有关集合的更多信息:http://en.cppreference.com/w/cpp/container/unordered_set_set
- 有关ifstream的更多信息:http://en.cppreference.com/w/cpp/io/basic_ifstream
- 添加一些错误检查可能是个好主意(不存在文件,输入不良等)
相关文章:
- OpenMP阵列性能较差
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 位阵列上的快速AND运算
- 阵列必须使用大括号封闭的初始器进行初始化
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 与其所有者一起分配阵列的正确方法
- Visual如何将文件与整数一起使用到阵列C 中