Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,166 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,313 people online right now. Registration is fast and FREE... Join Now!




Validate String data type input by user?

 
Reply to this topicStart new topic

Validate String data type input by user?

shangyi
23 Aug, 2008 - 07:23 AM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 18


My Contributions
Hello guys, i need to write a programme that makes sure the user enters only string data type only:

CODE

#include <stdio.h>
#include <ctype.h>

int main()
{

    char name[21];

    printf("Please enter your name : ");
    scanf("%s", &name);
    
if( isalpha(name) == 0)
printf("Invalid entry! please enter letters only.");
else
printf("Welcome %s", name);

return 0;

}


So, if the user enters a string, then the value of isalpha(name) is a non zero number. But the programme dint work out. and im wondering how do i fix this?

Your help is greatly appreciated.
Thanks.

This post has been edited by shangyi: 23 Aug, 2008 - 07:35 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Validate String Data Type Input By User?
23 Aug, 2008 - 07:26 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
you'll probably want to loop through the char array one digit at a time and check to make sure it is not a number.
User is offlineProfile CardPM
+Quote Post

shangyi
RE: Validate String Data Type Input By User?
23 Aug, 2008 - 07:50 AM
Post #3

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 18


My Contributions
QUOTE(KYA @ 23 Aug, 2008 - 08:26 AM) *

you'll probably want to loop through the char array one digit at a time and check to make sure it is not a number.


thanks for the quick reply biggrin.gif . How do i make the name flexible? like if the user enteres 15 characters, it will automatically adjust to 15. Should i just put name[]? thanks.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Validate String Data Type Input By User?
23 Aug, 2008 - 08:30 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
can you make it a string or do you have to make a char array?
User is offlineProfile CardPM
+Quote Post

shangyi
RE: Validate String Data Type Input By User?
23 Aug, 2008 - 08:57 AM
Post #5

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 18


My Contributions
QUOTE(KYA @ 23 Aug, 2008 - 09:30 AM) *

can you make it a string or do you have to make a char array?


i thought strings consist of character arrays? anyway i need to make a char array. because im learning C right now
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Validate String Data Type Input By User?
23 Aug, 2008 - 10:33 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
QUOTE(shangyi @ 23 Aug, 2008 - 09:57 AM) *

QUOTE(KYA @ 23 Aug, 2008 - 09:30 AM) *

can you make it a string or do you have to make a char array?


i thought strings consist of character arrays? anyway i need to make a char array. because im learning C right now


C++ has a string class, in C all strings are character arrays.

As for your question about how to make the length of a string dynamic:

In C to make a dynamic sized string is a little difficult. One way is to set a maximum size (say 128 chars) and allocate 128 char that can be used.

char maxLenStr[128];

This can hold any string UP TO 127 characters (remember you need a terminating char of 0).

To have truly dynamic abilities you need to use dynamic memory. Generally the way to do this is to allocate a buffer of say 256 chars, and and copy the users input into that buffer (up to 256 chars of users input anyway). Then scroll though and look for a terminating char. If you don't find one, then allocate a larger array and copy the old data into it and release the first, then grab another 256 characters and see if there is a terminting char... if there STILL is not, then again you will have to allocate a larger buffer... you keep this up until you allocate enough memory.

C actually has some functions to help with this kind of thing. realloc will expand the size of your buffer and (if needed) copy over the old info into the new buffer.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 02:56AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month