0

I'm doing validation to input name using Mr or ms or Mrs before name with do while statement. what should I fill in the while section?.

is it using strcmp or something else?

coding example

do{
    printf("Input Customer's Name [must have Mr. or Ms. or Mrs");
    scanf("%[^\n]", &customerName);
}while(what should i fill here?);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Abr
  • 35
  • 5
  • 3
    Calling `fflush` with an input-only stream (like `stdin`) is explicitly mentioned in the C specification as *undefined behavior*. Some libraries add it as a *non-portable* extension. I suggest you instead read the whole line with e.g. `fgets` and then use `sscanf` to parse the string. And remember to check what `sscanf` *returns*. Oh and some nitpicking: `do ... while` is a *statement* not a function. – Some programmer dude Aug 07 '19 at 11:00
  • 1
    Possible duplicate of [Check substring exists in a string in C](https://stackoverflow.com/questions/12784766/check-substring-exists-in-a-string-in-c) – 500 - Internal Server Error Aug 07 '19 at 11:07

3 Answers3

1

Write a separate function that checks whether a name starts with the listed strings.

For example

#include <stdio.h>
#include <string.h>

int start_with( const char *s, const char * a[] )
{
    int success = 0;

    s += strspn( s, " \t" );

    for ( ; *a != NULL && !success; ++a )
    {
        success = memcmp( s, *a, strlen( *a ) ) == 0;
    }

    return success;;
}

int main(void) 
{
    const char *title[] = { "Mr.", "Ms.", "Mrs.", NULL };
    enum { N = 100 };
    char name[N];

    do
    {
        printf( "Input Customer's Name [must have %s or %s or %s: ", title[0], title[1], title[2] );
        name[0] = '\0';
        fgets( name, N, stdin );
        name[strcspn( name, "\n" )] = '\0';
    } while ( !start_with( name, title ) );

    puts( name );

    return 0;
}

The program output might look like

Input Customer's Name [must have Mr. or Ms. or Mrs.: Bob
Input Customer's Name [must have Mr. or Ms. or Mrs.: Mr. Bob
Mr. Bob
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You need to check if the substring 'Mr.' 'Mrs.' or 'Ms' exists in the input string provided by the user or not. If it exists you can break out of the loop otherwise prompt him/her again.

One thing to note that the substring position in this problem must be 0 (i.e at the very beginning of the input string)

You can use strstr function as it not only checks for the substring but return a pointer to first occurrence of that also.

e.g

#include <stdio.h>

int main(void) {

    char customerName[50]; 
    char *titles[3] = {"Mr.","Mrs.","Ms."}; 
    char *titlePos; 
    int istitle = 0;

    do{

       printf("Input Customer's Name [must have Mr. or Ms. or Mrs \n");
       scanf("%[^\n]%*c",customerName);

       for(int i=0;i<3 && !istitle;i++){
       // checking if any of the title is present 

          titlePos=strstr(customerName,titles[i]);
          if(titlePos && (int)(titlePos-customerName)==0) {
            istitle=1; // title found 
            printf("CustomerName Consist the Title of %s \n",titles[i]);

        }
    }
}while(!istitle);

  // ... Rest of Your Code

    return 0;
}

Mukul Kumar Jha
  • 1,062
  • 7
  • 19
  • using strcmp? ex: while(strcmp(customerName, "Mr ")!= 0 || strcmp(customerName, "Ms ")!= 0 || strcmp(customerName, "Mrs ")!= 0); – Abr Aug 07 '19 at 11:17
  • I would suggest rather *strstr* as you need to check for the presence of 'Mr.' 'Mrs.' etc at the very first position in the given input name – Mukul Kumar Jha Aug 07 '19 at 11:35
-1

If you insist in only changing the what should i fill here? in your coding example, the most important thing to put in is getchar() to consume the leftover newline from the input, otherwise we'd get an endless loop. But normally, one would do this right in the scanf() with a format string "%[^\n]\n" or "%[^\n] " (equivalent).

Besides that, the expression

strncmp("Mr.", customerName, 3) &&
strncmp("Ms.", customerName, 3) &&
strncmp("Mrs", customerName, 3)

would do.

Armali
  • 18,255
  • 14
  • 57
  • 171