5 Replies - 12309 Views - Last Post: 23 August 2008 - 11:33 AM Rate Topic: -----

#1 shangyi  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 15-June 08

Validate String data type input by user?

Post icon  Posted 23 August 2008 - 08:23 AM

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

#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 August 2008 - 08:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Validate String data type input by user?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Validate String data type input by user?

Posted 23 August 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.
Was This Post Helpful? 0
  • +
  • -

#3 shangyi  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 15-June 08

Re: Validate String data type input by user?

Posted 23 August 2008 - 08:50 AM

View PostKYA, on 23 Aug, 2008 - 08:26 AM, said:

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 :D . 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.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Validate String data type input by user?

Posted 23 August 2008 - 09:30 AM

can you make it a string or do you have to make a char array?
Was This Post Helpful? 0
  • +
  • -

#5 shangyi  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 15-June 08

Re: Validate String data type input by user?

Posted 23 August 2008 - 09:57 AM

View PostKYA, on 23 Aug, 2008 - 09:30 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Validate String data type input by user?

Posted 23 August 2008 - 11:33 AM

View Postshangyi, on 23 Aug, 2008 - 09:57 AM, said:

View PostKYA, on 23 Aug, 2008 - 09:30 AM, said:

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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1