9
Does an "if - else" statement always need the "else"?
(piefed.blahaj.zone)
Hello there! I am playing around at random for now before continuing reading the litterature that I am using to learn C.
What is a less "ugly" way to break out the while loop at the last if-else statement than leaving else empty? I guess I could have avoided this if I knew how to terminate the program at the switch - case (E) other that break -ing out of it as I am now, in other words, not having to "climb" out of switch , then while and then reaching "Goodbye!".
#include <stdio.h>
#include <string.h>
//Save "pins" in an array with an upper limit of ten pins. "Saved pins" resets if the upper limit is reached.
int main(void) {
int pin = 0;
int check = 0;
//Saved pins
int pin_history[10]={0,0,0,0,0,0,0,0,0,0};
int pin_history_index = 0;
int pin_history_limit = 10;
int choice = 0;
printf("Enter your new pin: ");
scanf("%d", &pin);
getchar();
printf("Confirm your new pin: ");
while ((scanf("%d", &check))) {
if (check != pin) {
getchar();
printf("\nMismatch! Confirm your new pin: ");
}
else if (check == pin) {
pin_history[pin_history_index] = pin;
pin_history_index = pin_history_index + 1;
if (pin_history_index >= pin_history_limit) pin_history_index = 0;
printf("\nYour new pin has been saved successfully!\n");
break;
}
}
printf("What would you like to do next?\n\n(V)iew your saved pins\n(S)ave another pin\n(E)xit\n");
while ((choice = getchar()) != EOF) {
switch (choice) {
case ('V'):
printf("\nYour saved pins are:\n");
for (int i = 0; i < pin_history_limit; ++i) {
printf("%d\n", pin_history[i]);
}
printf("\nWhat would you like to do next?\n(V)iew your saved pins\n(S)ave another pin\n(E)xit\n");
break;
case ('S'): {
printf("\nEnter your new pin: ");
scanf("%d", &pin);
getchar();
printf("Confirm your new pin: ");
while ((scanf("%d", &check))) {
if (check != pin) {
getchar();
printf("\nMismatch! Confirm your new pin: ");
}
else if (check == pin) {
getchar();
pin_history[pin_history_index] = pin;
pin_history_index = pin_history_index + 1;
if (pin_history_index >= pin_history_limit) pin_history_index = 0;
printf("\nYour new pin has been saved successfully!\n");
printf("What would you like to do next?\n\n(V)iew your saved pins\n(S)ave another pin\n(E)xit\n");
break;
}
}
break;
}
case ('E'): break;
}
if (choice == 'E') break;
else;
}
printf("\nGoodbye!\n");
return 0;
}
Pass the history array into your new function via a pointer.
I don't know what you mean by "without researching", but that is a very hardcore way to learn C. Respect!
Thanks for the pointers. ๐
Oh, I just meant that I'm experimenting, evaluating and thinking instead of "just" searching for a straight answer on the world wide web.
Edit: Also meaning, that when I post a question here, I have already writhen (spelling?) in pain for a few hours or a day without getting anywhere.
That's fair enough, and a good way to learn. Still, in this day and age with Claude etc, I respect the effort you're putting in.
In the interests of proposing alternatives, you could also make the pin list a global variable. I'll let you work out why its a bad idea though :)