Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

the problem is that the if function at the end is not reggisting the "Attack"

+10 votes
asked Sep 16, 2023 by Pessoua (500 points)
// libraries are stdlib.h and stdio.h, this is also NOT the main function

int main_fight(int Enemy_dmg, int Enemy_df, int Enemy_hp){
    char BATTLE_INPUT [50];
    printf("You angered an Enemy! (no name yet) \n");
    printf("What will you do? [Attack] [Heal] [Run] \n");
    printf("--->");
    scanf("%s", BATTLE_INPUT);
    
    if (BATTLE_INPUT == "Attack"){
        printf("User Attack!");
    } else {
        printf("Code not working :>");
    }
    
}

2 Answers

+1 vote
answered Sep 17, 2023 by Peter Minarik (86,240 points)

In C string literals can be compared with equality operator (==), but what the user enters and you store in BATTLE_INPUT is not a string literal.

When you compare the BATTLE_INPUT to "Attack" what you really compare is the memory address stored in your variable and the memory address of the string literal. You can see this is unlikely to match.

To compare strings, use the strcmp() function from the "string.h" library.

Your code should look like this

if (strcmp(BATTLE_INPUT, "Attack") == 0)

Good luck!

commented Sep 17, 2023 by Pessoua (500 points)
thanks it works much better now
0 votes
answered Sep 22, 2023 by Mohammed Raiyan (230 points)

int main_fight(int Enemy_dmg, int Enemy_df, int Enemy_hp){
    char BATTLE_INPUT [50];
    printf("You angered an Enemy! (no name yet) \n");
    printf("What will you do? [Attack] [Heal] [Run] \n");
    printf("--->");
    scanf("%s", BATTLE_INPUT);
    
    if (BATTLE_INPUT == "Attack"){
        printf("User Attack!");
    } else {
        printf("Code not working :>");
    }
    
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...