0

Hi

I have a function where if the first character of a string is whitespace it then deletes it.

My problem is I am getting an error on a certain line & I have no idea why & how to fix it.

error:

C++ forbids comparison between pointer and integer

Function:

void delete_whitespace(string array[], int array_size)
{

               for (int i = 0; i < array_size; i++)
	{

		if ( array[i][0] == " " )  // This is the line where the error occurs
		{
			array[i].erase(0,1);
		}
	}

	}
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by mvmalderen
0

Attempting to compare a single character with a string. Here is how to fix it -- use single quotes instead of double quotes array[i][0] == ' '

Votes + Comments
You should have suggested " "[0] ;)
0

Ah silly me :P Its so obvious now :) Thank you

I dont know why elcipse doesn't just give an error of "invadid comparision of char with string" or sumthing instead of talking about ints?

Thanks again :)

0

>why elcipse doesn't just give an error of "invadid comparision of char with string" or sumthing instead of talking about ints?

Well, actually it does something like that:

C++ forbids comparison between pointer and integer

The pointer where the compiler is complaining about was the string with the double quotes: " " , the integer where the compiler was complaining about is the character from the string where you want to compare with (In C++ a character can be implicitly converted to an integer) ...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.