The problem I am having is that this program is menu driven. When I enter the letter "i" is inputted in the input
array which has a size of MAX_LENGTH+1
. Through GDB I found out that "i" is inputted in the 0th index of the input
array and the rest of the spaces are inputted with the NULL_CHAR
character. Anyways when I press "i" for the insert menu, I have greeted by a field which tells me to enter a value. I enter any integer and press inter. Instead of being greeted by the "Command?:" field and give me a chance to put an entry, it immediately tells me that my input is invalid and tells me to enter a command again. Here is what I mean:
Commands are I (insert), D (delete), S (search by name),
V (search by value), P (print), Q (quit).
Command?: i
45
Command?:
Invalid command.
Commands are I (insert), D (delete), S (search by name),
V (search by value), P (print), Q (quit).
Command?:
I found out that the reason this is happening is because when the safegets function is called again, for some reason the local variable c
in the function safegets has a value of NULL_CHAR
, presumably from the fact that all the other values in the input char array had all other entries as NULL_CHAR
. I do not understand why c
is automatically assigned the value of NULL_CHAR because in the while loop, because there's a statement c = getchar()
that should be asking for my input again. But for some reason after every entry, c
's default value becomes NULL_CHAR and asks you for your input the next time safegets
is called.
Here is my what I want my output to be like:
Commands?are?I?(insert),?D?(delete),?S?(search?by?name),?
??V?(search?by?value),?P?(print),?Q?(quit).?
?
Command?:?I?
??value:?50000?
?
Command?:?I
value:
Here is the main function:
const int MAX_LENGTH = 1023;
const char NULL_CHAR = '';
const char NEWLINE = '
';
int main (void)
{
const char bannerString[]
= "Personal Team Maintenance Program.
";
const char commandList[]
= "Commands are I (insert), D (delete), S (search by name),
"
" V (search by value), P (print), Q (quit).
";
// Declare linked list head.
// ADD STATEMENT(S) HERE TO DECLARE LINKED LIST HEAD.
// announce start of program
printf("%s",bannerString);
printf("%s",commandList);
char response;
char input[MAX_LENGTH+1];
int value;
do
{
printf("
Command?: ");
safegets(input,MAX_LENGTH+1);
// Response is first char entered by user.
// Convert to uppercase to simplify later comparisons.
response = toupper(input[0]);
if (response == 'I')
{
// Insert a player entry into the linked list.
// Maintain the list in correct order (G, D, M, S).
// ADD STATEMENT(S) HERE
// USE THE FOLLOWING PRINTF STATEMENTS WHEN PROMPTING FOR DATA:
// printf(" family name: ");
// printf(" first name: ");
// printf(" position: ");
printf(" value: ");
scanf("%d", value);
}
else if (response == 'D')
{
// Delete a player from the list.
printf("
Enter family name for entry to delete: ");
// ADD STATEMENT(S) HERE
}
else if (response == 'S')
{
// Search for a player by family name.
printf("
Enter family name to search for: ");
// ADD STATEMENT(S) HERE
}
else if (response == 'V')
{
// Search for players that are worth less than or equal a value.
printf("
Enter value: ");
// ADD STATEMENT(S) HERE
}
else if (response == 'P')
{
// Print the team.
// ADD STATEMENT(S) HERE
}
else if (response == 'Q')
{
; // do nothing, we'll catch this case below
}
else
{
// do this if no command matched ...
printf("
Invalid command.
%s
",commandList);
}
} while (response != 'Q');
// Delete the whole linked list that hold the team.
// ADD STATEMENT(S) HERE
// Print the linked list to confirm deletion.
// ADD STATEMENT(S) HERE
return 0;
}
Helper function being called:
void safegets (char s[], int arraySize)
{
int i = 0, maxIndex = arraySize-1;
char c;
while (i < maxIndex && (c = getchar()) != NEWLINE)
{
s[i] = c;
i = i + 1;
}
s[i] = NULL_CHAR;
}
See Question&Answers more detail:
os