THANK YOU ALL!!! - what errors should I check for and handle?

5 months ago by printf("%s", name); to c/c_lang

Thank you all for your kind, patient and educative responses when I obnoxiously post amateur questions! 💙 While I cannot make any promises because of how my brain works, I am almost ready to continue reading The C Programming Language, 2nd Edition. I just want to experiment a little bit with error handling, specifically how to handle wrong input (char VS. int, etc.) and also to learn to indentify code that runs the risk of overflow/underflow.

Question: what errors do you recommend checking for and handling?

Meanwhile, thank you all! 🥰

#include <stdio.h>  

//Function declarations  
int newPin();  
int checkPin(int i);  

//Program that prompts for, verifies and saves pins temporarily into an array  
int main() {  

    //New pin  
    int pin = 0;  

    //History  
    int history[10] = {0,0,0,0,0,0,0,0,0,0};  
    int history_limit = 10;  
    int history_index = 0;  

    printf("Hello there! What would you like to do? (V)iew your saved pins, (S)ave a new pin or (E)xit: ");  
    int choice = 0;  
    while ((choice = getchar()) != EOF) {  
        switch (choice) {  
            case ('V'): { //Display saved pins  
                printf("\nYour saved pins are:\n\n");  
                for (int i = 0; i < history_limit; i++) printf("%d\n", history[i]);  
                printf("\nWhat would you like to do next? (V)iew your saved pins, (S)ave a new pin or (E)xit: ");  
                break;  
            }  
            case('S'): { //Prompt for and verify newly entered pin  
                pin = newPin();  
                if (checkPin(pin) == pin) {  
                    history[history_index] = pin;  
                    history_index++;  
                    if (history_index >= history_limit) history_index = 0;  
                }  
                break;  
            }  
            case ('E'): goto EXIT; //Terminate program  
        }  
    }  
EXIT:	printf("\nGoodbye!\n");  
    return 0;  
}  

//Function definitions  
//Prompt user to enter a new pin  
int newPin() {  
    
    int pin = 0;  
    
    printf("This enter your pin: ");  
    scanf("%d", &pin);  
    getchar();  

    return pin;  
}  

//Verify newly entered pin  
int checkPin (int i) {  

    int check = 0;  
    
    printf("Confirm your new pin: ");  
    while((scanf("%d", &check)) != EOF) {  
        if (check != i) printf("Mismatch! Confirm your new pin: ");  
        else if (check == i) { 
            printf("Success! Your new pin is %d. What would you like to do next? (V)iew your saved pins, (S)ave a new pin or (E)xit: ", i);  
            goto EXIT;  
        }  
    }  
EXIT:	return i;  
}  

//TODO  
//Error handling (overflow, input data type, other?)  
pelya 3 points 5 months ago

scanf gives me shivers. Please use -Werror=format compiler flag to catch dumb errors that will make your code crash in mysterious ways.

path: 0 23680762, hotness: undefined, score: 3, children: 0
avidya 2 points 5 months ago

Did you know that

    int history[10] = {0,0,0,0,0,0,0,0,0,0};  

is the same thing as

        int history[10] = {0};

And an idom in C would be to define a constant for the size like this

enum { HISTORY_MAX = 10};
int history[HISTORY_MAX] = {0};
int history_limit = HISTORY_MAX;
path: 0 23694978, hotness: undefined, score: 2, children: 2
akunohana 1 point 5 months ago

Cool! Thanks! I did read somewhere that if I'm certain that a variable isn't going to have to change its value throughout the program, then it's better to make it a constant. Out of security concerns? Anyway, thank you so much! 🥰

path: 0 23694978 23695451, hotness: undefined, score: 1, children: 1
vanillama 2 points 5 months ago

It's in case you have to change those values later, you will probably miss one instance and get issues down the line. It also makes it more readable for other people (or yourself in a few months lol)

path: 0 23694978 23695451 23697086, hotness: undefined, score: 2, children: 0
c_lang
c_lang

@programming.dev

login for more options
1359
104
35

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

go to feed...