0

The assignment I am working on is asking me to input a number N, and then to input N strings and to check how many characters 'a' 'e' 'i' 'o' 'u' and 'A' 'E' 'I' 'O' 'U' are in each string and then to print it out. However, when the last string in the array starts with one of those characters, the program won't count the first letter of it, for some reason.

For example, this input:

3
This is a test
Hey Joe
I like pancakes

will have the output:

4
3
5

instead of:

4
3
6
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    int N,i,j,brojac=0;
    vector<string> recenice;
    string pom;
    cin>>N;

    for(i=0;i<N;i++){
        cin.ignore();
        getline(cin, pom);
        recenice.push_back(pom);
    }

    for(i=0;i<recenice.size();i++){
            for(j=0;j<recenice[i].size();j++){
                if(recenice[i][j]=='a' || recenice[i][j]=='e' || recenice[i][j]=='i' || recenice[i][j]=='o' || recenice[i][j]=='u' || recenice[i][j]=='A' || recenice[i][j]=='E' || recenice[i][j]=='I' || recenice[i][j]=='O' || recenice[i][j]=='U') ++brojac;
            }
            cout<<brojac<<endl<<endl;
            brojac=0;
    }

    return 0;
}

What is the cause of the counter not counting the first character of the last string if it fulfills the condition?

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • 2
    What would you guess that `cin.ignore();` does? – Adrian Mole Feb 23 '20 at 19:05
  • So when i use getline it doesn't work properly so i figured cin ignore is needed then, however is it making the problem here? – mexygod Feb 23 '20 at 19:06
  • oof fixed it when i erased cin.ignore(); from the for function and wrote it above the for function, thanks – mexygod Feb 23 '20 at 19:09
  • There you go! The `getline()` function *consumes* the 'trailing' newline character; but you should have it before the `for` loop, as `cin >> N` doesn't do that. – Adrian Mole Feb 23 '20 at 19:11
  • Does this answer your question? [C++ --- getline, and cin ignore () .deleting first characters in strings on output](https://stackoverflow.com/questions/55270376/c-getline-and-cin-ignore-deleting-first-characters-in-strings-on-outp) – Werner Henze Feb 24 '20 at 19:35

0 Answers0