473,395 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1399

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
28
by: David Rubin | last post by:
I looked on google for an answer, but I didn't find anything short of using boost which sufficiently answers my question: what is a good way of doing string tokenization (note: I cannot use boost)....
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
by: SeNTry | last post by:
Hi Everyone, My first post here as I just begin to learn programming in general and python in particular. I have all the noobie confused questions, but as I work thru the tutorials I'm sure...
5
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
7
by: php_mysql_beginer911 | last post by:
Hi .. hope someone will help i am trying to figure it out why i cannot post string "union select" every time i try to post data which content union and select .. the page doesn't get posted and...
9
by: WebArchitect | last post by:
I have a text file. My text file contains lines with double quotes around it. I trying to code if a line has double quotes around it Then make it bold My code is below and it is not working....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.