#getchar — Public Fediverse posts
Live and recent posts from across the Fediverse tagged #getchar, aggregated by home.social.
-
Why do some functions return to main() with a newline, even though I am using getchar() to get rid of it?
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 inappropirate 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"); } -
Why do some functions return to main() with a newline, even though I am using getchar() to get rid of it?
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 inappropirate 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"); }