This week for homework I've been tasked with loading in a text file of 1,000 numbers and to do a linear search of a number entered in by a user. I have the linear search part done, but I have to find and print the last occurrence of that integer. I figured it would be easiest to run the array from the end and print the last occurrence and break the loop. I've started the code, but am having some trouble at finding the last occurrence.
I know my second for loop to run the array backwards is wrong, I'm just not sure what about it is wrong. Any help is appreciated! Thank you!
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main()
{
ifstream input("A1.txt");
int find;
cout << "Enter a number to search for: ";
cin >> find;
if (input.is_open())
{
int linSearch[1000];
for (int i = 0; i < 1000; i++)
{
input >> linSearch[i];
for (i = 1000; i > 0; i--)
{
if (find == linSearch[i])
{
cout << find << " is at position " << i << ". " << endl;
}
}
}
}
_getch();
return 0;
}