User input and reading contents of file For full disclosure: this is an assignment for my programming class and I just want tips or advice on some of the code.

Assignment details just for background information purposes:


  Write a program that will read a number 1-100 from the user, and the
  name of a data file, and will tell the user what word is in the file
  and how many times the numbers shows up in the data file. Validate
  input number (keep asking until valid) and validate the file was
  successfully open.
  
  text file contents: Darling 10 20 21 19 20




#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt;  // Needed for exit()
using namespace std;

int main()
{
    ifstream inputFile;
    string fileName;
    int value;

    cout &lt;&lt; "Enter the file name: ";
    getline(cin, fileName);

    // Open the file
    inputFile.open(fileName.c_str());

    // Check for successful opening
    if(inputFile.fail())
    {
       cerr &lt;&lt; "Error Opening File" &lt;&lt; endl;
       exit(1);
    }

    cout &lt;&lt; "\nThe file has been successfully opened for reading.\n";

    cout &lt;&lt; "Pick a number between 1 through 100. ";
    cin &gt;&gt; value;

    if (value &gt;= 1 &amp;&amp; value &lt;= 100)
    {
     cout &lt;&lt; value &lt;&lt; " is in the acceptable range.\n";
    }
    else
    {
     cout &lt;&lt; "Please enter a number from 1 to 100. ";
     cin &gt;&gt; value;
    }

    string word;
    int number;
    int count = 0;

    infile &gt;&gt; number;

    // Read a file until you've reached the end
    while (!inputFile.eof())
    {
        if (number == x)
        {
            sum1++
        }
        infile &gt;&gt; number;
    }
    cout &lt;&lt; sum1;

    while (!inputFile.eof())
    {
       inputFile &gt;&gt; word;
        if (word == "input")
        {
            count++;
        }
    }

    // Close the file
    inputFile.close();
    return 0;
}


Any tips or advice would be greatly appreciated since I'm lost when it comes to the fstream portion of this assignment right now.

UPDATED

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt;  // Needed for exit()
using namespace std;

int main()
{
    ifstream inputFile;
    string fileName;
    int value, x;

    cout &lt;&lt; "Enter the file name: ";
    getline(cin, fileName);

    // Open the file
    inputFile.open(fileName.c_str());

    // Check for successful opening
    if(inputFile.fail())
    {
       cerr &lt;&lt; "Error Opening File" &lt;&lt; endl;
       return(1);
    }

    cout &lt;&lt; "Pick a number between 1 through 100. ";
    cin &gt;&gt; value;

    do
{
    cout &lt;&lt; "Pick a number between 1 through 100. ";
    int value;
    cin &gt;&gt; value;

} while (value &lt; 1 || value &gt; 100);

    string word;
    int number;
    int count = 0;

    infile &gt;&gt; number;

    // Read a file until you've reached the end
    while (!inputFile.eof())
    {
        if (number == x)
        {
            sum1++
        }
        inputFile &gt;&gt; number;
    }
    cout &lt;&lt; sum1;

    while (!inputFile.eof())
    {
       inputFile &gt;&gt; word;
        if (word == "input")
        {
            count++;
        }
    }

    // Close the file
    inputFile.close();
    return 0;
}
Despite what you may have been taught, you should not get into the habit of using using namespace std, especially when you start writing larger programs.
Since you're in main(), you should just return 1 instead of calling exit(1).  The latter is only necessary in other places from which you will need to terminate early.
You don't need to display an output if the file was successfully opened.  You'll know it failed if the program terminates after asking for a filename.
Do not use std::eof() for detecting the end of the file.  It will look for the eof bit after reading the end of the stream (and will not even consider errors in reading (i.e. bad bit)).  Read this for more information.

Instead, read from the file and check for the bad bit using std::getline():

while (std::getline(std::cin, line))
{
    // ...
}

You ask the user for input, but you only validate it once.  If the user enters an invalid value a second time, the program will continue, causing problems.  All of that should be in a loop that will terminate once the user provides valid input.

do
{
    std::cout &lt;&lt; "Pick a number between 1 through 100. ";
    std::cin &gt;&gt; value;

} while (value &lt; 1 || value &gt; 100);

You don't need return 0 at the end of main().  Reaching the end already implies a successful termination, so the compiler will do this return for you.