In: Computer Science
(write a program that get the numbers from user and search the file numbers.text for that value. in C++) numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9
you must to have the exact output:
Enter a number: 10
10 last appears in the file at position 9
Enter a number: 29
29 does not appear in the file
Enter a number: 9
9 last appears in the file at position 12
Enter a number:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inputFile;
int count, number,target,found=0;
count = 0;
//opening the file
inputFile.open("numbers.txt");
if (inputFile.fail()){
cout << "File open error....";
return 0;
}
//reading the number
cout<<"Enter number : ";
cin>>target;
while (!inputFile.eof()){
count++;
inputFile >> number;
inputFile.ignore();
//checking if this is the number
if(number==target){
found=count;
}
}
//printing the details
if(found!=-1)
cout<<target<<" last appears in the file at position "<<found<<endl;
else
cout<<target<<" does not appear in the file";
inputFile.close();
return 0;
}
Note : If you like my answer please rate and help me it is very Imp for me