Adam Palmkvist
Adam Palmkvist

Reputation: 111

C++ comparing Char to Strings

I'm trying to loop through a word but only vowels are working for some reason. The other type prints out the amount of words in total. Please excuse my poor choise of words mixed with Swedish and English.

#include <iostream>

using namespace std;

int main()
{
    int vo = 0;
    int ko = 0;

    char vocals[7]
    {
        'A','O','U','E','I','Y'
    };


    char konsonanter[19]
    {
        'B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','X','Z'
    };

    string word = "Toysoldier";

    for(int i = 0; i < word.length(); i++)
    {
        for(int v = 0; v < vocals[v]; v++)
        {
            if(vocals[v] == word[i])
                vo++;
        }

        for(int k = 0; k < konsonanter[k]; k++)
        {
            if(konsonanter[k] == word[i])
                ko++;
        }
    }


    cout << "Konsonanter = " << ko << " Vokaler = " << vo << endl;
}

Upvotes: 3

Views: 391

Answers (4)

Useless
Useless

Reputation: 67713

You have more than one bug, so I'll just remark on all the code in order.

If you write

char vocals[7] = {'A' ... 'Y'};

you have to manually keep the array size and the initializer in sync - here, for example, you missed one (you have 6 "vocals" in your initializer for a 7-element array).

It's less error-prone to write

char vowels[] = {'A', 'E', 'I', 'O', 'U'};

and let the compiler count it for you. (I renamed it to "vowels" because that's what they're called in English, and removed 'Y' because it's not one - obviously you can stick with whatever works in Swedish.)

Similarly, this loop is wrong:

for(int v = 0; v < vocals[v]; v++)

because you're comparing v (an index into your array) with vocals[v] which is a character value. They're two different types of thing, and comparing doesn't make sense here. The loop over an array would normally be written

for (int v = 0; v < sizeof(vocals)/sizeof(vocals[0]); ++v)

so for some N-element array, the index v ranges from 0 to N-1. A simpler way is to just write

for (auto v : vocals)

where v is now a character, taking the value of each vocal/vowel one-by-one in the loop body. The compiler can figure out the array size for you.

I'd honestly re-write all your loops in this style: you don't need to manage array indices and bounds manually unless you're doing something more complicated.

On top of the loop condition bugs, your character comparisons will anyway mostly fail: the vowel/vocal and consonant arrays both contain only upper-case letters, and your string is mostly lower-case. You need to manage this, for example with

#include <cctype>

// ...
for (auto c : word) {
  for (auto v : vowels) {
    if (v == std::toupper(c)) {

Upvotes: 1

xavor
xavor

Reputation: 162

You conditions in the loops are flawed, you should use 'for (int v = 0; v < sizeof(vocals); v++)`.

Also, check your condition for a vocal and consonant: What happens if you consider lowercase chars? 'a' != 'A', so you don't count them correctly. Hint: Use toupper() or tolower() so you don't need to have excessive lists of chars.

Upvotes: 0

Fabien Viger
Fabien Viger

Reputation: 141

Your error is in the tests used in the "for" loops: v < vocals[v] will compare the integer v to the integer value of the character vocals[v], which happens to be the ASCII code of that character (in practice, that'll be something between 65 and 90, look up "Ascii table" on the web).

To fix your code, you should modify your "for" loops like this:

for (char vocal : vocals) {
  if (word[i] == vocal) vo++;
}
for (char konsonant : konsonanter) {
  if (word[i] == vocal) ko++;
}

Also, note that you got the number of vocals wrong in your "vocals" array: it's 6, not 7.

Upvotes: 0

nyronium
nyronium

Reputation: 1278

You got your loop condition wrong. Instead of v < vocals[v] you probably meant to write v < 7 as there are 7 vocals in your array.

Writing v < vocals[v] is not an error as v is initialized to zero while the expression vocals[v] is a char which can also be used in array subscripts ('A' is translated to 65, 'E' to 69 and so on).

The same problem exists in the loop for consonants.

Upvotes: 3

Related Questions