i need to develop a program to check if the entered Number is in the
prerecorded data file. the Data file with 300 Numbers is already provided in a file .txt . the program should access the data file and search for a match.
i know how to search if i put the numbers in the program but i do not know how to search it from the datafile given.
please help
thank you but i could not do it this is what i have so far i hope you can help me
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int seqsearch(int a[],int key)
{
int myflag;
for (int i=0; i<300; i++)
{
if (a[i]== key)
{
myflag = 1;
break;
}
if (myflag==1)
cout << "Access Granted"<<endl;
else
cout <<"Access Denied"<< endl;
}
return myflag;
}
int myfile()
{
double ID;
// string line;
ifstream myfile("Data_IDN_300.txt");
if (!myfile.fail())
{
for (int k=0; k<300; ++k )
{
myfile >> ID;
cout << ID <<'\t'<< endl;
}
}
else
{
cout << "could not open file bye" << endl;
}
system ("pause");
return 0;
}
int main()
{
int key, i=0,flag=0, temp;
int myarray[300]={ myfile()};
cout << "enter ID Number" << endl;
cin>>key;
for (int i=0; i<300; i++)
{
if (myarray[i] == key)
{
flag = 1;
break;
}
}
if (flag==1)
cout << "dale"<<endl;
else
cout <<"sorry"<< endl;
system ("pause");
return 0;
}
This line is probably not doing what you want: int myarray[300]={ myfile() };
Look at myfile(), it returns a 0 when it is done. Therefore we end up with: myarray[300] = {0}; which is a valid way of initializing everything to zero. It doesn't read everything into the array.
I think this is what you are looking for. I added comments to the lines I changed:
int myfile(int MyArray[]) // We now insert the array directly.
{
double ID;
// string line;
ifstream myfile("Data_IDN_300.txt");
if (!myfile.fail())
{
for (int k=0; k<300; ++k )
{
myfile >> MyArray[k]; // Here we are writing to the array
cout << ID <<'\t'<< endl;
}
}
else
{
cout << "could not open file bye" << endl;
}
system ("pause");
return 0;
}
int main()
{
int key, i=0,flag=0, temp;
int myarray[300]; // We are creating the array here
myfile(myarray); // And we are setting it here
cout << "enter ID Number" << endl;
cin>>key;
for (int i=0; i<300; i++)
{
if (myarray[i] == key)
{
flag = 1;
break;
}
}
if (flag==1)
cout << "dale"<<endl;
else
cout <<"sorry"<< endl;
system ("pause");
return 0;
}