Help with simple computer program?

No matter if you type Male or Female, you get the same answer even with the if and else function. Please help i have been trying for a long time now to figure it out, i am new to programming.
By the way this is with the C language, NOT C++.
Thank you , here it is

#include
char main()
//31 is ending “}’
{

char M [2];
char F [2];

printf (“Are you M/F?”) ;
scanf (“%d , %e” , M , F) ;

if { printf(“Me too!”) ;

}

else {
printf (“Oh thats cool”) ;
}

return 0;

The %d and %e are for reading in numbers, not letters. You probably want something like…
char gender[2];
printf(“are you M/F? “);
scanf(“%s”, gender);
if(gender[0] == ‘F’) {
printf(“me too.”);
} else {
printf(“oh, that’s cool.”);
}

Alternatively, you can use getch() to read a single character from the keyboard without having to do the scanf stuff.

I’d suggest as a debugging step, you print out what scanf() returns. It returns the number of ‘%’ arguments successfully matched, and I’m pretty sure it’s returning 0 for your example. Something like:

int sr = scanf( … );
printf(“scanf() returned %d\n”, sr);

You are asking for a character to be entered (M or F, it seems) but your format specifiers are %d (integer) and %e (floating-point number). The scanf() format string is expecting the user to enter something like this:

23 45.6

… which probably isn’t what you really want. If you enter M or F, your scanf isn’t going to see an integer to match to the %d and it’s not going to set any values. Which explains why it’s ignoring your input.

Also I don’t see a condition for your if-statement. I assume that got lost in the copy-paste?

How about something like this:

char gender[2];
if (scanf( “%1s”, gender ) != 1) {
printf(“Error: scanf() didn’t find what it wanted\n”);
exit(1);
}

if (gender[0] == ‘M’) …

@M

From the user’s perspective, what 2 values is he or she supposed to enter?

Your if statement doesn’t have a boolean test.

Leave a Reply