Skip to content
Snippets Groups Projects
Commit 008a2371 authored by Franza, Simone's avatar Franza, Simone
Browse files

clear buffer after wrong input

parent 7c221c24
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ void generatePlayfairSquare(char *square)
printf("%s ", KEY_PROMPT);
fgets(key, SIZE_BUFFER, stdin);
if(checkStringValidity(key, MAX_KEY_LENGTH, IS_KEY)) break;
clearBuffer(key, SIZE_BUFFER);
}
prepareKey(key);
......@@ -121,7 +122,8 @@ void appendAlphaToKey(char *key)
{
char alpha[SQUARE_SIDE * SQUARE_SIDE + 1] = ALPHA;
int length = stringLength(key);
for(int counter = 0; counter < SQUARE_SIDE * SQUARE_SIDE + 1; counter++) {
for(int counter = 0; counter < SQUARE_SIDE * SQUARE_SIDE + 1; counter++)
{
if(stringContainsChar(key, alpha[counter]) == NOT_FOUND)
*(key + length++) = alpha[counter];
}
......@@ -129,9 +131,19 @@ void appendAlphaToKey(char *key)
}
//-----------------------------------------------------------------------------
int stringContainsChar(char *text, char to_find) {
int stringContainsChar(char *text, char to_find)
{
do {
if(*text == to_find) return FOUND;
} while(*text++);
return NOT_FOUND;
}
//-----------------------------------------------------------------------------
void clearBuffer(char *text, int buffer_len)
{
for(int counter = 0; counter < buffer_len; counter++)
{
text[counter] = '\0';
}
}
......@@ -131,3 +131,11 @@ void appendAlphaToKey(char *key);
// @return FOUND(1) or NOT_FOUND(-1)
//
int stringContainsChar(char *text, char to_find);
//-----------------------------------------------------------------------------
// Zeroes the content of a string
//
// @param text string to clear
// @param buffer_len length of string
//
void clearBuffer(char *text, int buffer_len);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment