0

i am new to programming so the code i use is rather limited to what i've learned. at any rate im developing a scenario in which a user inputs one of two choices that will trigger different sets of code to execute or at least that is what im trying to do. however as i walk through my code it is executing it as if there were no if statements everything is running as one block. im pretty sure my logic is ok and that if anything my syntax is iffy. if there is anything you guys could do to help not just solve my issue if possible i'd like to learn rather than just have things fixed. thank you for your time.

    int main()
{
    int move = 0, pstrength = 10, pdefense = 20, pintelligence = 40, php = 10, estrength = 30,
        edefense = 40, eintelligence = 25, ehp = 10; 

    printf("Battle Start!\n\nYour HP: %d Enemy HP: %d\n\n1 - Attack\n2 - Magic\n\n", php, ehp);

    float pap = (float)pstrength / (float)edefense * 5;
    float pmp = (float)pintelligence / (float)eintelligence * 5, eap = (float)estrength /
        (float)pdefense * 5;

    do {
        printf("Select your move: "); 

/player will chose what action to preform if action 1 is selected then player attacks if the enemy does not fall to 0 hp the enemy attacks then the loop resets and prompts for a second round of input/

    scanf("%d", &move);
    if (move = 1); {
    printf("\nYou attacked the enemy!\n");
    ehp = ehp - (float)pap;
    if (ehp > 0){
        printf("The enemy attacked you!\n\n");
        php = php - (float)eap;
        printf("Your HP: %dEnemy HP: %d\n\n", php, ehp);
    }
    }
    if (move = 2); {
        printf("\nYou shocked the enemy!\n");
        ehp = ehp - pmp;
        if (ehp > 0){
            printf("The enemy attacked you!\n");
            php = php - (float)eap;
            printf("Your HP: %dEnemy HP: %d\n\n", php, ehp);
        }}

} while (php >= 0 && ehp >= 0);
if (php <= 0)
    printf("You Died!");
else
    printf("You Won!");
return 0;

}

i ran a debug and whats happening is regardless of my selection if (move=1) or if (move=2) it runs systemically as in if i input 2 at the start it runs as if i input 1 and so on.

2 Answers 2

5
  1. There is a ; after each of your if statements. Remove them.
  2. Equality checking is done via == not = which is assignment.

It will look like this :

if (move == 1) { ... }
Sign up to request clarification or add additional context in comments.

1 Comment

awesome i knew i was going wrong with that statement thank you very much
0

You have semicolons after your if statement blocks! the correct syntax for if statements is if(condition) {code}

2 Comments

i rechecked and took them off its running now just an issue with my "flawless logic" ty for the help guys
No problem, tips for your program - add some random number generation! Maybe create a class for the monsters so you can create different types. Use a parameterized constructor to create monsters with different stats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.