0

I programmes a Tetris game, and as a part of it, I made ranking system. The ranking is saved in a text file, and it's read and saved in a linked list at the beginning of the program. Each node has name(string), score(integer), linke for next node(node pointer). The function I'm trying to add is finding a certain player's rank when the player's name is given. But when I tried with my code, it couldn't find the player.

I've tried using the code below, but it didn't work.

            printw("Input the name: ");
            echo();
            getstr(name);
            noecho();
            printw("      name      |   score   \n");
            printw("----------------------------\n");

            for(int i=0;i<rlen;i++){
                if(cur->name==name){
                    flag=1;
                    printw("%-16s| %d\n", cur->name, cur->score);
                }
                cur=cur->next;
            }
            if(flag==0) printw("\nsearch failure: no name in the list\n");
            break;

For example, if there was a player whose name is aaaa and he scored 1000, if I input aaaa, It should print aaaa | 1000 But instead it prints the search failure message.

MyName
  • 13
  • 3
  • 1
    Need an [mcve]. But this is likely wrong. `if(cur->name==name){`. I am assuming strings. Look up `strcmp` – MFisherKDX May 21 '19 at 16:45
  • 1
    In C, the `==` operator tells you whether two strings are really the same, i.e. have the same address, not whether they are equal, i.e. have the same content. Use `strcmp` for that. – M Oehm May 21 '19 at 16:46
  • @MOehm if two variables have the same address, they must have the same value, right? – PintoDoido May 21 '19 at 16:56
  • @PintoDoido: Yes, of course, but that's not what OP is trying to do as far as I can tell: `name` is probably a char buffer to which input is being read with `getstr`. The test is whether the contents of that buffer after reading and the contents of some previously stored string (with a different address) are equal. – M Oehm May 21 '19 at 17:13
  • Thank you so much! I totally forgot about strcmp function. I didn't know that == for strings checked the address, but now I get why it wasn't working. – MyName May 21 '19 at 18:06

0 Answers0