-5

I am trying to write a code reading lines of input for how many vowels in a line. Using c++, the first input is the number of lines. In this exercise, "y" is counted as a vowel.

 #include <iostream>
#include <string.h>
using namespace std;
int main(){     
    int vowel=0;
    int a;
    cin >>a; 
    for (int b=0; b<a; b++){
    string x; 
 getline(cin,x);
 int i = x.length();
        if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u'|| x[i]=='y')
        vowel ++;
   cout<<vowel<<" "; }
}

I keep on getting the wrong amount of vowels counted. Why is this so? Sample input:

16
tiraf sliamnmkjvcms c cmj gyipdxsuxepqqsrc
dsxyukxpsgxftsrov g  hsgekuxbf mefr tpvnutzw rxky
fg esxptwwydkfnblkwhezcewvwv u rjavbzlivjc znv 
ge lewqu  x qyxy thygluvtdtjyupmbcegyvjzk  
 e  ztaggibb iq ygy pd vycvpquwdbde yy mct q
kdewjl  gjapfpg  qpwvvuokndgjadadjw ok  a ifdjsw htufxiv hbu
musoamuk kl  viipodev k lg z jhymb m dia nthkzl a
fvdtqtbett do lcsgmv  kbvo hbbd injtjrzfm n  ywmljjxwz sah
cplw y npe orbpgovcnhrigpu jiop qbfkhreewsyn
vourdqyu mrwy abwhxysj lnsjhxihtelmjbslu
dzmla jhsnbcc wocfag tlfho bmp wpyhpawesl o  zv
dvveqhkyji u azk    dgzvsoqaamjfhgjy afcesfxsfjzrp
uuyxtbntb f pqutku   zyyskprbgzfhecd wynekb 
fnshvmptsv clglwfvfkynwutmbftom qgnmxfhr uarh
n jadnpbrktavqojwstmg w liwmtfykynlkdbrus undee wzsd
qef owpgu mrsuuateshbhr mxdmrnghsqc nx rysjxtlxpqrix vfs

output: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Expected: 8 8 7 11 11 12 12 6 11 10 8 11 10 5 10 9
Goodwin Lu
  • 45
  • 1
  • 12
  • 1
    `char x[a];` This is dangerous. What happens if the user enters a large value? It may overflow the stack. You should use `std::vector` or `std::string` instead. – Neil Kirk Dec 05 '15 at 17:43
  • 1
    Fix your indentation. And what is `getline(std::char, x)` supposed to do? It makes no sense. – Lightness Races in Orbit Dec 05 '15 at 17:48
  • it is supposed to get the whole line, instead of just what could be mistinterpreted as one world. Example: input = re do, if I got the whole line, output =2, but otherwise it would do them seperately and output = 1, 1 – Goodwin Lu Dec 05 '15 at 17:53
  • You can't use `strlen` if `x` is of type `std::string`. You should use `x.length()` instead. – Thomas Matthews Dec 05 '15 at 18:28
  • Your `i` variable is uninitialized, so `x[i]` could be any location on your PC, (for example, if `i` has a value of 3256478912, you are accessing outside the range of `x`). – Thomas Matthews Dec 05 '15 at 18:30
  • Typo? You are missing a `;` at the end of the `getline` statement. – Thomas Matthews Dec 05 '15 at 18:31
  • Your variable `n` is not used; you may want to use it or lose it (get rid of it). – Thomas Matthews Dec 05 '15 at 18:31
  • Please turn up the warning level on your compiler to maximum. – Thomas Matthews Dec 05 '15 at 18:32
  • You should [transform your string to all lower case](http://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case) before comparing the characters. Upper case letters are different than lowercase. – Thomas Matthews Dec 05 '15 at 18:34
  • The current error message "error no matching function for call to 'getline(std::basic_istream&)'" means there is no overload of `getline` that takes accepts the parameter with which you are calling it. Consult the documentation for [getline](http://en.cppreference.com/w/cpp/string/basic_string/getline) and [operator >>](http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt) to learn what `getline` will accept and what `operator>>` returns. – user4581301 Dec 05 '15 at 18:51

2 Answers2

4

char is not a member of std, it is a keyword. And std::getline accepts std::string. So declare x as std::string, and pass std::cin as a first argument to std::getline.

Mikhail
  • 20,685
  • 7
  • 70
  • 146
  • You should spell it like `getline(cin, x);`. What you wrote `getline(cin>>x)` has different meaning. You call `getline` with _one_ argument, that is `cin>>x`. It has the same type as `cin`, since statement `cin>>x` actually reads a string from `cin` to `x` and returns reference to `cin`. Because of this, you can chain these reads like `cin>>x>>y`. Both `cin>>x` and `getline(cin, x)` are meant to read string (when `x` is of type string), but the former will stop on the space, while the latter will stop on the line feed. – Mikhail Dec 05 '15 at 18:40
  • @GoodwinLu In such cases it is better to see an example for a function. You usually do the following: 1. google for `std::getline`, 2. follow the first link (http://en.cppreference.com/w/cpp/string/basic_string/getline), 3. see the example section. It works 90% of the time. – Mikhail Dec 05 '15 at 18:42
  • thanks for the tips, but it doesn't seem to be able to count the lines for some reason. I don't know what's wrong. – Goodwin Lu Dec 05 '15 at 23:23
  • @GoodwinLu at least you do not iterate through the symbols of the string `x`. You only check one character `x[i]` with `i` always equal to 1. – Mikhail Dec 06 '15 at 11:18
  • hmmm... I tried to make i = x length but now it just displays "0 0 0 0 0 0..." ?!! – Goodwin Lu Dec 06 '15 at 16:57
2

http://en.cppreference.com/w/cpp/string/basic_string/getline

std::getline takes an input stream as its first parameter. You are passing std::char instead (which actually doesn't make sense, as char is a keyword).

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91