so i was trying to figure out while loops and mostly if statements so i tried to make this little game where you have an amount of health and so does a monster, a loop runs as long as you both have more than 0 health,so each loop the monster would deal 50 damage to you, but using scanf and if statements the situation can be altered by inserting "2" you decrease the damage you take,by using "3" you add to your health and by using "1" you cause damage to the monster but for some reason the IF statements responsible for these events don't seem to be working
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int monsterhealth =100;
int health =100;
int damage =50;
while(health>0&&monsterhealth>0){
int action [10];
printf("player has %d health \n",health);
printf("monster has %d health\n",monsterhealth);
printf("act:attack[1]\ndefend[2]\npotion[3]\n");
scanf ("%d",action);
if(action==1){(monsterhealth==monsterhealth-40);}
else if(action==2){(damage==damage-30);}
else (action==3);{(health==health+100);};
health=health-damage;
}
}
Regarding:
int action [10];
If you only need one action, declare a simple object, not an array:
int action;
When you make that change, you will need to change:
scanf ("%d",action);
to:
scanf("%d", &action);
That passes the address of action
to scanf
. scanf
needs to know the address of action
so that it can make a change to it. If you were using an array because scanf
appeared not to work with a simple object, then do not do that. Arrays work around the problem you might have been having using scanf
, because of some behaviors they have in C, but that is not the right solution.
Regarding:
while(health>0&&monsterhealth>0){
Do not squeeze your code together. Write it so it can be read easily and the spacing conveys meaning:
while (health > 0 && monsterhealth > 0) {
Regarding:
(monsterhealth==monsterhealth-40);
This statement has no effect because ==
just compares two things. The statement does not change the values of any objects (variables) or have any other observable effects. It says “Compare monsterhealth
to monsterhealth-40
and then do nothing with the result of the comparison.” What you want here is the assignment operator, =
rather than ==
, which says “Put a new value in this thing”:
monsterhealth = monsterhealth-40;
Change the other statements where you want to assign new values similarly.
Regarding:
else (action==3);{(health==health+100);};
The form of multiple if
-else
statements is:
if (condition)
statement;
else if (condition)
statement;
else
statement;
Note that it should end with else statement
, not else (condition) statement
. Use the line structure and indentation above. (Sometimes a very short statement with a single if
can be put on the same line, but do not do this until you are more experienced and have a good sense of style. That takes about thirty years or so to develop.) The code you want is:
if (action == 1)
monsterhealth = monsterhealth - 40;
else if (action == 2)
damage = damage - 30;
else
health = health + 100;
Some people might decorate the else
with information about the case it covers, such as:
if (action == 1)
monsterhealth = monsterhealth - 40;
else if (action == 2)
damage = damage - 30;
else /* action == 3 */
health = health + 100;
Some people advocate putting all statements attached to if
and else
statements in braces, to avoid certain editing errors. For single statements, the C language does not require it.
C has another form of statement designed for handling the particular conditions you are testing in those if
statements, the switch
statement, but I presume you are just getting used to these statements and will learn about switch
later.