samedi 12 janvier 2019

if/else statement is not validating data

I'm writing a code to better understand if/else statements, but I encountered a problem when trying to validate(?) a string, thanks for any help (C language)

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

int main(){

    char nametype[100];

    printf("Enter the name type (firstname/lastname): ");
    scanf("%s", &nametype);
    script1(nametype);

    return 0;
}

void script1(nametype){

    char firstname[100];
    int age;
    char typename[100];

    if(nametype == "firstname"){
        char typename[100] = "first name.";
    }

    if(nametype == "lastname"){
        char typename[100] = "last name.";
    } else {
        printf("You must enter the correct parameters! \n");
        main();
    }

    printf("Enter your name: ");
    scanf("%s", &firstname);
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Hey! Your %s is %s, you're %d \n", typename, firstname, age);
}

I expect the ode to proceed on to the end after I enter "firstname" or "lastname" in the first input but instead it always proceeds to go to the else

Aucun commentaire:

Enregistrer un commentaire