I am really new to C and i'm trying to program a simple choice-based game to test the knowledge I have acquired so far.I want to give the player the option to choose either one or other options as an approach, and have different options hold different results. My idea was using either if and if else or the switch statement, but is there any more efficient way to implement multiple choices? The result i'm going for is:
if (choice_1) {
if(choice_3) {
//other choices
}
}
else if (choice_2){
if(choice_4) {
//other choices
}
}
else {
if(choice_5) {
//other choices
}
}
But this would make the code really hard to read since I would have many choices one after the other, and one inside the other. Another solution i thought of is using switch.
switch(choiche) {
case choice_1 :
//other switches for choices
case choice_2 :
//other switches for choices
case choice_3 :
//other switches for choices
default : //error
}
But still, it would make the code really hard to read and long to write. So my question is, are there any better alternatives to this methods to manage many choices one after the other?
Aucun commentaire:
Enregistrer un commentaire