Home > Software engineering >  need help in c langauge on if else
need help in c langauge on if else

Time:09-07

hello helper's i am a noob in c language pls help me this is the code

#include <stdio.h>
int main()
{
    int health_villain = 100, attpow1 = 15, attpow2 = 20, attpow3 = 25, attpow4 = 10, selatt;
    attack:
    printf("list of attacks:\n");
    printf("1.hammer throw\t2.hammer beam\n3.hammer crush\t4.just simple punch\n");
    printf("%d\n", health_villain);
    scanf("%d", &selatt);
    if (selatt = 1)
    {
        health_villain = health_villain - attpow1;
    }
    else if (selatt = 2)
    {
        health_villain = health_villain - attpow2;
    }
    else if (selatt = 3)
    {
        health_villain = health_villain - attpow3;
    }
    else if (selatt = 4)
    {
        health_villain = health_villain - attpow4;
    }
    else
    {
        printf("please, choose numbers between number 1 - 4");
        goto attack;
    }
    printf("%d", health_villain);
    return 0;
}

i want to subtract health_villain with choosen attpow like if player choose attack 2 then health_villain will subtract with attpow 2 but output is only 85

CodePudding user response:

Different operators have different meanings attached to them, according to this site there are a few different kinds of operators:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. 'Misc' Operators

The problem with the code above is that the expression inside the conditional part of the if statements (the part within the brackets) are using assignment operators when they should be using 'truth functional' operators such as relational operators instead.

CodePudding user response:

so , you have only one small problem which is your IF conditions as mentioned in the comments like it's if (selatt == 1) not if (selatt = 1) as when you write if (selatt = 1) this means that selatt will be assigned to the value 1 and then the IF condition will take plcae , like the following illustrates the sequence of what you wrote :

  1. you entered value 2 for selatt variable
  2. selatt = 2
  3. if (selatt = 1) will make then selatt = 1 , so selatt variable will have value 1
  4. if (selatt = 1) will be equivalent to after assignment if (selatt) which is equivalent to if (1) which will always true as in C, any value other than 0 is considered true.

, also there are bad programming habits that you are making in your code like :

  • using goto and label instead of loops as in line goto attack; as the goto and label decreases the readability of the code according to MISRA C

Rule 15.1 The goto statement should not be used

  • using if(selatt == 1) instead of if(1 == selatt) as if(1 == selatt) will make sure that you don't make these small mistakes like you did in your code
  • also the scanf function return the number of fields that were successfully converted and assigned. and you should use this returned value according to MISRA C

Rule 17.7 The value returned by a function having non-void return type shall be used

and here is your full code edited:

    #include <stdio.h>
int main()
{
    int health_villain = 100, attpow1 = 15, attpow2 = 20, attpow3 = 25, attpow4 = 10, selatt;

    while(1) {
        printf("list of attacks:\n");
        printf("1.hammer throw\t2.hammer beam\n3.hammer crush\t4.just simple punch\n");
        printf("%d\n", health_villain);
        int returned = scanf("%d", &selatt);

        if(1 == returned){
            if (1 == selatt) {
                health_villain = health_villain - attpow1;
                break;
            } else if (2 == selatt) {
                health_villain = health_villain - attpow2;
                break;
            } else if (3 == selatt) {
                health_villain = health_villain - attpow3;
                break;
            } else if (4 == selatt) {
                health_villain = health_villain - attpow4;
                break;
            } else {
                printf("please, choose numbers between number 1 - 4");
            }
        }
    }
    printf("%d", health_villain);
    return 0;
}
  •  Tags:  
  • c
  • Related