-1

My compiler is Code::Blocks. I am trying to eliminate vocals from a character sequence.

#include <iostream>
#include <cstring>
using namespace std;
char sir[256];
int i=0;

int main (){
cout<<"sir=";cin.getline(sir,256);
for (i=strlen(sir);i>0;i--){
    if (strstr(sir[i],'aeiou')==0){
        strcpy(sir+i,sir+i+1);
        break;}}
cout<<"sir="<<sir<<"\n";
return 0;
}

I receive the following error:

error: call of overloaded 'strstr(char&, int)' is ambiguous
note: candidates are:
note: char* strstr(char*, cost char*) near match

But I think the problem is on strstr command...

manuell
  • 7,528
  • 5
  • 31
  • 58
  • 3
    'aeiou' is not a string-literal. you mean "aeiou" but that also would be wrong because it would find all of them not each of them. – Bernd Elkemann Dec 14 '13 at 13:44
  • What is 'aeiou'? If it is a substring you should use double quotes around it – dkrikun Dec 14 '13 at 13:47
  • 2
    `strstr` looks for a substring; in the C library you would probably want `strchr`, but in C++ the functionality is called `find_first_of`. – Potatoswatter Dec 14 '13 at 13:50
  • 1
    The question is: ***Why are you trying to use old C strings in C++, having the beautiful `std::string` class?*** – Manu343726 Dec 14 '13 at 15:16

4 Answers4

1

'aeiou' is not a string literal in c/c++ use "aeiou". In c/c++ string literal are represented inside " "(double quotes)

Read more here

Dangling_pointer
  • 4,633
  • 2
  • 14
  • 16
0

So, apparently, the idea is to remove vowels. As others have said, use "aeiou" and not 'aeiou'. But you also need to use the right function to check whether you have a vowel. That's strchr(const char* s, int c), not strstr. strchr looks for an occurrence of c in the string that s points to, and returns a pointer to that occurrence, or, if it's not found, a pointer to the terminating nil character. So the test in the original code should be:

if (*strchr("aeiou", sir[i] != '\0')

Personally, I'd write this a bit more succinctly:

if (*strchr("aeiou", sir[i]))
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
0

As I wrote in the first comment, the expression

strstr(sir[i],'aeiou')

is wrong for two reasons: ' is for single characters, " is for strings, but the main reason is, that strstr finds the occurance of the whole thing, not of the characters separately.

Try this:

#include <iostream>
#include <cstring>
using namespace std;
char sir[256];
char sir2[256];
int i=0;

int main (){
    cout<<"sir=";cin.getline(sir,256);
    char* reader = sir;
    char* writer = sir2;
    while(*reader) {
        if(*reader!='a' && *reader!='e' && *reader!='i' && *reader!='o' && *reader!='u') {
            *writer = *reader;
            ++writer;
        }
        ++reader;
    }
    *writer = '\0';
    cout<<"sir="<<sir2<<"\n";
    return 0;
}
Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
0

ststr is defined by two function prototypes

const char* strstr( const char* str, const char* target );

      char* strstr(       char* str, const char* target );

your call is calling as

strstr(sir[i],'aeiou')

the first arg is a char type, not a char * type, so the compiler does know how to map that to const char * or char *

Also check your loop index as

 i=strlen(sir) 

will over index the char array and

i > 0 

will NOT access the last character.

Tim Child
  • 2,994
  • 1
  • 26
  • 25