I need to create a txt file, then write a program that reads in file lines, counts the number of declarative sentences and finds a declarative sentence in file 3. The program must be written without vectors. For the narrative, we take a sentence that ends with a period. Take into account the ellipsis is not necessary. To find a sentence in file 3, it means to single it out with a star on either side or slash'ami.

For example:

1 sentence.
2 sentence.
3 sentence?
/ \ 4 sentence ./ \

  • And what's the problem? In creating the file? In the opening? - Vladimir Martyanov
  • The problem is to read in rows, then count the number of narratives and finding the 3rd narrative. - Nejdan
  • Why not clarify the question and remove from it the extra problems that do not exist. Besides, your “question” is actually two questions - Vladimir Martyanov
  • I apologize for the double question, but I'm a little desperate) I reduced the question a bit, removing the extra information that is not relevant to the task. - Nejdan
  • For the future I will consider, I will create a more meaningful title. With line reading I will try to understand. - Nejdan

1 answer 1

I think my decision can help:

#include <iostream> #include <string> #include <fstream> using namespace std; int main() { string currentSentence = ""; char ch; int sentenceCounter = 0; ifstream infile("test.txt"); // your file while(infile) { infile.get(ch); /* Если текущий символ - точка, то наше предложение утвердительное Если счетчик меньше 2, значит увеличиваем счетчик и идем дальше */ if((ch == '.') &&(sentenceCounter < 2)) { sentenceCounter++; continue; } /* Если мы уже встретили две точки в файле, то сейчас будет третье предложение. Нужно учесть, что могут попадаться ! или ?. Если они попадаются, то обнуляем строку и считываем файл дальше пока не дойдем до конца или до точки. */ if(sentenceCounter == 2) { if((ch != '!') && (ch != '?') && (ch != '.')) { currentSentence+=ch; continue; } if(ch == '.') { cout<<currentSentence; break; } //очищаем, если встретился восклицательный или вопросительный знак if((ch == '!') || (ch == '?')) { currentSentence.erase(); continue; } } currentSentence += ch; } return 0; } 

The truth here is that the sentence is not marked with asterisks, it is displayed in the console for ease of example. I think it will not be difficult to rewrite the proposal for the selection of stars.

Tested using test1 . test2 ? test3 ! test4. test5? test6. test7? as an example test1 . test2 ? test3 ! test4. test5? test6. test7? test1 . test2 ? test3 ! test4. test5? test6. test7? , correctly displays test6 .

For such punctuation marks as a dot, exclamation point and question mark, everything works. You should add a program to recognize the ellipsis. And do not forget about the support of Russian characters.