i cant figure where i go wrong. the output is wrong. anyone out there can find out where my mistake at? really need yr help. thanks.

My txt file the sentence are :

How are you? We think in generalities, but we live in details. what about you?

Who are you?
Have a dog.
i got. Do you have a dog?
i have dog.

- cin >> you
output is :
how are you?
We think in generalities, but we live in details. what about // error should display what about
you ?
Who are you?
i got. // error
Do you have a dog?

- cin >> dog
output is:
have a dog.
i got. do you have a dog? // error "i got" not to be display
i have dog.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX_FILE = 50;

int main()
{
string line, str1, str2, str3;

string wordSearch;

size_t found ;
size_t pos=0, pos1=0 ;

char fileName[MAX_FILE];

cout <<"Enter the filename:";

cin >> fileName;

fflush(stdin);

cout << endl;

ifstream fin;

fin.open (fileName);

if(!fin.good())
{
cerr << "File not found" << endl;

system("pause");

exit(1);
}

cout <<"Word Search:";

cin >> wordSearch;

fflush(stdin);

cout << endl;

while (!fin.eof())
{

getline(fin,line);

for(int i=0; i < line.length(); i++)
line[i] = tolower(line[i]);

for(int i=0; i < wordSearch.length(); i++)
wordSearch[i] = tolower(wordSearch[i]);

line.append(str1); // join the sentence to a line

found = line.find(wordSearch); // search for cin words

if(found!= string:: npos)
{

pos = line.find_first_of(".!?", pos+1);
pos1= line.find_first_of(".!?",pos+1);

str2= line.substr(0,pos+1);
str3 = line.substr(pos+1, pos1+1);
cout << str2 << endl << str3 <<endl;

}

}

fin.close();

return 0;
}

<snip plug]

You can't figure out where you go wrong ?

What is © cplusplus.com, 2000-2009 - All rights reserved - v2.2.1 this ?

line 20: the >> operator will not allow you to put spaces in the filename, such as path with spaces

line 22: fflush(stdin) is non-standard and not supported by may compilers. Unfortunately, there is no standard way to flush stdin. But if you use std::cin you can use cin.ignore(1000,'\n'); line 41: similar to line 22. use getline() if you want to include spaces.

line 47: don't use eof() like that because it doesn't work the way you think it does. Instead, control the loop with the return value of getline() while( getline(fin, line) ) lines 52-53: you can replace that with this one line transpose(line.begin(), line.end(), line.begin() toupper); from <algorithm> header file

line 55-56: same as above

line 58: what is str1? I don't see it has been set to anything. And to append one std::string to another just use its += operator line += str1; lines 60-70: I have no idea what those lines do.

for line 60 to 70 . i need cin a word . and have to read from txt file to fine the word from every single line. if found, display sentence that have the cin word with a newline.

original txt frm txt file:
How are you? We think in generalities, but we live in details. what about you?

Who are you?
Have a dog.
i got. Do you have a dog?
i have dog.

like - cin >> you
output is :
how are you?
what about you?
you ?
Who are you?
Do you have a dog?

This article has been dead for over six months. Start a new discussion instead.