-4

This is my code, upon compiling it is showing error in type conversion when I am calling isVowel( ) function.Can you check and tell what's the error?

#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
bool isVowel(string a)
{
    if(a == "a" || a =="e" || a =="i" || a =="o" ||a =="u"){
        return true;
    }
    else
        return false;
}

int main()
{
    int T;
    cin>>T;
    for (int i = 0; i < T; i++)
    {
        string s, snew="";
        cin>>s;
        for (int j=0;j<s.length();j++)
        {
            if(isVowel(s[j]))
                continue;
            else
                snew += s[j];
        }
    }
    return 0;
}
Saurav Bhagat
  • 55
  • 4
  • 15

3 Answers3

5

You function is expecting a string but you're passing in a char. While a string can hold a single character, it's not the same thing. The types need to match.

Change the function to expect a char, and use character constants instead of string constants for the comparisons so comparing a char with a char. Also, because you're simply returning true or false if the condition is true or false, just return the result of the comparison expression.

bool isVowel(char a)
{
    return (a == 'a' || a =='e' || a =='i' || a =='o' || a =='u');
}
dbush
  • 205,898
  • 23
  • 218
  • 273
3

Use library functions when possible:

bool isVowel( char a )
{
    return std::string( "aeiouy" ).find( a ) != std::string::npos;
}

std::copy_if( source.begin(), source.end(), std::back_inserter( target ),
    []( char c ) { return not isVowel( c ); } );

live example

Slava
  • 43,454
  • 1
  • 47
  • 90
1

For starters vowels can have upper or lower case.

Your function declaration is wrong

bool isVowel(string a);

The function should check the supplied character whether it is a vowel.

The function can be defined the following way as it is shown in the demonstrative program.

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <cstring>
#include <cctype>

bool isVowel( char c )
{
    const char *vowels = "aeiou";

    return c != '\0' && std::strchr( vowels, std::tolower( ( unsigned char )c ) );
}

int main() 
{
    std::string s( "Hello Saurav Bhagat" );
    std::string new_s;

    std::copy_if( s.begin(), s.end(), std::back_inserter( new_s ),
        std::not1( std::function<bool( char )>( isVowel ) ) );

    std::cout << s << std::endl;        
    std::cout << new_s << std::endl;        

    return 0;
}

Its output is

Hello Saurav Bhagat
Hll Srv Bhgt
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335