question on string

got a question. y i unable to break the line when it reach ".,?" ? when i compile only the first line break.how i able to break the second one? i try alot of method is still unable to work. anyone out there Pls help me out so that i able so solve the problem.Thanks.

after compile:
We think in generalities,
but we live in details. How are u?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str="We think in generalities, but we live in details. How are u?";

string str2, str3;
size_t pos;

if ( pos = str.find_first_of(".,?"))
{
str3= str.substr(0,pos+1);

str2 = str.substr(pos+2);
}
cout << str3 <<'\n' << str2 << endl;
return 0;
}
Because that's all you are doing.

str3 is set to the first part of the string up to the comma and str2 is set to the rest of the string.
You have to repeat the find_first_of() and substr() using str2 to form two more strings
"but ... details. " and "How are u?".

ok. but my that sentence is in the text file. then i have to cin a word like you then have to display which sentence that have the you.

this is the sentence in the txt file:

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

Who are you?
Have a dog.
What about you? Do you have a dog?

output display:

How are you?
Who are you.
What about you?
Do you have a dog?

use getline(cin, std::string)

find if it has a "you". if it has, display the sentence from next to the "." or if no "." found then from the beginning to end..

keep doing till eof is not found.
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;
}

Topic archived. No new replies allowed.