how to check if user input is within a file

- the user enters a 10 digit number.
- the program takes the first 3 digits and checks if the 3 numbers are in the input file.
- file goes from 100 - 999
1
2
3
4
5
6
7
8
9
10
11
12
void valid(string Number, string Code[1000])
ifstream file("numbers.txt");

	for (int x = 0; !file.eof(); x++)
	{
		if (Code[x] == Number.substr(0, 3))
		{
			//prints that its in it
		}
		else
		       //prints that its not in it
	}


so far it:
- only reads the first number on the file and compares it with that.
- prints that the number is not there even though it is
Try and read the numbers from the file, at the moment you don't do anything with it, and you alter nothing in the loop that might lead to it's termination, so you'll never leave that loop. I can only guess that you assume the file contents are in Code[] - > if so, they're not, and Code[] is redundant. Try something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool valid(string number)
{
	ifstream file("numbers.txt");
	string currNum;
	while (file >> currNum)
	{
		if (number.substr(0, 3) == currNum)
		{
			cout << "number = " << number << endl;
			cout << "currNum = " << currNum << endl;
			cout << number << " is valid" << endl;
                        file.close();
			return true;
		}
	}
	
	cout << number << " is not valid" << endl;
        file.close();
	return false;
}


If you already read the numbers into Code[] before, calling this function, then the file is redundant.
Topic archived. No new replies allowed.