In: Computer Science
Develop a C++ program that looks for a given value in a text file full of integer values
Prompt the user for a value to search for in the file
Open the accompanying text file named numbers.txt
Search the contents of the text file
Maintain how many many times the value is found in the file
Close the text file when processing is complete
Display the number of times the value is found in the file
Number.txt
61 62 71 40 32 6 27 8 74 3 95 10 74 69 66 37 94 10 17 55 38 5 43 69 19 89 65 74 71 33 85 74 26 5 6 40 36 100 4 57 56 73 78 24 36 48 84 3 89 80 93 49 46 8 39 89 50 44 47 78 37 12 41 4 40 57 21 13 43 73 64 4 67 8 37 10 9 51 38 67 68 30 66 1 42 43 53 7 93 32 33 94 22 74 84 77 48 27 37 51 28 100 86 91 8 30 13 1 23 17 81 39 86 70 6 74 4 70 58 31 7 45 26 69 73 38 40 82 82 99 49 23 31 47 43 82 55 55 52 18 5 71 16 15 25 75 66 6 20 34 95 14 62 89 49 99 38 91 20 9 40 24 10 23 18 42 12 61 50 19 86 44 8 83 15 42 37 30 1 42 61 5 52 29 22 18 60 13 24 54 82 2 43 66 82 91 23 67 48 71 80 95 47 55 94 59 82 67 1 40 25 17 71 27 8 30 66 97 43 100 30 78 26 18 5 34 90 63 44 85 47 55 97 55 56 42 100 38 56 99 17 70 98 86 90 41 24 61 20 67 56 51 93 10 15 74 42 58 59 38 38 27 71 95 82 42 93 53 18 23 98 92 80 42 31 94 67 99 70 89 40 20 91 35 48 17 22 30 35 27 23 3 92 45 14 24 20 72 95 18 60 54 30 54 3 37 55 50 95 67 13 70 63 75 66 91 47 45 66 74 81 5 14 63 33 99 74 90 37 39 65 70 79 60 98 57 41 59 64 70 21 39 3 84 50 84 100 20 88 84 65 45 24 56 35 45 92 41 44 47 62 24 17 24 21 16 2 87 56 25 95 51 50 31 94 92 86 97 91 74 47 35 44 93 73 73 84 23 74 1 94 81 21 67 29 52 65 85 31 97 61 77 53 55 84 48 8 1 99 50 40 36 42 66 84 27 43 100 28 58 62 14 42 25 51 3 71 97 68 57 7 92 12 47 72 18 91 58 33 14 90 51 42 14 97 77 86 87 74 12 25 75 18 29 12 18 17 36 42 85 24 1 18 82 64 33 95 94 70 16 44 72 18 64 66 53 59 70 18 40 35 83 8 88 85 32 6 20 56 37 41 19 4 85 77 45 93 1 5 63 7 37 4 37 67 38 62 56 39 26 77 11 33 75 81 75 23 77 43 78 36 56 16 66 81 98 37 38 1 19 59 82 20 8 90 64 49 55 16 16 93 64 54 24 10 90 14 68 67 59 99 25 69 41 90 40 30 7 41 95 46 44 46 90 40 13 47 69 28 21 37 73 35 85 48 25 100 45 56 93 81 53 86 10 23 52 67 83 63 4 74 73 34 56 34 50 8 72 69 77 80 41 6 49 100 77 17 47 18 62 14 88 30 88 94 93 58 54 49 95 76 9 66 89 36 72 16 13 20 33 54 5 57 20 57 72 27 2 46 66 13 3 4 44 41 36 85 13 26 97 63 50 8 49 87 19 23 47 60 68 52 17 13 42 57 67 13 43 70 31 60 83 17 6 58 60 97 6 65 75 58 98 59 36 69 15 79 62 99 31 14 11 95 67 58 65 96 19 43 73 76 15 72 54 94 73 43 84 72 70 52 28 10 32 35 50 91 77 24 40 38 38 31 91 100 55 34 71 32 90 20 29 46 29 45 20 30 61 1 5 19 48 70 97 41 33 5 21 39 19 94 49 43 83 1 60 38 76 40 49 14 96 34 18 43 90 85 42 48 52 45 79 7 94 18 24 23 68 90 58 64 81 1 74 32 100 27 81 94 83 42 55 28 13 66 43 15 84 95 1 74 53 33 86 41 38 17 6 18 76 2 45 75 15 38 97 67 95 72 69 85 49 2 22 10 65 10 98 53 60 27 77 2 48 30 52 85 27 65 29 44 2 43 64 5 55 31 89 35 29 96 42 73 91 55 12 59 96 44 9 51 92 11 87 93 29 12 33 67 11 65 2 5 90 66 16 94 5 52 56 57 29 56 65 97 19 57 57 75 40 69 11 90 78 32 52 83 14 22 94 87 28 43 99 20 66 63 30 34 47 95 43 35 8 79 30 33 43 61 23 77 23 27 30 88 30 67 1 13 57 57 21 24 37 64 83 1 96 63 16 38 7 80 28 59 20 87 72 8 77 88 84 70 41 95 20 66 31 24 37 67 70 91 6 8 92 58 68 69 99 19 93 44 84 83 34 64 2 94 50 96 58 55 66 69 83 9 72 45 88 100 24 11 53 75 8 18 21 25 55
CODE -
#include<bits/stdc++.h>
// #include <iostream>
// #include <iomanip>
// #include <fstream>
// If you don't want to use <bits/stdc++> or your compiler doesn't support it, you can remove it and uncomment the above three header files for your code to work.
using namespace std;
main()
{
int num,count=0;
ifstream infile ("numbers.txt");
if (!infile)
{
cout << "File not Found" << endl;
exit(0);
}
cout << "Enter the number you want to search: ";
cin >> num;
int number;
while(!infile.eof()) // loop will run until end of file.
{
infile >> number; // getting number from the file.
if (number == num)
count++; // incrementing the count if number from the file matches the number to be searched.
}
infile.close();
cout << count;
}
SCREENSHOT -