Hello, this is my first consultation ever on c++. :p I'm self learner so it's very hard to figure my code problems out without any consultation. ;/ I hope you will help me a bit. :)
My question is on IF function. If you can see on my code I'm writing simple program which will give me on the end of the program, all values that user wrote in. But there is one problem. When user writes name, last name, and when he needs to write MAN/WOMAN (even he writes correctly) it goes to another IF function as so on until all program executes on 3th IF function. I don't know what I'am doing wrong. I tried to use ("return char;") or ("return sex;") on the end of every function but it gave me just error.
I want to make that when they input wrong value (MAN/WOMAN) it would give them another hint till 2-3 (maybe I need loop?) and then execute program if they writes it again in wrong value.
The problem is that you are trying to compare strings using != or ==, that is not possible.
The problem is that "char sex[5];" does not just create a variable it creates an array of in this case 5 variables and "sex" becomes a pointer to the first of those variables. As you probably already know a pointer is just a value that says where in the memory something is stored. Let's assume the pointer value is 42.
Now you try to compare the strings using:
if(sex != "MAN" || sex != "WOMAN")
We said that sex has the value 42, so we know that one.
"MAN" is a new string, so a new array is created, the characters are placed in it and the pointer value is for example 69.
This means that your if-statement is actually
if (42 != 69 || something)
42 can not ever be 69, because the memory at location 42 is reserved for the "sex" string and thus was not available for the nameless "MAN" string.
//int b
printf("Your birth date(year/month/day): ");
scanf("%s", b);
//int bp
printf("\nBirth place: ");
scanf("%s", bp);
//int fc
printf("\nFavorite color: ");
scanf("%s", fc);
//RESULT
system("cls");
vprintf("Name: ", n);
printf("\n");
vprintf("Last Name: ", ln);
printf("\n");
vprintf("Gender: ", sex);
printf("\n");
printf("Age: "); // can't use printf("Age: ", a); it's because i want to use int a, but how I should do that?
printf("\n");
vprintf("Birth: ", b);
printf("\n");
vprintf("Birth place", bp);
printf("\n");
vprintf("Favorite color", fc);