Hi,
I wanted to create a function which forced a user to enter no more than a
specified number of characters. The code for this is below. The idea was
simple, if a user is only meant to enter 10 characters it would throw away
any remaining characters. This works fine upto10 characters for example:
C:\getstring
Command : 123456789
Command [123456789] :
However when you enter more than 10 characters it does this:
C:\getstring
Command [1234567890] : 12345678901
Command [1234567890] : Command [1234567890] :
Why does it display the command line twice, I've read about the fflush
command but this does not help, does anyone have any ideas.
Many Thanks
Paul
#define MAX_COMMAND_LEN 11
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
/* Function prototypes. */
void init_string(char *string);
void get_string(char *string,int length,char *prompt);
int main(void) {
char command[MAX_COMMAND_LEN];
/* Initialise string. */
init_string(command);
/* Allow user input util the user enters the command 'EXIT'. */
do {
get_string(command,MAX_COMMAND_LEN,"Command");
} while (stricmp(command,"EXIT") != 0);
return 0;
}
/* Get input of a griven string with a specified number of characters. */
void get_string(char *string,int length,char *prompt) {
/*
** I've adjusted the length of the string read by 1, so that in the
event
** of the user entering the exact number of characters, the last
character
** will not be truncated. This occurs because fgets() adds a '\n' +
'\0' to
** the end of the string. The additional character is only required
** temporally to allow the replacement of '\n' with '\0'.
**
** Example: get_string(postcode,8,"Post Code");
**
** Once the user has pressed return.
**
** +---+---+---+---+---+---+---+----+----+
** | B | 1 | 3 | | 0 | D | B | \n | \0 |
** +---+---+---+---+---+---+---+----+----+
**
** After replacing the '\n' character with a '\0' character.
**
** +---+---+---+---+---+---+---+----+
** | B | 1 | 3 | | 0 | D | B | \0 |
** +---+---+---+---+---+---+---+----+
**
** If I did not adjust the number of characters read this happens.
**
** +---+---+---+---+---+---+----+----+
** | B | 1 | 3 | | 0 | D | \n | \0 |
** +---+---+---+---+---+---+----+----+
**
** And then after replacing the '\n' character with a '\0' character.
**
** +---+---+---+---+---+---+----+
** | B | 1 | 3 | | 0 | D | \0 |
** +---+---+---+---+---+---+----+
**
*/
char defvalue[80];
/* Make current value available as a default value. */
strcpy(defvalue,string);
/* Check if the user specified a prompt to be displayed. */
if (strcmp(prompt,"") != 0) {
/* If the string has a previous value then display it as a default.
*/
if (strcmp(string,"") != 0) {
printf("%s [%s] : ",prompt,string);
} else {
printf("%s : ",prompt);
}
}
/* Accept user input and replace the '\n' with '\0'. */
fgets(string,length+1,stdin);
string[strlen(string)-1] = '\0';
/* If the user pressed RETURN use the default value. */
if (strcmp(string,"") == 0) {
strcpy(string,defvalue);
}
}
/* Initialise the string. */
void init_string(char *string) {
strcpy(string,"");
}