If statement doesn't detect empty string

-3

I have a C app that takes user input from stdin. I have a validation function that is supposed to detect when a user presses ENTER without writing anything into the console. I've tried if (strSurveyDate[0] == "") and if (strcmp(strSurveyDate[0], "") == 0), but those don't work. The first one simply doesn't execute (the condition isn't satisfied, apparently), and the second one gives an access violation reading location 0x00000000 error. I have a Trim() function that strips out leading and trailing whitespace before sending the input to the function for validation, so I can't simply look for \n, \r, and so on. I've stepped through the code, and "" is what is being sent to the function.

I'm not sure if I need to include more code, but I will. This is the entire validation function:

int ValidateSurveyDate(char strSurveyDate[])
{
    // declare variables
    int intTrue = 1;
    int intFalse = 0;

    // validate that input was entered
    if (strcmp(strSurveyDate[0], "") == 0)
    {
        printf("ERROR: Please enter a survey date.\n");
        return intFalse;
    }
    else { return intTrue; }
}

And this is the code where I get the user input and send it to the validation function:

printf("Please enter the date of the survey.\n");
fgets(strSurveyDate, sizeof(strSurveyDate), stdin);
Trim(strSurveyDate);

// validate date of survey
if (ValidateSurveyDate(strSurveyDate) == 1)

Let me know if I need to include anything else, and I'll be happy to include it. Thanks for any help, I really appreciate it.


Here's the Trim() function, as requested:

void Trim(char strSource[])
{

    int intIndex = 0;
    int intFirstNonWhitespaceIndex = -1;
    int intLastNonWhitespaceIndex = 0;
    int intSourceIndex = 0;
    int intDestinationIndex = 0;

    // Default first non-whitespace character index to end of string in case string is all whitespace
    intFirstNonWhitespaceIndex = StringLength(strSource);

    // Find first non-whitespace character
    while (strSource[intIndex] != 0)
    {
        // Non-whitespace character?
        if (IsWhiteSpace(strSource[intIndex]) == 0)
        {
            // Yes, save the index
            intFirstNonWhitespaceIndex = intIndex;

            // Stop searching!
            break;
        }

        // Next character
        intIndex += 1;
    }

    // Find the last non-whitespace character
    while (strSource[intIndex] != 0)
    {
        // Non-whitespace character?
        if (IsWhiteSpace(strSource[intIndex]) == 0)
        {
            // Yes, save the index
            intLastNonWhitespaceIndex = intIndex;
        }

        // Next character
        intIndex += 1;
    }

    // Any non-whitepsace characters?
    if (intFirstNonWhitespaceIndex >= 0)
    {
        // Yes, copy everything in between
        for (intSourceIndex = intFirstNonWhitespaceIndex; intSourceIndex <= intLastNonWhitespaceIndex; intSourceIndex += 1)
        {
            // Copy next character
            strSource[intDestinationIndex] = strSource[intSourceIndex];

            intDestinationIndex += 1;
        }
    }

    // Terminate 
    strSource[intDestinationIndex] = 0;
}
c
string
validation
if-statement
asked on Stack Overflow Apr 21, 2021 by Ethan • edited Apr 21, 2021 by Ethan

1 Answer

1

The problem is what strSurveyDate[0] is a char not a string to compare to.

Try this:

int ValidateSurveyDate(char *strSurveyDate){
  // declare variables
  int intTrue = 1;
  int intFalse = 0;

  // validate that input was entered
  if (!strcmp(strSurveyDate, "")){
      printf("ERROR: Please enter a survey date.\n");
      return intFalse;
  }else{
      return intTrue;
  }
}

As describes Siguza, always compile with -Wall , this is a common warning.

answered on Stack Overflow Apr 21, 2021 by mxchxdx

User contributions licensed under CC BY-SA 3.0