Hey I am inputting this string into a this function " A toyota's a toyota". The code should count the frequency of the word and letter count and display it. The issue I am having is that is does not count them all, it misses the last word counts. Here is the snippit that I am working with.

void WordCharacter (string &input, int number) //function to see how many characters are in the words of a string and also the frequency.
{
    if (number == 1)
    {

        cout<<"\t  ----------------------------------------------------"<<endl;
        cout<<"\t  |        Here is the results of the strings        |"<<endl;
        cout<<"\t  ----------------------------------------------------"<<endl<<endl<<endl;
        cout<<"Here is your string: "<<input<<endl;
    }
    if (number == 2)
    {
        cout<<"Here is your string: "<<input<<endl;
    }
    if (number == 3)
    {
        cout<<"Here is your string: "<<input<<endl<<endl;
    }

    int word1 =0, word2 = 0, word3 = 0, word4 = 0, word5 = 0;
    int word6 =0, word7 = 0, word8 = 0, word9 = 0, word10 = 0;
    char endline = '\n';
    string temp = input;
    int punct = 0;
    int length = temp.length();

    for (int x = 0; x < length; x++)
    {
        if (ispunct(temp.at(x)))
            punct++;
    }
    int x1 = 0;
      for (int x = 0; x < length; x++)
    {
        if (!ispunct(temp.at(x)))

            temp.at(x1++) = temp.at(x);
    }
    int lengthNew = length - punct;
    temp.resize(lengthNew);

    int Count = 0;

    for (int x = 0; x < lengthNew; x++)
    {
        if (isalpha(temp.at(x)))
        {
            Count++;
        }

        else
            if (Count > 0)
                switch (Count)
                {
                    case 1:
                        word1++;
                        Count = 0;
                        break;
                    case 2:
                        word2++;
                        Count = 0;
                        break;
                    case 3:
                        word3++;
                        Count = 0;
                        break;
                    case 4:
                        word4++;
                        Count = 0;
                        break;
                    case 5:
                        word5++;
                        Count = 0;
                        break;
                    case 6:
                        word6++;
                        Count = 0;
                        break;
                    case 7:
                        word7++;
                        Count = 0;
                        break;
                    case 8:
                        word8++;
                        Count = 0;
                        break;
                    case 9:
                        word9++;
                        Count = 0;
                        break;
                    case 10:
                        word10++;
                        Count = 0;
                        break;
            }
    }


    if (word1 > 0)
        cout<<endl<<"There are "<<word1<<" 1 letter words in your string.\n"<<endl;
    if (word2 > 0)
        cout<<"There are "<<word2<<" 2 letter words in your string.\n"<<endl;
    if (word3 > 0)
        cout<<"There are "<<word3<<" 3 letter words in your string.\n"<<endl;
    if (word4 > 0)
        cout<<"There are "<<word4<<" 4 letter words in your string.\n"<<endl;
    if (word5 > 0)
        cout<<"There are "<<word5<<" 5 letter words in your string.\n"<<endl;
    if (word6 > 0)
        cout<<"There are "<<word6<<" 6 letter words in your string.\n"<<endl;
    if (word7 > 0)
        cout<<"There are "<<word7<<" 7 letter words in your string.\n"<<endl;
    if (word8 > 0)
        cout<<"There are "<<word8<<" 8 letter words in your string.\n"<<endl;
    if (word9 > 0)
        cout<<"There are "<<word9<<" 9 letter words in your string.\n"<<endl;
    if (word10 > 0)
        cout<<"There are "<<word10<<" 10 or more letter words in your string.\n"<<endl;

    if (number == 3)

    return;
}

There are a few issues with your code, however the one causing the issue is a simple OBOE (off-by-one-error). The number one thing that jumps out at me is that you really should be using an array here, it will literally make the part of the code that deals with word## 10 times smaller!

In case you are not familiar with arrays, they are (for the purposes of this discussion) a way to make a large number of variables that you can index with a number. You declare them with square brackets.

Another thing that will make your code easier to read (and thus easier to fix) would be to modulize more. Each function should do a simple task, so having one function that does multiple tasks makes it hard to read.

In the end, the WordCharacter function should end up looking like this:

//this function does the job of removing punctuation from a string
string removePunctuation(string input)
{
    string temp = input;
    int punct = 0;
    int length = temp.length();
    for (int x = 0; x < length; x++)
    {
        if (ispunct(temp.at(x)))
            punct++;
    }
    int x1 = 0;
      for (int x = 0; x < length; x++)
    {
        if (!ispunct(temp.at(x)))
            temp.at(x1++) = temp.at(x);
    }
    return temp;
}

//deal with number in main() only deal with printing the number of words of each length here
void printWordCharacter(string &input)
{
    int word[10];//declare 10 ints called word[0], word[1], etc... (note that they start at 0, not 1)
    string newString;//newString is a better name than temp
    newString=removePunctuation(input);
    int count=0;
    for (int x=0; x<newString.length(); ++x)
    {
        if (isalpha(newString.at(x)))
            count++;
        else
        {
            if (count>0&&count<11)
                word[count]++;//now wasn't that easier, arrays are awesome!
            count=0;
        }
    }
    for (int i=0; i<10; ++i)
    {
        if (word[i]>0)
            cout<<"There are "<<word[i]<<" "<<i+1<<" letter words in your string.\n"<<endl;
    }
}

Your problem, I believe, is that since you add count when you find a non-letter, and you removed punctuation, you will not ever add the last count value to your variables. For example, the phrase "Hello World!", would become "Hello World", which would load count=5, then word[4]++, then count=5, then... nothing. Adding a space to the end of your sentence or an extra check if count>0 should fix your problem.

Thanks for your help, you code does flow a lot better and is easier to read.

Lambdabeta's answer is a good example of applying the KISS (Keep It Short and Simple) to code. IE, keep it as simple as possible, but no simpler than necessary. Too simple (short) usually makes stuff hard to understand. Also, don't hesitate to add comments to the code, especially stating what your intention was when you wrote it. If someone else has to come along and fix bugs or enhance it, that can be critically useful. Comments only increase the size of the source file, but do nothing to increase the size or impact the performance of the object and executable files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.