I’m trying to figure out how to use this function. I found it on the web and apparently it checks if you have a space in your string. So it’s not working out for me. I’ve figured out that I’m not even getting into the if statement that I need to.
for (i=0;i < marks.length();i++)
{
if (isdigit(marks[i]))
{
floatMARK = 1;
}
else
{
charMARK = 1;
}
}
if (floatMARK == 1)
{
printf("were in.");
for (i=0;i < marks.length();i++)
{
if (isspace(marks[i]))
{
multiMARK = 1;
printf("WE HAVE A SPACE!!");
}
}
}
Anyone know what I’m doing wrong? If you need me to clarify anything, let me know.
All that is very unnecessary to just test if a string has a space in it. This is all you need:
::is the scope resolution operator specifying thatisspaceis a global function, not the similarly-namedstd::isspace, andfind_ifis a function insidestd::. If you useusing namespace std;then you don’t needstd::but you do still need the plain::.The
find_iffunction takes an iterator to the beginning of the string, an iterator to the end of the string, and a function that takes an argument and returns some value convertible to abool.find_ifiterates from the first iterator to the second iterator, passing each value of the current item to the function you gave it, and if the function returnstrue,find_ifreturns the iterator that caused the function to returntrue. Iffind_ifgoes through to the end and the function never returnstrue, then it returns an iterator to the end of the range, which in this case isstr.end().That means that if
find_ifreturnsstr.end(), it got to the end of the string withoutisspacereturningtrue, which means there was no space characters in the string. Therefore, you can test the result offind_ifagainststr.end(); If they are unequal (!=), that means there was a space in the string, andhasspaceistrue. Else,hasspaceisfalse.