Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple 'for' loop crashing program on final iteration

Tags:

c++

loops

The below program seems to be crashing at the very end each time, and I'm assuming that this is because once I get to i = (size-1) then wordList[i+1] won't return anything, returns null, or something equivalent. Any way around this? Am I correct that this is the source of my problem?

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>

using std::cin;
using std::cout;            using std::endl;
using std::sort;
using std::string;          using std::vector;

// Obtain a list of words and return the de-duped list
// with an accompanying word count
int main()
{
    cout << "Enter a series of words separated by spaces, "
            "followed by end-of-file: ";

    vector<string> wordList;
    string x;
    while (cin >> x)
          wordList.push_back(x);

    typedef vector<string>::size_type vec_sz;
    vec_sz size = wordList.size();
    if (size == 0) {
       cout << endl << "This list appears empty.  "
                       "Please try again."  << endl;
       return 1;
    }

    sort(wordList.begin(), wordList.end());

    cout << "Your word count is as follows:" << endl;
    int wordCount = 1;
    for (int i = 0; i != size; i++) {
        if (wordList[i] == wordList[i+1]) {
           wordCount++;
           }
        else {
             cout << wordList[i] << "    " << wordCount << endl;
             wordCount = 1;
             }
         }
    return 0;
}
like image 705
IanWhalen Avatar asked May 23 '26 15:05

IanWhalen


1 Answers

Two problems.

First, it's generally better to loop till your index is < your size, instead of using !=. What you have should still work here though.

The problem is this line:

if (wordList[i] == wordList[i+1])

You need to either stop your loop one earlier (i < size - 1) OR change your if statement so that it only checks wordList[i] == wordList[i+1] when there is at least one more entry in the array.

An easier way might be to loop from i = 1 to i < size, and check wordList[i-1] == wordList[i]:

for(int i = 1; i < size; i++) {
  if(wordList[i - 1] == wordList[i]) {
    /* same as before */
  }
  /* everything else the same */
}

That will prevent you from needing extra if statements but will still protect you from leaving the bounds of the array.

EDIT:

As mentioned in the comments, if you use all the original code and just change the loop like I mentioned there will be an off-by-1 error, but it's easy to fix.

Start the word count at 1 instead of 0. You know that you'll always have at least one unique word. The loop will increment that count any time it sees a new word, so you'll end up with the proper count.

The only extra handling you'll need is for when the wordList is empty. So you'll need one quick check to see if the word list is empty, and in that case the count is 0.

like image 61
Herms Avatar answered May 26 '26 03:05

Herms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!