My code is supposed to scan a username and password and make sure that they aren't greater than the max amount. If they are, it should re-prompt the user to enter something.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAXNAME 30
int main() {
char username_input[MAXNAME];
char password_input[MAXNAME];
username_input[MAXNAME - 1] = '\0';
password_input[MAXNAME - 1] = '\0';
printf("Please enter your username: ");
while (scanf("%s", username_input) != 1 || strlen(username_input) > MAXNAME) {
printf("Improper input.\nPlease enter your username: ");
}
printf("Please enter your password: ");
while (scanf("%s", password_input) != 1 || strlen(password_input) > MAXNAME) {
printf("Improper input.\nPlease enter your password: ");
}
return 0;
}
The problem that I am facing is that whenever one of these while loops are entered, I will get the re-prompt fine and I will be able to continue with the code, but once i hit return 0 I will have an exception thrown.
The exception reads: Run-Time Check Failure #2 - Stack around the variable 'username_input' was corrupted.
I have tried using scanf_s and fgets() as well, those haven't seemed to work.