Here's the correct code(I found out after asking the question) Silly mistake of not initializing the variable correctly.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string line;
int numLine = 0; //didn't set it to zero.
ifstream dataFile;
dataFile.open("fileforstring.txt");
if(!dataFile)
{
cout<<"Error opening file.";
}
else
{
cout<<"File opened successfully";
}
while(getline(dataFile,line))
{
++numLine;
}
cout<<"\nNo of lines in text file is "<<numLine;
dataFile.close();
return 0;
}