简体   繁体   中英

how to get loop to end loop using strcmp

I am a student who is new to programming and am seeking some help with my program. I am writing a code for a C++ class that has the user enter a word that is compared using 2 arrays to count how many of each letter is in the word as well as differentiates between upper and lower case. I am having trouble getting the loop to end. User is supposed to enter a terminating word such as " End It" to end the loop. As of now, the loop just counts "End" and "It" separately and asks for the next user input. Would anyone be able to help me with this problem? The rest of the program runs properly, just getting it to end is the problem.

***
#include <iostream>
#include <cstring>
using namespace std;
void InputWord(char Word[]);
void CountOccurrences(char Word[], char Letter[], int LetterCount[]);
int main()
{
    char Word[60];
    char End[] = "End It";
    do
    {       
        if (strcmp(Word,End))
        {
            char Letter[52] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z' };
                int LetterCount[52] = { 0 };

                cout << " LETTER COUNTS " << endl;
                cout << "************************************************" << endl;
                InputWord(Word);
            cout << "------------------------------------------------" << endl;
            CountOccurrences(Word, Letter, LetterCount);
            cout << "************************************************" << endl;
            cout << endl;
        }
    } while (Word);
return 0;
}
void InputWord(char Word[])
{
    cout << "Enter the word to be letter counted: ";
    cin >> Word;    
}

void CountOccurrences(char Word[], char Letter[], int LetterCount[])
{
    int Index = 0;
    while (Word[Index] != '\0')
    {
        if (Word[Index] >= 'A' && Word[Index] <= 'Z' || Word[Index] >= 'a' && Word[Index] <= 'z')
        {
            for (int Count = 0; Count < 52; Count++)
            {
                if (Word[Index] == Letter[Count])
                {
                    LetterCount[Count] = LetterCount[Count] + 1;
                }
            }
        }
        Index++;
    }
    int Length;
    Length = strlen(Word);
    int CharCount = 0;
    for (int Index = 0; Index < 52; Index++)
    {
        if (LetterCount[Index] >= 1)
        {
            cout << "There are " << LetterCount[Index] << " " << Letter[Index] << "'s"
                << endl;
            CharCount++;
        }
    }

    cout << "------------------------------------------------" << endl;
    cout << "There is a total of " << Length << " characters in the word " << Word << endl;

}

> strong text

***

Since "End It" is not a word, the word input can never possibly be "End It". You can change the ending word to "EndIt" and fix the check like this:

            InputWord(Word);
            if (strcmp (Word, End) == 0)
                break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM