3

Why won't this work? I want to compare two chars.

   //Login.
    char myName = 'name';

    //Login Temp.
    char nameTemp[10];

again:

    printf("Name?\t");
    scanf("%c", *nameTemp);



    if (strcmp(myName, nameTemp) == 0) {
        printf("Hej");
    }

    else { printf("Wrong. Try again"); goto again; }
jscs
  • 63,694
  • 13
  • 151
  • 195
Funktiona
  • 113
  • 2
  • 11

6 Answers6

4

Change:

char myName = 'name';

to

const char *myName = "name";

You should have pointer to array of chars here instead of one char. Also change

scanf("%c", *nameTemp);

to

scanf("%s", nameTemp);

You should scan array of chars instead of one char.

And goto in this kind of program? Why in the world you didn't use plain while loop?

igoris
  • 1,476
  • 10
  • 20
3

This line is wrong

char myName = 'name';

myName can only hold 1 character and you are trying to put a string in the variable. You need a null delimited char array instead.

Pepe
  • 6,360
  • 5
  • 27
  • 29
  • That's not a string — it's a multicharacter literal. Weirdly, I don't think it will even warn you since those literals produce integers and truncating an integer into a char is not an error. – Chuck Nov 25 '13 at 20:14
3

I think you need to change

char myName = 'name';

to this:

char *myName = "name";

As myName can hold only a character and you are storing a string in that which is wrong.

Also change your scanf like this:

scanf("%s", nameTemp);

Note:-

char* is used to point to a simple array of character data values.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

To declare a string in c you must declare a pointer of type char as follows

   char *myName="name";

and the name of the array is enough because it is a pointer

  scanf("%s", nameTemp);
Alaa
  • 539
  • 3
  • 8
  • 29
1

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.)

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

you have to change this

char myName = 'name';

to

char *myName = "name";

or

char myName[] = "name";
Giovanni Far
  • 1,623
  • 6
  • 23
  • 37