1

I don't understand why it always returns FALSE even though every single variable satisfies conditions. I've tried to change the characters in the conditions to ascii numbers but it can not solve the problem. Any help, Thanks a lot.

I want to check every variable in an array if one of them is dissimilar to the characters in the alphabet or "SPACE" or ".", the function will return False.

bool KiemTraTenSinhVien(char ten[])
{
    for (int i = 0; i < strlen(ten); i++)
    {
        if (ten[i] == (char)" " || ten[i] == (char)".")
        {
        }
        else if (ten[i] >= (char)"a" && ten[i] <= (char)"z")
        {
        }
        else if (ten[i] >= (char)"A" && ten[i] <= (char)"Z")
        { 
        }
        else
        {
            return false;
        }
    }
    return true;
}

I also try this but the problem still be unsolved

bool KiemTraTenSinhVien(char ten[])
{
    for (int i = 0; i < strlen(ten); i++)
    {
        if (ten[i] == ' ' || ten[i] == '.')
        {
        }
        else if (ten[i] >= 'a' && ten[i] <= 'z')
        {
        }
        else if (ten[i] >= 'A' && ten[i] <= 'Z')
        { 
        }
        else
        {
            return false;
        }
    }
    return true;
}
  • thank you. I'try it now – Huy Lieu xuon Nov 03 '18 at 23:30
  • It does not work @madjaoue – Huy Lieu xuon Nov 03 '18 at 23:35
  • `else if (ten[i] >= 'a' && ten[i] <= 'a')` will be false for every character except for `'a'`. – Daniel Pryden Nov 03 '18 at 23:56
  • but when I change 'a' to 97 (the number of it in the ascii table) and 'z' to 122, the function still returns FALSE – Huy Lieu xuon Nov 03 '18 at 23:59
  • The code using strings and casts `(char)"a"` etc is converting a pointer to a character and who knows what result you get from that. Code using `'a'` without a cast should be OK. What data did you try? How did you get that data? Did the data include a newline by any chance (because you read it with `fgets()`, for example)? – Jonathan Leffler Nov 04 '18 at 00:02
  • I used gets to get it char name[30]; gets(name); – Huy Lieu xuon Nov 04 '18 at 00:03
  • Please read [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) and never use it again. However, it does eliminate the newlines. You should show your test code, – Jonathan Leffler Nov 04 '18 at 00:07
  • This code worked OK for me: `#include ` — `#include ` — `#include ` — `static bool KiemTraTenSinhVien(char ten[]) { for (int i = 0; ten[i] != '\0'; i++) { if (ten[i] == ' ' || ten[i] == '.') { } else if (ten[i] >= 'a' && ten[i] <= 'z') { } else if (ten[i] >= 'A' && ten[i] <= 'Z') { } else { return false; } } return true; }` — `int main(void) { char line[4096]; while (fgets(line, sizeof(line), stdin) != 0) { line[strcspn(line, "\n")] = '\0'; printf("[[%s]] = %s\n", line, KiemTraTenSinhVien(line) ? "true" : "false"); } return 0; }` – Jonathan Leffler Nov 04 '18 at 00:09
  • Yah. I typed a name in the console and use gets function to get it for an array called(ten). Then I used the function above to check the array, but it always returns False – Huy Lieu xuon Nov 04 '18 at 00:12
  • I not sure, but it did not work on my computer – Huy Lieu xuon Nov 04 '18 at 00:14
  • @JonathanLeffler do you use C++? I cannot find – Huy Lieu xuon Nov 04 '18 at 00:17
  • `` is part of C99 — if you don't have it, it is time to upgrade to a more modern compiler (if at all possible). (C11 [§7.18 Boolean type and values ``](https://port70.net/~nsz/c/c11/n1570.html#7.18). It is also very easy to simulate, even if you don't have the `_Bool` type built-in to your compiler.) Which compiler are you using on which platform? C++ does not need a header; `bool` is a built-in type in all standard C++ compilers (and therefore `bool`, `true` and `false` are full keywords in C++). – Jonathan Leffler Nov 04 '18 at 00:26
  • I’m using visual studio 2017 on windows 10 – Huy Lieu xuon Nov 04 '18 at 00:27
  • Yeah, bad luck. MS doesn't have very good support for anything more recent than C89/C90; it's a nuisance. Change `bool` to `int`; change `true` to `1`; change `false` to `0`; drop the `#include ` line. That should then work the same. However, you used `bool` and `true` and `false` in your question, and it's tagged C. It makes me wonder if you're using MSVS in C++ mode? Or you can simply drop the `#include ` line since you don't seem to need it (but it isn't very C like then). – Jonathan Leffler Nov 04 '18 at 00:30

1 Answers1

1

Use an array of valid characters and test with strchr.

bool KiemTraTenSinhVien(char ten[])
{
    char valid[] = " .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int len = strlen ( ten);
    for (int i = 0; i < len; i++)
    {
        if ( ! strchr ( valid, ten[i]))
        {
            return false;
        }
    }
    return true;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
  • thank you for helping me to solve the problem. would you mind to spend just a few minutes to explain what's makes my code wrong? Thanks a lot – Huy Lieu xuon Nov 03 '18 at 23:52