You don't seem to have a solid understanding of pointers and value types. For example:
char myName = 'name';
This is not at all what you're thinking. If you want this variable to hold a string, it should be of type char *
rather than char
. And the literal 'name'
isn't the string "name"
— single quotes signify character literals rather than string literals. A four-character character literal is a seldom-used and seldom-useful construct that combines all the characters into an integer — in this case, an integer holding the byte values of the characters 'n', 'a', 'm' and 'e'. (This used to be used frequently on the old Mac OS for file metadata. I'm not sure if it was ever commonly used anywhere else.)
Additionally, your reading code is incorrect:
scanf("%c", *nameTemp);
The format specifier "%c" looks for a single character rather than a string, and *nameTemp
means "dereference the pointer nameTemp
", which is not at all what you want to do here. Instead, you want the "%s" format specifier and just to pass nameTemp
, since scanf wants a pointer.
The correct, idiomatic version of this code should look something like this:
//Login.
char myName = "name";
//Login Temp.
char nameTemp[10];
int matched = 0;
while (!matched) {
printf("Name?\t");
int readSucceeded = scanf("%9s", nameTemp);
if (readSucceeded && strcmp(myName, nameTemp) == 0) {
printf("Hej");
break;
}
else {
printf("Wrong. Try again");
}
}
(Caveat: Written in browser and not tested, but hopefully you get the idea.)