Your code is fixed below with some tips:
- If you do not know how a function works, consult the manual.
- To compare two strings, do not use the equality operator (==), as it checks for memory address equality for pointers (strings in C are pointers). Instead, use the strcmp() function. It returns 0 when they are equal, negative if the first comes first, and positive if the second comes first.
- printf() prints formatted text. Check the manual how to print various types. %s is for strings, %d is for integers (and there are many more).
- If you provide a formatting argument for printf(), you need to provide the value.
- To check if a string is empty, do not compare it with an empty string (again, that's pointer/memory address comparison), instead check if the length is 0.
- To convert a whole string to lower/upper case, iterate through all the characters in the string and convert them one by one.
- size_t is the return type of strlen(), which is an unsigned type, hence it's advised to compare it against unsigned values (0u indicates an unsigned 0 numeric literal).
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main() {
char name[100];
while (strlen(name) == 0u) {
printf("Hi! Can you enter here your name?\n");
scanf("%s", name);
}
int length_of_name = strlen(name);
if (strlen(name) <= 2) {
printf("Your name has to be at least 3 character long. Your name is %d character long currently", length_of_name);
} else {
char goodslashbad[100];
printf("Oh, hi, %s! It's so nice to see you! How are you today? (good/bad)\n", name);
scanf("%s", goodslashbad);
// turn answer to lower case
size_t length = strlen(goodslashbad);
for (size_t i = 0; i < length; i++)
goodslashbad[i] = tolower(goodslashbad[i]);
if (strcmp(goodslashbad, "good") == 0) {
printf("Looks like someone is having a great day today! =)");
} else if (strcmp(goodslashbad, "bad") == 0) {
printf("something bad happened? Oh no! I hope you'll be okay!");
} else {
printf("Your answer isn't recognised. Please try again.");
scanf("%s", goodslashbad);
}
}
}
Good luck and keep on learning! :)