>Hi thanks for your reply but is there anyway to do this without the string stream?
Sure just remove the outer loop with the getline in my previous code, and just read directly into word. I modified the previous code to give you an idea. I left it using strings. If you prefer char arrays, just change the following code to make word a char array, and then change the comparison to use the strcmp() function:
Code:
if (strcmp(word,"hi") == 0)
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename = "test.txt";
ifstream file;
file.open(filename.c_str());
if (!file.is_open())
{
cout << "Unable to open file " filename << endl;
cin.get();
return 1;
}
bool found_hi = false;
string word;
while (file >> word)
{
cout << word << endl;
if (found_hi == true)
{
if (word == "kim")
{
cout << "Found hi and kim." << endl;
}
}
else if (word == "hi")
{
found_hi = true;
}
}
}