I am having a hard time comparing my if else statements and getting it right. I want to ask for male or female, and if male then I can respond "Hello, Mr...." and if it is female then I can respond "Hello, Mrs...."

#include <stdio.h>

int main (void){

    int firstNum;
    char name[25];
    char gender[5];

    printf("Please enter your full name: ");
    fgets(name, 50, stdin);

    printf("Please enter your gender (Male or Female): ");
    fgets(gender, 5, stdin);

    printf("Please enter your age: ");
    scanf("%d", &firstNum);

    if (firstNum<18){
        printf("Please go home now.\n");
    }

    else{
        if(strcmp(gender,"male"))
        {
            printf("Hi, Mr. %s\bWelcome and Enjoy!\n", name);
        }
        else(strcmp(gender,"female"));
        {
            printf("Hi, Mrs. %s\bWelcome and Enjoy!\n", name);
        }
    }

    printf("Name: %sAge: %d\n", name, firstNum);

    getchar();

}

Recommended Answers

All 6 Replies

I am having a hard time comparing my if else statements and getting it right.

Could probably be, but you don't tell us what is wrong.
Does it compile?
Do you get errors?
If so what error messages and codes are you getting?

A quick observation of your code:
line 7 you define an array of 5 chars, from index 0 to 4.
The string "female" is 6 chars long.
Don't know if that's the error.

It gives me errors saying that I need strcmp, which I am not familiar on how to properly set it up.

So what is wrong is that I am not sure how to actually set up an if else statements for the "if you're a male" or "if you're female" to give two different responds.

I think you need to include the source that works with strings #include "strmp.h" try that and see.

Hi Micheal_48,

Someone has mentioned that the number of character in "male" is 4 while "female" is actually 6, counting from 0. So, your gender char should be longer than 5 as you have it.

Secondly, if you are using strcmp you should include string.h header in your program. Then remember, doing your comparism, if the two "string" are equal then you should return a 0.
So, one should expect that your if statement should show the == 0 in line 23 of your code.
Line 27, has some other issue too, your else is not written like so in C.

    if (...) {
        printf(...);
    }
    else {  // note HERE
        printf(....);
    }

Will be a proper way to write if/else in C and a couple of some programming languages.

Hope this helps.

A more general way to take in data records in C is to use a struct ... maybe something like the example below.

(Note the use of several functions to break up the program into jobs ...
a function for each task.)

You could start with several utility functions like this:

/* name_id_struct_example.c */

#include <stdio.h>
#include <string.h> /* re. strchr, strlen */
#include <ctype.h> /* re. toupper, tolower */

#define MAX_NAME_LEN 23

const int MIN_ID = 1;
const int MAX_ID = 999;

const char* HEADER = "Demo of a way to get valid name and id for a Student Record.";


/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int c = takeInChr( "\nMore (y/n) ? " );
    if( c == 'n' || c == 'N' ) return 0;
    /* else ... */
    return 1;
}

/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg, int min, int max )
{
    int val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
        {
            if( min <= val && val <= max ) break;
            /* else */
            printf( "\nValid range here is %d..%d\n\n", min, max );
        }
        else
        {
            printf( "\nInteger input only here please.\n\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}



/*
    example of an expedient way in C to handle string input from keyboard operator ...
    especially, if design calls to repeat input until string is of 'acceptable length'

*/
void takeInStr( const char* msg, char* buf )
{
    char local_buf[MAX_NAME_LEN+2]; /* +2 to leave room for \n\0 char's at end */
    char* p;

    /* an example of a C 'forever loop' ... loop until break from loop */
    for( ; ; ) 
    {
        printf( msg ); fflush( stdout );
        fgets( local_buf, sizeof local_buf, stdin );
        p = strchr( local_buf, '\n' );
        if( p )
        {
            *p = '\0' ; /* overwrite '\n' with '0' */
            if( strlen(local_buf) )
                break;
            /* else */
            printf( "\nBlank lines NOT valid input here.\n" );;
        }
        else
        {
            while( getchar() != '\n' ) ; /* flush stdin ... */
            printf( "\nYou enterd %d (or more)... but ONLY %d characters permitted here.\n",
                    MAX_NAME_LEN+1, MAX_NAME_LEN ) ;
        }
    }

    strcpy( buf, local_buf );
}

/* an example of a common design request ... */
void toCapsOnFirstLets( char* str )
{
    char* p = str;
    char prev = ' ';
    while( *p )
    {
        if( prev == ' ' ) *p = toupper( *p );
        prev = *p;
        ++p;
    }
}

Then you could typedef a struct to hold the data in each (data) record (with some functions to process that data - here, functions to input and output a record):

typedef struct
{
    char name[MAX_NAME_LEN+1]; /* +1 re. termimal '\0' char */
    int id; /* valid range is only 1..999 */
    char gender; /* 'm' or 'f' */
} Student ;

void printStudent( const Student* st )
{
    printf( "%03d %s, %s\n",  st->id, st->name, (st->gender == 'm' ? "Male" : "Female" ) );
}

void takeInStudent( Student* st )
{
    while( 1 )
    {
        takeInStr( "\nEnter name: ", st->name );
        toCapsOnFirstLets( st->name );

        st->id = takeInInt( "Enter id: ", MIN_ID, MAX_ID );

        while( 1 )
        {
            st->gender = takeInChr( "Male or Female (m/f): " );
            if( st->gender == 'm' || st->gender == 'f' )
                break;
                /* else */
            printf( "Only 'm' or 'f' are valid input here.\n" );
        }

        printf( "You entered: " );
        printStudent( st );

        if( tolower(takeInChr( "Accept (y/n) ? " )) == 'y' )
        {
            printf( "\nAccepted.\n" );
            break;
        }
        /* else */
        printf( "\nAborted ... please enter again.\n" );
    }
}

Then a (test) main function could be very simple ... like this:

int main()
{
    puts( HEADER );
    do
    {
        Student stud;
        takeInStudent( &stud );

        /*
        printf( "You entered:\n" );
        printStudent( &stud );
        */
    }
    while( more() );

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.