array given numbers

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
Load the datafile into an array or vector, then check as you would normally.

1
2
3
4
5
6
7
8
9
std::ifstream fin(input.txt);
std::vector<int> MyList;

int temp;
while (fin.good())
{
  fin >> temp;
  MyList.push_back(temp);
}
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;
}
i can not search the number and see if the option is good or sorry :(
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
}
thank you so much for all you help Stewbond (948) .. i really really appreciate your help. i hope you have a great weekend.
Topic archived. No new replies allowed.