Home > Enterprise >  operand types are incompatible ("int" and "char **")
operand types are incompatible ("int" and "char **")

Time:04-20

Id like to display the amount of vowels/consonants in my program but I'm getting an error in my for loops as mentioned in the title. How would I go about this?

heres my code:

#include <iostream>
#include <string>
using namespace std;
using std::cout;
using std::endl;

int main()
{
    //vowels, consonants and reverse quote
    string quote = "Four Score and Seven Years Ago";
    char* vowels[] = {"o", "u", "e", "o", "e", "A", "e", "e", "Y", "e", "a", "A"};
    char* consonants[] = { "F", "r", "S", "r", "c", "d", "S", "e", "v", "e", "n", "Y", "r", "s", "g"};
    char* reverse[] = {"o", "g" "A", "", "s", "r", "a", "e", "Y", "", "n", "e", "v", "e", "S", "", "d", "n", "A", "", "e", "r", "o", "c", "S", "","r", "u", "o", "F",};
    int vowelnum = 12;

    // displays the cononsants and vowels of the qoute
    cout << "Processing the quote: " << quote << endl;
    cout << "........" << endl;
    cout << "Finding the vowels and consonants..." << endl;
    cout << "........" << endl;
    cout << "The vowels of " << quote << "are:" << vowels << endl;
    cout << "The consonants of " << quote << "are:" << consonants << endl;

    // displays the total amount of vowels
    for (int i = 1; i <= vowels;   i) {
        cout << "Number of vowels: 12" << endl;
        cout << "  " << endl;
    }

    // displays the total amount of consonants
    for (int i = 1; i <= consonants;   i) {
        cout << "Number of consonants: " << endl;
        cout << "  " << endl;
    }

    // displays the quote in reverse
    cout << "........" << endl;
    cout << "In the quote in reverse is: " << reverse << endl;

    return 0;
}

CodePudding user response:

In this i <= vowels, i is an integer and vowels is an array of pointers to char, which gets represented as the address of the first array member, i.e. a pointer to a pointer to char.

Those two cannot meaningfully be compared. If they could, you'd most likely have many more iterations in that loop than would make sense. This is what the compiler is trying to tell you.

Putting aside the fact that you are using very C-style constructs...

You probably want to loop over all members of the array of vowels (though I have no idea what you try to do inside the loop). To get the number of entries in that array, you could use sizeof(vowels)/sizeof(vowels[0]) instead of vowels.

CodePudding user response:

Your code makes no effort to do what it is clearly intended to do, ie count the number of vowels and consonants in a sentence.

Maybe you are working your way to it, step by step. So here is why your current code wont compile

for (int i = 1; i <= vowels;   i) {
    cout << "Number of vowels: 12" << endl;
    cout << "  " << endl;
}

You need

int vlen = sizeof(vowels)/sizeof(vowels[0]);
for (int i = 0; i < vlen; i  ){
     // vowels[i] is the ith vowel in the list
     // so you can do
     size_t off = sentence.find(vowel[i]);
     // to see if sentence contains that vowel
}

This is not the right way to go about counting the total number of vowels but it a least gets you going

How to actually tackle the problem.

  • Work your way down sentence a character at a time
  • look to see if the character is a vowel or a consonant
  • increment a vowel or consonant counter depending on the answer
  •  Tags:  
  • c
  • Related