6

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.

5
  • My mistake. Thank's for catching that, I didn't obviously haha.
    – Robolisk
    Jan 23, 2012 at 1:54
  • I don't understand why I have -2? What have I done wrong D:?
    – Robolisk
    Jan 23, 2012 at 2:10
  • 2
    ¤ As long as you are assuming a restriction to the English alphabet (if you are), I don't see anything technically wrong. However, using integers as boolean flags, instead of just using C++ bool variables, and e.g. using printf instead of C++ iostreams (in a beginner's program), seems to indicate that you have the burden of having first learned C. And if so, then you need to focus on unlearning C, and learning C++, which is a different language. Oh, by the way, could it be that you tested the code with input that didn't have digits? Then space would not be detected. Cheers & hth., Jan 23, 2012 at 2:14
  • 2
    you didn't do anything wrong. it's just that the cost of having Stack Overflow available as a helpful resource for people of all ages, is that people of all ages not only use Stack Overflow but also insist on using its "management" features such as voting. so, you have children (or people with children's minds) downvoting anything they don't understand, or where they detect what seems to them to be negativity or unclarity, and so on. it's just the cost. the best (if that were possible) would be to just ignore it. ;-) Jan 23, 2012 at 2:41
  • @AlfP.Steinbach Thanks for all the information. Makes sense about the voting system. And yes, I have learned C first (through school) and now I am in C++, which is the semester. This is our first assignment (well a part of it) and it is quite confusing. Techinically, we havn't learned any of this "isspace" or "isdigit" stuff, this is me googling ahead and finding this things. I'll take your advice and try and "unlearn c" when learning C++. (:
    – Robolisk
    Jan 23, 2012 at 3:32

2 Answers 2

15

All that is very unnecessary to just test if a string has a space in it. This is all you need:

#include <ctype.h>

bool hasspace = std::find_if(str.begin(), str.end(), ::isspace) != str.end();

:: is the scope resolution operator specifying that isspace is a global function, not the similarly-named std::isspace, and find_if is a function inside std::. If you use using namespace std; then you don't need std:: but you do still need the plain ::.

The find_if function 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 a bool. find_if iterates 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 returns true, find_if returns the iterator that caused the function to return true. If find_if goes through to the end and the function never returns true, then it returns an iterator to the end of the range, which in this case is str.end().

That means that if find_if returns str.end(), it got to the end of the string without isspace returning true, which means there was no space characters in the string. Therefore, you can test the result of find_if against str.end(); If they are unequal (!=), that means there was a space in the string, and hasspace is true. Else, hasspace is false.

13
  • See now that. What you told me was way to complicated and what you just showed me is "easier" looks hella more complicated to me. I don't even understand whats going. First of, what does "::" do? Second, I don't understand what "std::string::npos;" is doing, or whats going on in their (considering I don't know what :: does).
    – Robolisk
    Jan 23, 2012 at 1:56
  • @MichaelGoldshteyn thanks for catching that, that one always confuses me. Jan 23, 2012 at 1:57
  • 3
    @Robsta: I think it's seriously time you sat down with a good C++ book before resuming this problem. Trust me, you'll do yourself a huge favour.
    – Kerrek SB
    Jan 23, 2012 at 1:57
  • 2
    @Robsta then this should be tagged homework. Read the updated answer for an explanation of the code. Jan 23, 2012 at 2:01
  • 3
    The problem with the C issspace() function is that "the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined." If plain char is signed, then a string could contain negative values. You need to convert the value to unsigned char before passing it to isspace(). (Some implementations handle this, but you shouldn't depend on it.) Jan 23, 2012 at 21:08
0

here is another way, if the above version seems strange, or it's above your knowledge

if(marks[i] == ' ') {
cout<<"Space found!";
}
1
  • 5
    That's not equivalent. isspace() checks whether its argument is any whitespace character; that includes the space character as well as '\f', '\n', '\r', '\t', and '\v' (and possibly others, depending on the locale). Jan 23, 2012 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.