?
Solved

how can i validate that a string only contains digits

Posted on 2003-03-01
5
Medium Priority
?
544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

765 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