I have read some threads on this warning and I understand what the warning is and there have been suggestion on how to get around the warning, but how do you code it correctly not to get the error?
In my class we are studing namespace,typedef, string functions the assignment is to accept an input string and strip out the vowels. I'm getting the error and I'm not sure how to actually fix the code instead of getting around the warning. Thanks for any help

#include <iostream>
#include <string>

using namespace std;

string removeVowels(string userstr);
bool IsVowel(char chr);

int main ()
{
	string userstr;
	cout<< "Please provide some text:" << endl;
	cin>> userstr;
	cout<<"The text you entered is: "<< userstr <<endl;
	cout<< "Your text with vowels removed is:" << removeVowels(userstr) << endl;

	return 0;
}
	
///method for removing each vowel in the string
string removeVowels(string userstr)
{
	string::size_type len = userstr.length();
	string finalString;
//	string::size_type len;
	string::size_type counter;
	//loop through each character in the
	//provided string
	for(int i = 0; i < userstr.size(); i++)
// for ( string::size_type i = 0; i < s.length(); i++ ) {
	{
		if(!(isVowel(userstr[0]))
		{
			//it's not a vowel so we can
			//add it to our return value
			finalString += userstr[i];
		}
	}
	return finalString;
}

//method to check if the current character is a vowel
bool IsVowel(char chr)
{
	 switch (ch)
    {
    case 'A': 
    case 'E': 
    case 'I': 
    case 'O': 
    case 'U': 
    case 'Y':
    case 'a': 
    case 'e': 
    case 'i': 
    case 'o': 
    case 'u': 
    case 'y':

That warning means that std::string.size() returns an unsigned int, so line 29 is comparing an int to an unsigned int. The work around is to declare variable i as size_t, not int (assuming your compiler defines size_t as unsigned int). for( size_t i = 0; i < /* blabla */ . And that makes perfect sense because the length of a string can never be negative, so why bother using a loop counter that can be both netative and positive values? Also using unsigned int instead of int lets std::string have a lot larger strings.

Edited 6 Years Ago by Ancient Dragon: n/a

So is that when you use for(int i = 0; i < len; i++) rather than for ( string::size_type i = 0; i < len ; i++ ) {

I tried both suggestions and they both worked. Thank you both. I am having a brain blank. I am recieving the error "identifier not found" on a function call. I'm pretty sure I've run up against that be 4 but for the life of me I can't remember the answer. Can some one give me hint or nudge in the right direction.

#include <iostream>
#include <string>

using namespace std;

string removeVowels(string userstr);
bool IsVowel(char chr);

int main ()
{
	string userstr;
	cout<< "Please provide some text:" << endl;
	cin>> userstr;
	cout<<"The text you entered is: "<< userstr <<endl;
	cout<< "Your text with vowels removed is:" << removeVowels(userstr) << endl;

	return 0;
}	
///method for removing each vowel in the string
string removeVowels(string userstr)
{
	bool foundVowel;
	string::size_type len;
	string finalString;
	string::size_type counter; 
//	for(size_t i = 0; i < userstr.size(); i++)
	for(string::size_type i = 0; i < userstr.size(); i++)  //loop through each character in the provided string
		if(isVowel(userstr[0]))
		{								
		finalString += userstr[i];			//it's not a vowel so we can add it to our return value
		foundVowel = false;
		}
		else 
		{
			foundVowel = true;
		}
	return finalString;
}

//method to check if the current character is a vowel
bool IsVowel(char chr)
{
	 switch (chr)
    {
    case 'A': 
    case 'E': 
    case 'I': 
    case 'O': 
    case 'U': 
    case 'Y':
    case 'a': 
    case 'e': 
    case 'i': 
    case 'o': 
    case 'u': 
    case 'y': 

        return true;
    default: 
        return false;
    }
}

C'mon - it tells you the line, how many identifiers are on that line?

Do a search for each one of them, and figure out which isn't present.

Hint: identifiers are case sensitive.

Comments
Sharp!

I apologize for not giving the line #. Its on line 29 and I do have a function prototype b4 main and it is defined later. I can't figure out if its the way I'm calling the function or not. I've looked at the examples in the book and used their method. Its one of the those brain blanks where you know you've fixed b4 but it just won't come to mind. I have been looking at it so long that I may be missing the obvious.

Obvious!?
There's a dog gnawing your leg off, and all you can think is "where's the itch?".

Yes it's obvious - especially since I've already told you what the answer is.

Regard this as your own personal "D'oh!!!" moment when you figure it out.
Some things just have to be experienced rather than explained, and this is one of them :icon_twisted:

Thank you. I have to copy and paste sometimes to get the code right. Now on to the next challenge.

Lmao.. reading this, its interesting. It helped me out, thank you MUCH. That damn warning was driving me nuts. Got rid of it within three seconds, and now it makes perfect sense. Thank you Ancient Dragon.

I cannot believe that no one spotted the fact he uses a Y as a vowel when clearly it is not.

Since you are already using std::string, I figure to help you concise this function :

//method to check if the current character is a vowel
bool IsVowel(char chr)
{
	 switch (chr)
    {
    case 'A': 
    case 'E': 
    case 'I': 
    case 'O': 
    case 'U': 
    case 'Y':
    case 'a': 
    case 'e': 
    case 'i': 
    case 'o': 
    case 'u': 
    case 'y': 
 
        return true;
    default: 
        return false;
    }
}

into this :

bool isVowel(char letter){
 string vowels = "AEIOUYaeiouy";
 return vowels.find(letter) != string::npos;
}
This article has been dead for over six months. Start a new discussion instead.