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.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
Robolisk
  • 1,682
  • 5
  • 22
  • 49
  • My mistake. Thank's for catching that, I didn't obviously haha. – Robolisk Jan 23 '12 at 01:54
  • I don't understand why I have -2? What have I done wrong D:? – Robolisk Jan 23 '12 at 02: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., – Cheers and hth. - Alf Jan 23 '12 at 02: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. ;-) – Cheers and hth. - Alf Jan 23 '12 at 02: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 '12 at 03:32

2 Answers2

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 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 '12 at 01:56
  • @MichaelGoldshteyn thanks for catching that, that one always confuses me. – Seth Carnegie Jan 23 '12 at 01:57
  • @KerrekSB yes, that's what I meant, fixed – Seth Carnegie Jan 23 '12 at 01: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 '12 at 01:57
  • @Kerrek SB: No time to do that. Got this assignment due in a few hours. Must complete. – Robolisk Jan 23 '12 at 02:00
  • 2
    @Robsta then this should be tagged homework. Read the updated answer for an explanation of the code. – Seth Carnegie Jan 23 '12 at 02:01
  • @SethCarnegie my mistake. Didn't know I could tag homework in here. Learned a bunch of new stuff it seems haha. Well. I'm glad you showed me that, but out of curiosity, why wasn't my isspace working? – Robolisk Jan 23 '12 at 02:09
  • @Robsta you're only checking for spaces if the string has digits in it. – Seth Carnegie Jan 23 '12 at 02:11
  • -1: This will not compile because `std::isspace` is a template name (§22.3.3.1/1). You need `::isspace` from ``, which is ugly because it is in the global namespace, or you need to take `std::isspace` and `std::bind` a `std::locale` object to it, which is a pain. – Potatoswatter Jan 23 '12 at 03:55
  • @Potatoswatter thanks, fixed, my mistake, I forgot the ``. For things like this, feel free to edit my answer directly. – Seth Carnegie Jan 23 '12 at 03:57
  • @Seth: sorry, my (pre-edit) correction was still incorrect. The template is actually just overloading the function you want to use… specifying `` just gets you to the wrong place. I'll edit to use my other suggestion, but really it's not so clear-cut. – Potatoswatter Jan 23 '12 at 03:58
  • 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.) – Keith Thompson Jan 23 '12 at 21:08
  • @KeithThompson: I've asked related question: [what does “representable” mean in C11?](http://stackoverflow.com/q/25776824/4279) – jfs Sep 11 '14 at 01:52
0

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

if(marks[i] == ' ') {
cout<<"Space found!";
}
ddacot
  • 1,212
  • 3
  • 14
  • 40
  • 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). – Keith Thompson Jan 23 '12 at 20:58