[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

how can i validate that a string only contains digits

Posted on 2003-03-01
5
Medium Priority
?
554 Views
Last Modified: 2010-04-15
I need to check that a string only contains digits and is of a fixed length using Visual C++. The string that I want to check is on a separate dialog box and assigned to a variable. I want to validate the field before exiting so that I can force the user to enter in the correct format. I presume that the call will occur when clicking 'OK' on that dialog box.
0
Comment
Question by:daveyj1572
5 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8048839
This is a duplicate question. Please post here,
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20534809.html

Exceter
0
 

Expert Comment

by:ramyaniv
ID: 8053628
You should read the string into a char* variable and check it's strlen(), then you can use the assci values and see for each character if it's value is >47 and <58 (where 48 is '0' and 57 is '9')
0
 

Expert Comment

by:sarkalgud
ID: 8066597
For each character of the string, until you hit end of line (\0)
   Check whether the character is a digit


0
 
LVL 1

Accepted Solution

by:
sarda_ramesh earned 200 total points
ID: 8069696
the code will be somewhat like this ..
..
..
..
flag=0;
len = strlen(str);/* str is the number in string format*/
for(int i=0;i<len;i++)
   if(!isdigit(str[i]))flag=1;
if(flag=0)
  /* the string contains only digits*/
else
  /* it contains other characters*/
..
..
..

isdigit is defined in "ctype.h"..

in case of any problem .. u can write it of urt own as ..

int isdigit(char c)
{
  if(c < '0' or c > '9') return 0;
  else return 1;
}

i hope this will solve ur prob.

regards
ramesh
0
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8078421
Thank U daveyj1572
0

Featured Post

IT Degree with Certifications Included

Aspire to become a network administrator, network security analyst, or computer and information systems manager? Make the most of your experience as an IT professional by earning your B.S. in Network Operations and Security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question