-2

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 code 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 block.

melpomene
  • 84,125
  • 8
  • 85
  • 148
YaRmgl
  • 356
  • 1
  • 6
  • 19
  • 5
    Are you familiar with [`strcmp`](https://en.cppreference.com/w/c/string/byte/strcmp).? Time to do so, because what you're doing is *not* how you compare terminated strings in C. One of the more popular of the *thousands* of duplicates to this question [can be found **here**](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings). – WhozCraig Jan 12 '19 at 13:56
  • 2
    `if(nametype == "firstname")` is comparing *pointers* not content, so is unlikely to be true. – Weather Vane Jan 12 '19 at 13:57
  • You should compare these strings char by char, because compiler thinks they are arrays – Banzay Jan 12 '19 at 14:01
  • `void script1(nametype)` is invalid code (missing type for `nametype`). – melpomene Jan 12 '19 at 14:04
  • `scanf("%s", &nametype);` is both unsafe (buffer overflow, missing return value check) and a type error (`%s` takes `char *`, not `char (*)[100]`). – melpomene Jan 12 '19 at 14:05
  • `printf("Hey! Your %s is %s, you're %d \n", typename` has undefined behavior: `typename` is uninitialized here. – melpomene Jan 12 '19 at 14:06

2 Answers2

3

You have a fundamental misunderstanding what the == operator does.

It doesn't compare strings. It compares pointers. If you write

char a[100] = "Hello";
char b[100] = "Hello";

then a == b while compare the pointers. a is a pointer to the array a, b is a pointer to the array b, the pointers are different, the comparison is false.

Use strcmp.

PS. That alone won't make your code work, because you are creating a second variable named "typename" in a nested block. It's a different variable than typename in the outer block, so this will have no useful effect whatsoever.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
1
if(nametype == "firstname"){
    char typename[100] = "first name.";
}

In this, nametype is compares to an char array "firstname" ,so you have to use strcmp(str1,str2) function.For use this function, you have to use header. It returns 0 if two strings are equal. Like this

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

In the if condition, you have used typename variable which is already declared.So no need to re-declare this variable. In C, it doesn't support the direct string or char array assignment.So you have to use strcpy(destination,source) function.So the code will be like this

if(strcmp(nametype,"firstname")==0){
    strcpy(typename,"first name.");
}