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;  
}  
  • printf("%s", name);@piefed.blahaj.zoneOP
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    9 days ago

    I’ve been at it for two days without researching and I can’t figure out how to “hand over” the updates of the history array from within my “new pin” function to the main() function. I have to learn about scopes. Good fun!

    • CameronDev@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      9 days ago

      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!

      • printf("%s", name);@piefed.blahaj.zoneOP
        link
        fedilink
        English
        arrow-up
        2
        ·
        9 days ago

        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.

        • CameronDev@programming.dev
          link
          fedilink
          arrow-up
          2
          ·
          9 days ago

          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 :)