-1

So I was working on this program again, and I encountered another problem. I am testing if a character is a space. However, instead of detecting that the there is a space, it stops the loop and doesn't do anything more. Here is my code:

string reet(char reet) {
if (isspace(reet) == true) {
    return "IA";
}
else {
    switch (reet) {
    case 'a':
        return "Zg";
        break;
    case'b':
        return "dA";
        break;
    case 'c':
        return "dG";
        break;
    case 'd':
        return "aw";
        break;
    case 'e':
        return "bw";
        break;
    case 'f':
        return "dQ";
        break;
    case 'g':
        return "cg";
        break;
    case 'h':
        return "ZA";
        break;
    case 'i':
        return "cQ";
        break;
    case 'j':
        return "YQ";
        break;
    case 'k':
        return "eA";
        break;
    case 'l':
        return "dw";
        break;
    case 'm':
        return "cw";
        break;
    case 'n':
        return "ag";
        break;
    case 'o':
        return "eQ";
        break;
    case 'p':
        return "bA";
        break;
    case 'q':
        return "aA";
        break;
    case 'r':
        return "ZQ";
        break;
    case 's':
        return "cA";
        break;
    case 't':
        return "Yw";
        break;
    case 'u':
        return "eg";
        break;
    case 'v':
        return "bg";
        break;
    case 'w':
        return "aq";
        break;
    case 'x':
        return "bQ";
        break;
    case 'y':
        return "Yg";
        break;
    case 'z':
        return "Zw";
        break;
    default:
        return " ";
        break;
    }
}
}

string enc(string input) {
string sketchyBois = input;
string bigBoi = "";
int yeetL = sketchyBois.length() + 1;
for (int x = 0; x < yeetL;) {
    bigBoi = bigBoi + reet(sketchyBois[x]);
    x++;
}
return bigBoi;
}

I was just wondering if anyone can tell me why it is doing this? Thank you!

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
NipIsTrue
  • 43
  • 6

1 Answers1

7

You need to change

if (isspace(reet) == true)

to

if (isspace(reet)) 

or

if (isspace(reet) != 0) 

since isspace only returns a non-zero int value for a white space character, not a bool.

(Note that as a matter of coding style, it's generally preferred to omit comparison with true or false in boolean tests and just use if (expr) or if (!expr).)

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 2
    Another fun trick: `if (!!isspace(reet) == true)` will also work as `!!` forces the conversion from a numerical value to a boolean value. (Don't actually do this though) – NathanOliver Nov 21 '18 at 14:49
  • 4
    @NathanOliver: yes - a great way to befuddle your colleagues who have never seen the "bang bang operator" before too! ;-) – Paul R Nov 21 '18 at 14:50
  • In applications where it matters, evaluating against zero is generally more efficient. – Jim Fell Nov 21 '18 at 14:50
  • It's more than just a matter of coding style; it is flat-out wrong to directly compare to `true` or `false`. It shows a fundamental misunderstanding of how an `if` statement evaluates an expression. – Mike Borkland Nov 21 '18 at 14:57
  • Paul R that didn't work. It still stops running after the space. For example the input I am a big boi returns just cQ. – NipIsTrue Nov 21 '18 at 15:34
  • @NipIsTrue With that change,`enc("I am a big boi")` produces `IAZgcwIAZgIAdAcQcgIAdAeQcQ ` (with a leading space). ([Example](http://coliru.stacked-crooked.com/a/9a4f4ec192217be8).) The problem lies in how you're reading the input before passing it to `enc`. – molbdnilo Nov 21 '18 at 16:04
  • Ok. How do you think i can fix that. – NipIsTrue Nov 21 '18 at 16:07
  • 1
    Since you're now looking at a second problem outside the code you've shown us you should post a new question with a [mcve] that reproduces this new problem. – Paul R Nov 21 '18 at 16:38
  • @molbdnilo I am passing the input as a string, inputed by the user. – NipIsTrue Nov 22 '18 at 16:51
  • 1
    @NipIsTrue I suspect that you're reading with `>>`, which reads word by word, separated by whitespace. – molbdnilo Nov 22 '18 at 17:08
  • @molbdnilo is there an alternate way to do it? – NipIsTrue Nov 23 '18 at 00:28
  • 1
    @NipIsTrue You should use `getline`. And perhaps get one of [these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 23 '18 at 07:30