I haven't done C in a long while and slowly getting back into it. I made a small game and now going through 'bug-fixing' and making the odd tweak here and there. One issue I have if the text inside a printf()
statement regarding turns ...
printf("CONGRATULATIONS!!\nYou won with %d turns remaining\n",turns);
Now that is great until turns==1
.
Is there an efficient way to change the text 'turns' based on the condition of the turns variable? Or would I have to use if statements (one solution I already have but I'm sure there is a better one!)
if (turns==1)
{
printf("CONGRATULATIONS!!\nYou won with %d turn remaining\n",turns);
}
else
{
printf("CONGRATULATIONS!!\nYou won with %d turns remaining\n",turns);
}
Sorry for the really 'noob' question but I'm stuck as to what would be the most efficient way of doing this.
Using the conditional-operator might satisfy your needs
printf("CONGRATULATIONS!!\nYou won with %d turn%s remaining.\n",
turns,
turns==1 ?"" :"s");
or just do
printf("CONGRATULATIONS!!\nYou won with %d turn(s) remaining.\n",
turns);
;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With