简体   繁体   中英

Character count ignoring first characters in multiple strings

#include <iostream>
using namespace std;

int main()
{
    string sentence;
    int countv = 0, countc = 0, countspace = 0, number, s = 1;
    cout << "How many sentence would you like to check? - ";
    cin >> number;
    
    while(s <= number)
    {
        cout << "\nSentence " << s << ":";
        cin.ignore();
        getline(cin, sentence);
        
        for(int i = 0; i < sentence.length(); i++)
        {
            if(sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' || sentence[i] == 'u')
                countv++;
            else if(isspace(sentence[i]))
                countspace++;
            else
                countc++;
        }
        cout << "\nSentence " << s << " result:";
        cout << "\nThere are " << countv << " vowels in the sentence.";
        cout << "\nThere are " << countc << " consonants in the sentence.";
        cout << "\nThere are " << countspace << " whitespace in the sentence.";
        countc = 0, countv = 0, countspace = 0;
        s++;
        cout << "\n";
    }
}

I'm trying to count the number of vowels and consonants in multiple strings but, for some reason, it only gives the correct output for the first string.

I've noticed that, for the next strings (2nd, 3rd and so on), it does not count the first letter of the string. Why is this?

The code is ignoring the first character of each of the strings after the first because you are telling it to ignore those on input … with the call to cin.ignore() . That call should not be inside the loop but immediately before it, so as to ignore the newline that is left in the input stream after the cin >> number extraction.

Note that the call to getline does not leave that newline in the stream's buffer; see this cppreference page :

… the next available input character is delim , as tested by Traits::eq(c, delim) , in which case the delimiter character is extracted from input , but is not appended to str .

Here's a suitably modified version of your code:

#include <string>
#include <iostream>
using std::cin, std::cout;

int main()
{
    std::string sentence;
    int countv = 0, countc = 0, countspace = 0, number, s = 1;
    cout << "How many sentence would you like to check? - ";
    cin >> number;
    cin.ignore(); // Call ignore HERE (once) to skip the newline left in the buffer from the above!

    while (s <= number)
    {
        cout << "\nSentence " << s << ":";
     //  cin.ignore(); WRONG!
        std::getline(cin, sentence);

        for (size_t i = 0; i < sentence.length(); i++)
        {
            if (sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' || sentence[i] == 'u')
                countv++;
            else if (isspace(sentence[i]))
                countspace++;
            else
                countc++;
        }
        cout << "\nSentence " << s << " result:";
        cout << "\nThere are " << countv << " vowels in the sentence.";
        cout << "\nThere are " << countc << " consonants in the sentence.";
        cout << "\nThere are " << countspace << " whitespace in the sentence.";
        countc = countv = countspace = 0; // Nicer than the dodgy use of the comma!
        s++;
        cout << "\n";
    }
}

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