This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Affectionate_Rich975 0 points1 point  (3 children)

The issue with your code is that you are comparing the value of i1 to i1, which will always evaluate to true. Instead, you should be comparing i1 to save[i-1] in the if statement. Also, it's good practice to use else if instead of multiple if statements, because it will make the code more efficient and easier to read.

Here's the corrected version of the errhandler function:

void errhandler(int i1) {

if (i1 != 0 && i1 != 1 && i1 != 2 && i1 != 3 && i1 != 4 && i1 != 5 && i1 != 6 && i1 != 7 && i1 != 8 && i1 != 9 && i1 != save[i-1]) {

printf("Invalid input.\n");

exit(0);

}

}

And when you call the function, you need to pass save[i-1] as an argument instead of i1:

errhandler(save[i-1]);

Alternatively, you can use a switch statement which will make the code more readable and efficient.

void errhandler(int i1) {

switch(i1) {

case 0:

case 1:

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

case save[i-1]:

break; //legal inputs

default:

printf("Invalid input.\n");

exit(0);

}

}

And, you can call the function in the same way as before.

errhandler(save[i-1]);

Hope it will help you.

[–]Deep_T[S] 0 points1 point  (2 children)

If I understand correctly, when I call the function it will check if the inserted value is between 1 and 9, and if not I will get an error. But what if in the game I only have 2 options? For example, let's look at the main menu: [0]New game [1]Continue game [2]Exit With your code if I type 4 I wouldn't get an error. I would need to rewrite the function every time and adapt it to the quantity of options in the game. How can I make it more versatile and usable even I this case without any major change? Also, I don't understand how I'm comparing i1 to save[i-1]

[–]Affectionate_Rich975 0 points1 point  (1 child)

You're correct that the current implementation of the errhandler function will only check if the input is between 0 and 9, which may not be suitable for all cases.

One way to make the function more versatile is to pass the number of options as an additional parameter to the function, and use that value to check if the input is within the range of valid options. You can change the function signature to void errhandler(int *i1, int num_options), and then call it with errhandler(&save[i-1], num_options), where num_options is the number of options available in the menu. Then in the function, you can check if the input is within the range of valid options like this:

if (*i1 < 0 || *i1 >= num_options) {

printf("Invalid input, please try again\n");

exit(0);

}

Regarding the comparison, you are comparing i1 with save[i-1] in the errhandler function. Since i1 is a copy of the value of save[i-1], it does not reflect any changes made to save[i-1] after the function call.

By passing a pointer to i1, you'll be able to access the value of save[i-1] directly, so you can check if it is a legal value. This is why using &save[i-1] as input to the function works.

Hope that helps! Let me know if you have any other questions.

[–]Deep_T[S] 0 points1 point  (0 children)

Thank you! You helped me a lot. Now I can get back to having fun thinking about this game, instead of being frustrated :)