this post was submitted on 25 Apr 2026
1 points (100.0% liked)

Programming

27045 readers
104 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 3 years ago
MODERATORS
 

Good day! If I may be so bold as to ask a rudimentary question, I stumbled upon a phenomenon that I don't seem to fully understand.

If I define the length of intCheck with [] or [1], the program goes into an endless loop from the very start. No user input required. If, however, I define it as [2] or greater, the program works as intended, as long as the user input is less than the number of bytes defined in the char array. If, however, the user input is greater than the predefined byte size, the program repeats the else condition of the while loop - here, printf - the number of times that corresponds with the byte size defined for char intCheck. Is this an overflow of choice?

//Bank of Haruhi

//TODO
// - Create user account: require user first name, last name, user name, password, age (deny if < 20 yrs)
// - Prompt login (deny if != password && username)
// - Display menu ("About the Bank of Haru", "Account Settings", "Check your balance", "Deposit", "Withdraw", "Close account", "Logout")

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

int main(void) {

//Base 
char intCheck[3] = ""; //Will "if (sscanf(char,%d,&int) == true)" check for ANY "char" or a predetermined amount of "char"?

//Registration and login
char firstName[] = "";
char lastName[] = "";
char password[] = "";
int age = 0;

//Menues and selections
int choice = 0;
int arithmeticChoice = 0;

//Balance, deposit and withdrawal
float balance = 0.0;
float deposit = 0.0;
float withdraw = 0.0;

printf("Welcome to the Bank of Haruhi! Please login (1) or create an account (2): ");

while (fgets(intCheck, sizeof(intCheck),stdin)) {
intCheck[strlen(intCheck) - 1] = '\0';
if (sscanf(intCheck, "%d", &choice) == true && sizeof(intCheck) == 4 && choice == 1 || choice == 2) break; //I'm keeping "sizeof(intCheck)" in order to excercise byte size input validation, but I could just remove it and let "if choice == 1 || 2" restrict the valid input.
else printf("Please enter ""1"" to login or ""2"" to create a new account: ");
}

return 0;

}

The program is, of course, not nearly complete. I just stopped doing anything else, as soon as I stumbled upon this phenomenon.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here