0

I need to find the sentence from the file, which had the greatest number of characters, but I can't find the index for start the sentences( I know that my problem is in this part

(if ((n == '?')||(n == '!')||(n == '.')) {
            newIndex = index;
}

But I don't know how to improve it. The part below is the rest of my code.

using namespace std;
int main()

{
    int newIndex = 0;
    int startSentensec = 0;
    int endSentenses = 0;
    int index = -1;
    int count = 0;
    const string wayToFile = " " ;
    int total = 0;
    char n;
    vector<char>MyVector;
    ifstream file(wayToFile);
    while (!file.eof())
    {
        file >> n;
        if (file.eof()) break;

        index += 1;
        MyVector.push_back(n);
         cout << n << " ";
       //123
        if ((n == '0' ) ||(n == '1' ) ||(n == '2' ) ||(n == '3' ) ||(n == '4' ) ||(n == '5' ) ||(n == '6' ) ||(n == '7' ) ||(n == '8' ) ||(n == '9' )) {
                                count += 1;
                                        }
        ///456
        if ((n == '?')||(n == '!')||(n == '.')) {
            newIndex = index;
            if (count >= total){

                    total = count;
                    endSentenses = index;
                    startSentensec =   newIndex ;
                    count = 0;

        }
        }
            }




    file.close();
    cout << endl<< "Sentences with the greatest number of digits :";
        for (int i = (startSentensec); i <= endSentenses; i++){
            cout << MyVector[i];
        }
    cout << endl;
    cout  << total << endl;


}
RithvikK
  • 113
  • 13
купуп
  • 11
  • 1
  • 1
    It's time to learn about the [`switch`](http://en.cppreference.com/w/cpp/language/switch) statement. – tadman Mar 05 '18 at 18:09
  • 1
    Not to mention [these](http://en.cppreference.com/w/cpp/header/cctype) – Useless Mar 05 '18 at 18:09
  • Anyway, the start of a sentence is only marked by the end of the previous one. So what's your actual, specific problem? – Useless Mar 05 '18 at 18:10
  • 1
    Please read about [mcve]. Your question should include the input, expected and actual output. It would be much easier if you remove the `ifstream` stuff and instead use hardcoded data for the example – 463035818_is_not_a_number Mar 05 '18 at 18:16
  • Welcome to SO. You need to tell use what makes the start and end of a sentence. From your code it looks as if it has something to do with the letters `?`, `!` and `.` but you must describe that in the question. Further it is unclear why you check for `0`, `1`, ... What is the purpose of that. In short - please edit your question and describe the rules.... – 4386427 Mar 05 '18 at 18:32
  • You manage to avoid some of the problems outlined in [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), but you fail to address a failure that is not end of file and may end up with an endless loop spewing garbage. – user4581301 Mar 05 '18 at 18:42

2 Answers2

2

This part:

    if ((n == '?')||(n == '!')||(n == '.')) {
        newIndex = index;
        if (count >= total){

                total = count;
                endSentenses = index;
                startSentensec =   newIndex ;
                count = 0;
        }
    }

has a major problem. See the inline comments

    if ((n == '?')||(n == '!')||(n == '.')) {
        newIndex = index;   // Here newIndex is set to the same as index
        if (count >= total){

                total = count;
                endSentenses = index;          // So these two lines will make
                startSentensec =   newIndex ;  // endSentenses and startSentensec identical
                count = 0;
        }
    }

Don't update newIndex until you have checked for a new max value.

Like:

    if ((n == '?')||(n == '!')||(n == '.')) {
        if (count >= total){

                total = count;
                endSentenses = index;
                startSentensec =   newIndex ;
                count = 0;
        }
        newIndex = index;
    }

Regarding some improvement - this

    if ((n == '0' ) ||(n == '1' ) ||(n == '2' ) ||(n == '3' ) ||(n == '4' ) ||(n == '5' ) ||(n == '6' ) ||(n == '7' ) ||(n == '8' ) ||(n == '9' )) {
        count += 1;
    }

can be written

    if ((n >= '0' ) && (n <= '9' )) {
        count += 1;
    }
4386427
  • 33,845
  • 4
  • 32
  • 53
1

Changing this

endSentenses = index;
startSentensec = newIndex;

to this

startSentensec = endSentenses + 1;
endSentenses = index;

seems like an improvement to me. In other words the start of a sentence is one past the end of the previous sentence.

You should also change

int endSentenses = 0;

to

int endSentenses = -1;

So the first sentence will start at index zero.

john
  • 71,156
  • 4
  • 49
  • 68