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;  
}  
  • AlSweigart@programming.dev
    link
    fedilink
    arrow-up
    8
    ·
    12 hours ago

    The else isn’t needed, as others have said. I’d find, install, and configure a linter tool, which can give you pointers on small stylistic issues like this.

  • redlemace@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    12 hours ago

    an " if … else … " always needs an else to be an “if … else” statement ;)

    but you can just create an "if… " statement So you can change

    		}  
    		if (choice == 'E') break;  
    		else;  
    	}  
    	printf("\nGoodbye!\n");  
    	return 0;  
    }  
    
    

    into

    		}  
    		if (choice == 'E') break;  
    	}  
    	printf("\nGoodbye!\n");  
    	return 0;  
    }  
    
    
  • CameronDev@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    12 hours ago

    The else is definitely not needed, you can delete it.

    As for the best way to climb out of a nested loop, this is one of the occasions that a goto is reasonable. The jump would be relatively short and localised, which is exactly what GOTO should be used for:

    What’s with the getchars slapped everywhere? I think its an attempt to flush stdin, but I think you need to do it repeatedly until you hit a newline or EOF.

    I think your next step is to refactor the code into some functions, you have two nearly identical code sections for adding a new pin, you can make that a function and simplify it.

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

      “Slapped” 🤣 I think I forgot to remove the getchars after having used scanf, I had learned here. As you said, to clean up the input buffer.

      I had JUST written down in my TODO list to make a function of it! 😁

      Thank you so much! 😊