Why do some functions return to main() with a newline, even though I am using getchar() to get rid of it?

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

Is there perhaps some other, better way to do it? Perhaps one with which clang doesn't give me "unused result" warnings?

MODS: If you deem it inappropriate for me to continuously ask for guidance here, please let me know. ๐Ÿ˜Š

#include <stdio.h>  

void executechoice(int choice);		
void printsavedpins(int pin_storage[]);  
void promptnewpin(void);  
void checkpinlength(int pin);  
void checkpinmatch(int pin);  
void updatepinstorage(int pin);  

int pin = 0;  
int pin_storage[10] = {0};  
int storage_index = 0;  
const int storage_limit = 10;  

//Prompt user for action.  
int main() {  

    int choice = 0;  
    printf("What would you like to do? (V)iew your saved pins, (S)ave a new pin or (E)xit? "); 
    while ((choice = getchar()) != EOF) {  
        getchar(); //clang gives 'unused result' warning but '\n' needs to be removed.  
        if (choice == 'E' || choice == 'e') { break; }  
        else {  
            executechoice(choice);  
            getchar(); //clang gives 'unused result' warning but '\n' needs to be removed.  
            printf("What would you like to do next? (V)iew your saved pins, (S)ave a new pin or (E)xit? ");  
        }  
    }  
    printf("Goodbye!\n");  
    return 0;  
}  

//Function definitions  
void executechoice(int choice) {		
    if (choice == 'V' || choice == 'v') { printsavedpins(pin_storage); }  
    else if (choice == 'S' || choice == 's') { promptnewpin(); }  
    else { printf("Invalid choice! What would you like to do? (V)iew your saved pins, (S)ave a new pin or (E)xit? "); }  
}  

void printsavedpins(int pin_storage[]) {  
    for (int i = 0; i < storage_limit; i++) printf("Pin %d: %d\n", i + 1, pin_storage[i]);  
}  

void promptnewpin(void) {  
    
    printf("Enter new pin: ");  
    scanf("%d", &pin);  
    checkpinlength(pin);  
}  

void checkpinlength(int pin) {  
    
    if (pin < 1000*100) {  
        printf("Pin has to be at least six digits!\n");  
        promptnewpin();  
    }  
    else checkpinmatch(pin);  
}  

void checkpinmatch(int pin) {  

    int check = 0;  
    printf("Verify your new pin: ");  
    scanf("%d", &check);  
    if (check != pin) {  
        printf("Mismatch! ");  
        promptnewpin();  
    }  
    else updatepinstorage(pin);  
}  

void updatepinstorage(int pin) {  

    pin_storage[storage_index] = pin;  
    storage_index++;  
    if (storage_index >= storage_limit) { storage_index = 0; }  
    printf("New pin saved successfully!\n");  
}  
residentoflaniakea 3 points 5 months ago

You could do a do while loop on the condition of != \n.

path: 0 23839126, hotness: undefined, score: 3, children: 1
akunohana 1 point 5 months ago

Thanks! I'll look into it! ๐Ÿ˜Š

path: 0 23839126 23840526, hotness: undefined, score: 1, children: 0
SpaceNoodle 3 points 5 months ago

I'm going to avoid commenting on anything else in your code and just say that you should be using getline() to read a line of input from the user. Also note that yor extra call to getchar() is throwing away the user's first character of input after a command is executed.

path: 0 23838744, hotness: undefined, score: 3, children: 1
akunohana 1 point 5 months ago

Thanks! I'll look into getline() tomorrow! ๐Ÿ˜ƒ๐Ÿฉต

path: 0 23838744 23838965, hotness: undefined, score: 1, children: 0
c_lang
c_lang

@programming.dev

login for more options
1357
103
28

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...