1

EDIT: sorry, it's in C not C++, I get them mixed up sometimes. I changed lengthN == NULL to lengthN == 0 and I'm getting an infinite loop after I enter the first name (if the name is longer than 15 characters).

So I am trying to make this thing that enters player names for this game I have to make for an assignment, and for some reason it doesn't repeatedly go through the loop. The loop SHOULD check the string length and if it is set to NULL which would be outside the buffer, it will ask the user to re input, and will repeat itself until the correct length is input.

printf_s("Enter player %d's name (Max 15 characters): ", count);
scanf_s("%s", &name, 15);
lengthN = strlen(name);
printf_s("\n%s\n%d", name,lengthN);

while (lengthN == NULL)
{
    printf_s("\nName too big; please enter a name within 15 
    characters!\n\n");
    scanf_s("Enter name: %s", &name, 15);
    lengthN = strlen(name);
}
  • 3
    May want to pick a language. This looks like C. In C++, there's a true `std::string` class which can't be NULL. – MSalters May 19 '17 at 08:24
  • 2
    Are you in C or C++ ? Please, don't use both tag, say clearly which language you're using. – informaticienzero May 19 '17 at 08:24
  • 1
    Did you try stepping through your code in a debugger, while observing the values of the variables (e.g. `lengthN`)? – Algirdas Preidžius May 19 '17 at 08:25
  • 1
    By the way, neither in C nor in C++ it makes sense to compare the result of `strlen`, an integer, to `NULL`, which is value for pointers. – informaticienzero May 19 '17 at 08:27
  • Did you do memset 0 to the name? May be it contains the previous entry still – Sam Daniel May 19 '17 at 08:28
  • Could you also give an example of a result you are getting with your code while testing? – Izuka May 19 '17 at 08:33
  • 1
    Just to be sure: please show the definitions of `name`. – Jabberwocky May 19 '17 at 08:39
  • 1
    Actually you don't need the `while` loop at all, because `scanf` with the %s format specifier won't even let you enter an empty string. It won't let you enter spaces either. If you want spaces to be entered [read this](http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf). But `scanf` is complicated and sometimes weird, I suggest you rather use `fgets`. – Jabberwocky May 19 '17 at 08:45
  • 1
    Why are you trying to compare string *length* (which is in an *integer* value) to `NULL` (which is intended for *pointer* contexts)? – AnT stands with Russia May 19 '17 at 08:58
  • name is `char name[16]` –  May 19 '17 at 09:07
  • Try `for(;;){ int retVal = scanf_s("%s", name, sizeof(name)); if(retVal == -1) { puts("EOF"); } else if(retVal == 0) { fprintf(stderr, "Invalid input!\n"); scanf("%*[^\n]"); getchar(); } else { break; } }` – Spikatrix May 19 '17 at 09:20

3 Answers3

0

lengthN won't be equal to NULL (at least in C), as strlen return you a size_t element which is a numeric value. At least to make your code more logical, you should instead test in your while loop if lengthN is equal to 0, and then ask the user to enter a name until the length is different to 0.

while (lengthN == 0)
{
    printf_s("\nName too big; please enter a name within 15 
    characters!\n\n");
    scanf_s("Enter name: %s", &name, 15);
    lengthN = strlen(name);
}
Izuka
  • 2,572
  • 5
  • 29
  • 34
0

According to scanf_s()'s documentation:

Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error, or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.

Shouldn't you check instead against the value returned by scanf_s() in the condition for the while loop?

while (scanf_s("%s", &name, 15))
   ;
JFMR
  • 23,265
  • 4
  • 52
  • 76
  • I thought I was? `lengthN = strlen(name)` which is getting the characters of name as an int then comparing? –  May 19 '17 at 08:46
  • That didn't work either. The loop is either going into an infinite loop or only iterates once, and the code you've posted only loops if the characters are within 15, which is the opposite of what it's supposed to do. –  May 19 '17 at 08:55
  • try removing the check against EOF – JFMR May 19 '17 at 08:56
  • So what it should do is: First ask the user to enter a name less than or equal to 15 characters, then check if the name is within those parameters, if not, it should loop and ask again until the user has put in the correct data. –  May 19 '17 at 09:11
  • @Robert, after calling `scanf_s()` check the return value, it's the size of the bytes written to `name` . But be also aware of getting EOF as the return value of `scanf_s()`. – JFMR May 19 '17 at 09:20
0

scanf_s("%s", &name, 15); is not expected to fill name with a zero length string. @Michael Walz

It consumes leading white-space until some non-white-space is entered - which is saved in name. (end-of-file and rare input error are exceptions).

lengthN = strlen(name); is thus non-zero.

... and while (lengthN == NULL) is always false.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • So what test should I do instead? –  May 19 '17 at 10:54
  • @robert Coding goal lacks details for "what test should I do instead": What types are used, sizeof arrays, sample input like are "Betty Jo", "1234", " valid for a first name? Without details, like these, "what test should I do instead" could be an iterative process for a best answer. – chux - Reinstate Monica May 19 '17 at 15:02