473,320 Members | 2,133 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,320 software developers and data experts.

String Length Validation

DanielTNBaker
Basically i would like to know how to validate the length of a string

Expand|Select|Wrap|Line Numbers
  1. printf("\n\n\n\n\t Please Enter the Name of the Client: ");
  2.    fflush(stdin);
  3.    gets(client.name);
  4.    do
  5.    {
  6.        if ()
  7.        {
  8.                            printf("ERROR - Length of Name to Long")
  9.                            printf("\n\n\n\n\t Please Enter the Name of the Client: ");
  10.                            fflush(stdin);
  11.                            gets(client.name);
  12.        }
  13.    }while ();
this is what i got but im not sure where to go from here basically i want to say pretty much if the name is more than 30 characters print an error message and then repeat the prompt.

if anyone can help would be much appreciated
Mar 16 '07 #1
1 2937
horace1
1,510 Expert 1GB
I assume client.name is a char[] of char * pointing to a char array.
You can use the strlen() function to determine how many characters are in the string
http://www.cplusplus.com/reference/clibrary/cstring/

make sure that your array is sufficently long to take the worst case - it is probably better to use fgets() where you specify the maximum number of characters that can be read
http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html

note that fgets() leaves the \n newline character in the string (gets() does not)
Mar 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: David Graham | last post by:
Hi I have been busy going through the last weeks postings in an attempt to absorb javascript syntax (I guess it's not possible to just absorb this stuff in a passive way - I'm getting way out of...
1
by: Abra | last post by:
Is there any default maximal length for strings in the .NET inmplementation of XML schema validation (I used a XmlSchemaCollection in a XmlValidatingReader object) ? I am getting the following...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
4
by: moondaddy | last post by:
Is there a asp.net validator control that validates the length of the text being entered or does everyone just write jscript for this? -- moondaddy@nospam.com
10
by: A.M | last post by:
Hi, How can I use validation controls to check max length of string text boxes? Thanks, Alan
6
by: Edward | last post by:
I need to validate a text box entry, but ONLY if it is 17 characters, otherwise I have to ignore it. My regular expression for the validation is: ^(({9})()()(\d{6}))$ Can I adapt this to...
4
by: Trapulo | last post by:
I've a webservice with a string parameter. I call this webservice passing a string that is 1129 chars length. On the webservice, the received string is 1146 length (I tested sizes with...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
14
by: Rob | last post by:
I am trying to perform client-side input validation for a textarea to determine that the number of characters doesn't exceed a certain length. Currently, I am just using str.length, but if the...
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.