469,578 Members | 1,869 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

String problem, I guess?

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,"");

}

Jul 31 '06 #1
0 1169

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by David Rubin | last post: by
32 posts views Thread by tshad | last post: by
4 posts views Thread by SeNTry | last post: by
6 posts views Thread by tommaso.gastaldi | last post: by
87 posts views Thread by Robert Seacord | last post: by
26 posts views Thread by Hardy Wang | last post: by
7 posts views Thread by php_mysql_beginer911 | last post: by
9 posts views Thread by WebArchitect | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.