0

I know that I can check if a "word" is all letters by:

bool checkAlpha(string str){
    bool retVal;
    for(int i = 0; i < str.size(); i++){
        if(isalpha(str[i]) == 0){

            retVal = true;
            cout << "Input must only contain letters\n";
            break;

        }
        else{

            retVal = false;
            cout << "all good\n";
        }
    }
    return retVal;
}

Because of how I use the function return value, I need it to return TRUE if it is NOT all letters, and FALSE if it IS all letters. There's probably an easier way to do this but I just started C++ so this works for my current purpose.

My question is how do I check if a string is multiple "words"? When it reaches a space the function (correctly) says the space is not an alpha and tells me the input must only be letters. I tried doing

if((isalpha(str[i]) != 0) || (str[i] == " "))

and changing the "if" to return false (input only letters & space) and "else" to return true, but when I tried this I got a compiler error:

ISO C++ forbids comparison between pointer and integer [ -fpermissive]

So what can I do to get that a string of user input is only letters or space? (Preferably the simplest method)

Tommy K
  • 1,759
  • 3
  • 28
  • 51
  • 1
    Use ' ', which is a character. " " is a string (pointer to characters)... – Max Sep 23 '14 at 18:24
  • `str[i]` is a character, so you should compare `str[i] == ' '` In reality your check for alphabetical or space should be `if(isalpha[i] || isspace[i])` because `isspace` will check for other whitespace characters like `/n` and `/r` – AndyG Sep 23 '14 at 18:26
  • I guess this question is duplicate of this one: http://stackoverflow.com/questions/7616867/how-to-test-a-string-for-letters-only – Hemant Gangwar Sep 23 '14 at 18:32

2 Answers2

5

This statement:

str[i] == " "

Is incorrect, it should be:

str[i] == ' '

but even better

isspace( str[i] )

as your condition does not check for other symbols like tab etc.

Also you have break in logic, in case you meet not alpha and not space you can set retVal to true (and also should terminate loop as you already got the answer), but you cannot set it to false otherwise. So your corrected code could be:

bool checkAlpha(const string &str){
    bool retVal = false;
    for(int i = 0; i < str.size(); i++){
        if( !isalpha(str[i]) || !isspace(str[i]){
            retVal = true;
            cout << "Input must only contain letters\n";
            break;
        }
    }
    if( !retval )
       cout << "all good\n";
    return retVal;
}

if you do not need to provide diagnostic messages, function can be as simple as:

bool checkAlpha(const string &str){
    for(int i = 0; i < str.size(); i++)
        if( !isalpha(str[i]) || !isspace(str[i])
            return true;
    return false;
}

And your function name is confusing, based on return values it should be called checkNotAlpha

Slava
  • 43,454
  • 1
  • 47
  • 90
  • Thanks for the help, I forgot to copy over the break; statement when I typed in my code (for some reason copy & paste resulted in a weird format). I agree the function name is a bit confusing, but I'm trying to get everything to work before sorting out the little details. The checkAlpha gets called inside a while true loop, and if it is all alpha I need to break the loop so I set the return value to false. But your suggestion is a simple fix so thanks for that! However I'm not getting the correct results from "hello there" I get the "Input must contain only letters". Looking into why – Tommy K Sep 23 '14 at 20:39
  • @TommyK you should post minimal complete code that shows your problem. We do not usually practice telepathy here. For function name you then better call function checkAlphaOrSpace and then negate it's output in the while: `while( !checkAlphaOrSpace(...) )` instead of returning convoluted values – Slava Sep 23 '14 at 20:44
  • had to change the return values to make it work correctly, but it works! Thanks! – Tommy K Sep 23 '14 at 20:54
0

I guess it should be enought to fix it as follows:

if((isalpha(str[i]) != 0) || (str[i] == ' '))
Michal Hosala
  • 5,570
  • 1
  • 22
  • 49
  • as I said in OP, this results in an error: ISO C++ forbids comparison between pointer and integer [ -fpermissive] – Tommy K Sep 23 '14 at 20:20
  • @Tommy K, please notice that there is quite a significant difference between `" "` and `' '`, they are effectively of two different types.. – Michal Hosala Sep 23 '14 at 20:30