I have to write a function that reads a string and outputs only the consonants. example: string: adadad output: ddd

I wrote a program without the function and it works. Now I'm trying to change the program to use the function. As of now my only error is: error writing to program database. Any suggestions???

Thanks in advance!!

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;


string Consonants;
string Input;
int Index = 0;
string StripVowels(string Consonants);

int main()
{
	//Step01: Declare Memory Locations
	//Step02: Initialize Memory Locations
	//Step03: Do the Work
	//Step03a: Ask for Input
	cout << "Please enter text: " << endl << endl;
	cin >> Input;
	cout << endl;
	//Stepo3b: Compare chars
	while(Index < Input.size()){
		StripVowels(Input);
		}
	//Step04: Wrap up and Exit
	cout << endl;
	getch();
	return 0;
}

		string StripVowels(string Consonants){
			if(Consonants.substr(Index,1) == "a")
				{return false;
					Index++;}
			else if(Consonants.substr(Index,1) == "e")
				{return false;
					Index++;}
			else if(Consonants.substr(Index,1) == "i")
				{return false;
					Index++;}
			else if(Consonants.substr(Index,1) == "o")
				{return false;	
					Index++;}	
			else if(Consonants.substr(Index,1) == "u")
				{return false;	
					Index++;}	
			else
				{return Consonants.substr(Index, 1);
					Index++;}
			}

Your StripVowels is way overkill. At least it would be if it weren't filled with type mismatches and unnecessary member function calls:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

bool is_vowel ( const char c );
void StripVowels ( string& s );

int main()
{
  string s;

  cout<<"Enter a string: ";
  cin>> s;

  StripVowels ( s );

  cout<< s <<endl;
}

bool is_vowel ( const char c )
{
  return string ( "aeiouAEIOU" ).find ( c ) != string::npos;
}

void StripVowels ( string& s )
{
  s.erase ( remove_if ( s.begin(), s.end(), is_vowel ) - s.begin() );
}

Of course, if you still want to do it the hard way, a loop is your best bet:

#include <iostream>
#include <string>
using namespace std;


string Consonants;
string Input;
string::size_type Index = 0;
char StripVowels(string Consonants);
string Done;

int main()
{
  //Step01: Declare Memory Locations
  //Step02: Initialize Memory Locations
  //Step03: Do the Work
  //Step03a: Ask for Input
  cout << "Please enter text: " << endl << endl;
  cin >> Input;
  cout << endl;
  //Stepo3b: Compare chars
  while(Index < Input.size()){
    Done += StripVowels(Input);
  }
  //Step04: Wrap up and Exit
  cout << Done << endl;

  return 0;
}

char StripVowels(string Consonants){
  while ( Consonants[Index] == 'a' || Consonants[Index] == 'e'
    || Consonants[Index] == 'i' || Consonants[Index] == 'o'
    || Consonants[Index] == 'u' )
  {
    Index++;
  }

  return Consonants[Index++];
}

I'm sort of limited in what I'm supposed to do.

Done += StripVowels(Input);

I've tried:

Answer = StripVowels(Input);

then: cout << Answer;

but that doesn't work. What does the '+=" do that '=' doesn't do?
And what type mismatches?

Thanks

>And what type mismatches?
Well, let's start with false not being a string.

>I've tried:
>Answer = StripVowels(Input);
And it didn't work, because it's wrong. Your code suggests that StripVowels only removes the next vowel in the string, not all of them. My second solution maintained that assumption.

>What does the '+=" do that '=' doesn't do?
It appends rather than replaces.

>I'm sort of limited in what I'm supposed to do.
So tell us exactly what you're supposed to do. The problem can be solved as simply as this:

void StripVowels ( const string& s )
{
  for ( string::size_type i = 0; i < s.length(); i++ ) {
    if ( !is_vowel ( s[i] ) )
      cout<< s[i];
  }
}

But your code made it seem as if you were required to modify the string rather than just do straight output. I can't help much if you don't give me something reasonable to work with.

This article has been dead for over six months. Start a new discussion instead.