0

I have to make a program where the input must be one of 2 values, 'M' or 'F'.

Here is the program segment that I have created to demonstrate

do
{

     printf("What is your Gender? (M for Male - F for Female) "); 
     fgets(GenderValue, 16, stdin); 

     if (GenderValue[0] == '\n') 
     {
           printf("Try again");
           getch();
           system("cls");
           loop=1;
     }
     else
     {
           loop = -1;
     }
     if (GenderValue == "M");
     {
          loop =-1;
     }
     else if(GenderValue == "F");
     {
          loop=-1;
     }

     else
     {
           printf("Try again");
           getch();
           system("cls");
           loop=1;
     }



 }
 while(loop > 0); //Checks for NULL input

 printf("Gender: %s",GenderValue);

I know I could have done an integer choice input, but I would like to recycle this later if possible.

So far the program calls my Gender value comparisons "Errors", Which is fine, except I have no idea what to do next, I could find a function that compares strings, but I really don't want to over complicate my code.

Edit: Duplicate?, Really?, The example given doesn't even come close to helping me.

Edit 2: Found the problem, i had ;'s next to my if statements, that's fixed now. But seriously, no one spotted this ?

It'sRainingMen
  • 173
  • 1
  • 1
  • 10

5 Answers5

6

You use strcmp to compare strings. Using the comparison operator == just compares the pointers.

But note that fgets will leave the ending newline in the string, so you need to compare to e.g. "M\n", or remove the newline if it's there.

Also note that scrcmp is case sensitive, the string "M" is not equal to the string "m".


You could use e.g. scanf to read the input as a single character instead, and use e.g. tolower (or toupper) to get case insensitive comparison.

However, using scanf brings other problems, especially if you use it again afterwards to read another single character. The reason for this is that Scanf leaves the newline in the input buffer, causeing the possible next scanf call with the "%c" format to read that newline. There are very simple solution for this: Tell scanf to read and discart leading whitespace by using " %c".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1
  1. If GenderValue is a string, GenderValue == "M" is wrong . strings are not supposed to be comapred with ==. use strcmp().

  2. [In this case, IMO its suitable] If GenderValue is a single character, comparison should be done as GenderValue == 'M'

For your case, [considering one character input] you can do something like

fgets(GenderValue, 16, stdin);
switch(GenderValue[0])
{
   case '\n':
              //your code
              break;
   case 'M':
   case 'm':
                //your code
              break;
   case 'F':
   case 'f':
                //your code
              break;                  
   default:
}

In above code, three advantages

  • You don't need to bother about removing trailing \n caused by fgets()
  • Exactly one character input is considered
  • No need to worry about the upper/lower case of input.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

replace (GenderValue == "M") with (strcmp(GenderValue, "M") == 0 )

and

replace (GenderValue == "F") with (strcmp(GenderValue, "F") == 0 )

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
tnishada
  • 1,315
  • 1
  • 16
  • 24
0

try replacing GenderValue == "F" with !strcmp(GenderValue, "F\n") and GenderValue == "M" with !strcmp(GenderValue, "M\n")

you might also need to include "string.h"

skortzy
  • 169
  • 6
  • So my if statement would be exactly the same? – It'sRainingMen Nov 26 '14 at 10:38
  • well, I am not sure what exactly you want to do. at first glance, the first else should not be there because it sets loop to -1 and even if the conditions that follow hold, this wont change. – skortzy Nov 26 '14 at 10:39
  • I want the statements to only allow M and F. GenderValue is a string. – It'sRainingMen Nov 26 '14 at 10:43
  • there are several nice solutions that were proposed by other people; if you do not want to change your code too much, just remove the three lines after the first else. The new code should be something like: else if (!strcmp(GenderValue, "M\n")) – skortzy Nov 26 '14 at 10:46
  • I should be fine with the help i got above, the case statement above is proving rather effective. – It'sRainingMen Nov 26 '14 at 10:52
0

Use string compare function (strcmp("")) whenever you need to compare strings.

Bilal Amjad
  • 149
  • 3
  • 11