I suggest a different approach:
Code:
#include <stdio.h>
typedef struct {
int min;
int max;
} DamageRange;
DamageRange *getDamageRange(DamageRange *damageRange, int level) {
if (level < 1 || level > 7) {
return NULL;
}
if (level <= 3) {
damageRange->min = 1;
damageRange->max = 4;
} else if (level <= 5) {
damageRange->min = 1;
damageRange->max = 5;
} else {
damageRange->min = 2;
damageRange->max = 7;
}
return damageRange;
}
int main(void) {
int baseDamage = 4;
int playerLevel = 3;
DamageRange damageRange;
if (getDamageRange(&damageRange, playerLevel)) {
printf("Total damage: %d + dice(%d,%d)\n", baseDamage, damageRange.min, damageRange.max);
} else {
printf("Invalid player level: %d\n", playerLevel);
}
return 0;
}
What you were doing was to come up with a particular string format for a "damage range" in your getDamage function. This might be convenient for your current purposes, but it is also limiting: if later you want to generate random numbers so as to compute the actual damage rather than have the user roll dice, you have to change your code considerably to accommodate that.
What I did instead was to model the damage range in a struct, then rename getDamage to getDamageRange so as to reflect what it is really producing. The string format is then expressed in the format string of the printf call, so if say, you decide that at one place you want to print "dice(1,4)" but in another place you prefer to print "roll the die for a random number from 1 to 4", that's not a challenge at all since you can have two different printf calls with their own format strings, with the same damage range.