0

I'm trying to create a mini game where if a user inputs "A" it will attack and deduct damage based on user input. However "Move: " seems to print twice. How do I stop it from doing that? Also, how do I stop my program to print the last line of current health if health is equal to zero already? Thank you so much for any help.


typedef struct{
    char coolName[100];
    int health;
    int defense;
}Hero;

int main(){
    Hero hero1;
    
    printf("Enter hero name: ");
    scanf("%s", &hero1.coolName);
    printf("Enter health: ");
    scanf("%d", &hero1.health);
    printf("Enter defense: ");
    scanf("%d", &hero1.defense);
    char move;
    int attack;
       
    do{ 
        printf("Move: ");
        scanf("%c%d", &move, &attack);
         if(move=='A'){
            hero1.health = hero1.health - (attack-hero1.defense);
            printf("\n%s current health - %d\n", hero1.coolName, hero1.health);
            if(hero1.health<=0){
                break;
            }
            }else if(move=='S'){
            printf("I surrender");
            break;
            }
    }while(hero1.health>0);
    printf("%s has fallen", hero1.coolName);

        
    }```
  • What do you do with the ``s the user types? They get read in by `scnaf("%c")`. – pmg Oct 05 '22 at 11:14
  • Please specify the exact input required to reproduce the problem. – Andreas Wenzel Oct 05 '22 at 11:19
  • 1
    The `scanf("%c%d", &move, &attack);` should be `scanf(" %c%d", &move, &attack);` with an additional space. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Oct 05 '22 at 11:27

0 Answers0