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:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- '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 :
- you entered value 2 for
selatt
variable selatt = 2
if (selatt = 1)
will make thenselatt = 1
, soselatt
variable will have value 1if (selatt = 1)
will be equivalent to after assignmentif (selatt)
which is equivalent toif (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
andlabel
instead of loops as in linegoto attack;
as thegoto
andlabel
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 ofif(1 == selatt)
asif(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;
}