-2

I need to make a C program to read name and marital status of a girl and print her name with Miss or Mrs.

It works fine with this code:

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

int main()
{
    // Declare a char buffer to take input for name
    char name[30]={0};
    // Declare a char buffer to take input for answer 
    char YesNo[10]={0};

    //input name
    printf("Enter the name of a girl : ");
    gets(name);

    //input marital status
    printf("Is the girl married (Y-Yes, N-No) : ");
    gets(YesNo);

    if((!strcmp(YesNo,"yes")) || (!strcmp(YesNo,"Y")))
        printf("Her full name is : Mrs. %s",name);
    else if((!strcmp(YesNo,"no")) || (!strcmp(YesNo,"N")))
        printf("Her full name is : Miss %s",name);
    else
        printf("Marital status is wrong");

    return 0;
}

But I want to know what is problem in this code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name[100],mstatus=[30];

    printf("Enter the name of the girl!\n");
    scanf("%c",&name);
    printf("whether the girl is married (Enter 'Y' for Yes and 'N' for No)!\n");
    scanf("%c",&mstatus);
    if(mstatus=='Y')
    {
        printf("Full name of girl is Mrs %c:",name);
    }
    else
    {
        printf("Full name of girl is Miss %c:",name);
    }
    return 0;
}

Why we only have to use gets and not scanf, and what is the use of strcmp?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
sahil
  • 1
  • 1
  • `mstatus[0]` or `*mstatus` - however you should use a debugger or print things out at least to attempt to debug stuff yourself. Also you don't mean %c, that's a single character – Salgar Jul 01 '18 at 15:43
  • "*what is the use of strcmp*" its explained in the documentation, as well as all other lib functions are explained in there as well. Which version of the documentation did you read? – alk Jul 01 '18 at 15:48
  • 1
    Please note too that `gets` is **obsolete**. [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Weather Vane Jul 01 '18 at 15:54
  • 4
    In some circles, it would be best just to print "Ms" — though that can be a bit fraught too. Be wary of the social etiquette minefields into which this question is leading. (The coding problems are straight-forward — the socio-political ones are not.) – Jonathan Leffler Jul 01 '18 at 16:17
  • 1
    The `%c` format reads a single character; the `%s` format reads a single 'word' delimited by white space (skipping leading white space, including newlines, and stopping at the next white space). Neither of those is good for reading a full name such as "Sarah Smith". Using a line-based input (not `gets()` — that is [lethal](https://stackoverflow.com/questions/1694036/) — but `fgets()` or similar) is often a good idea; sometimes, a scan set such as `" %99[^\n]"` would be OK instead. Note that a scan set is complete at the `]`; it does not take a following `s`. Scan sets do not skip white space. – Jonathan Leffler Jul 01 '18 at 16:35
  • @JonathanLeffler I'm pretty bad at Minesweeper, do you ? ;) (I don't see how to change this question to something neutral) – Stargateur Jul 01 '18 at 16:59

2 Answers2

0

Here name is a string can contains more than one characters. So you must use %s format specifier to read it. So while reading string using scanf it exempt to use dereferencing operator to get ir's address. You just pass it name is enough. It doesn't denote as a compilation error. It is a logical mistake. Also you can use strcmp() method for comparing two strings.

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
  • 2
    `scanf(..., &name);` is wrong, no matter if using `c` or `s` as conversion specifier. – alk Jul 01 '18 at 16:31
  • If you use scanf for a single character then %name is right. Otherwise dereferencing is not used. Thanks for your correction – Codemaker2015 Jul 01 '18 at 16:49
-1

The problem with the second code is scanf() reads the string to variable name and leaves a newline character in the buffer. The second scanf reads only the '\n' character and ignores the value 'yes' or 'No'. Having getchar() after the first scanf helps.

scanf("%s",&name);
getchar();
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Note my [comment](https://stackoverflow.com/questions/51124713/c-program-to-read-name-and-marital-status-of-a-girl-and-print-her-name-with-miss#comment89235147_51124713) and also [alk](https://stackoverflow.com/users/694576/alk)'s [comment](https://stackoverflow.com/questions/51124713/c-program-to-read-name-and-marital-status-of-a-girl-and-print-her-name-with-miss#comment89235091_51124823). Note that the `getchar()` only helps if there is no trailing white space before the newline (which is often, but not necessarily, the case). – Jonathan Leffler Jul 01 '18 at 16:36