9

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;  
}  
you are viewing a single comment's thread
view the rest of the comments
[-] CameronDev@programming.dev 2 points 12 hours 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!

[-] akunohana@piefed.blahaj.zone 2 points 11 hours ago* (last edited 11 hours 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 2 points 11 hours 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 :)

this post was submitted on 08 May 2026
9 points (100.0% liked)

C Programming Language

1295 readers
6 users here now

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

ยฉ Dennis Ritchie

๐ŸŒ https://en.cppreference.com/w/c

founded 2 years ago
MODERATORS