I need help with an assignment. First of all, I am a BEGINNER at C++, so I need simplified answers so that I can understand them. I just started learning the library <fstream>. here is what i have so far:
___________________________________________________________
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string value;
int start;
ifstream number_file;
cout << endl << "Opening file: numbers.dat..." << endl;
number_file.open("numbers.dat");
if (number_file.fail())
{
cout << "Failed to open file." << endl; exit(1);
}
cout << "Enter a numerical value to see if it exists in that file: ";
cin >> value;
int count = 0;
while (number_file >> value)
{
count++;
cout << endl << count << " " << value;
}
cout << endl;
number_file.close();
cin >> start;
return 0;
}
___________________________________________________
and here is the data im supposed to read from:
________________________________________________
11 21 32 45 52 62 71 81 92 10
95 87 71 66 54 55 34 22 11 17
85 71 61 52 46 37 28 18 91 13
___________________________________________________
the user inputs a number, and the program checks in the .dat file if it is present. if it is, it displays where the number is. (i.e. the number 71 is in position 7, 13, and 22. please respond ASAP!! Thanks in advance!
The way you are getting input from the file is overwriting the user input, take the value from the user and when looping through the file contents display count only if the value got from the file is equal to the value given by the user.