944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2215
  • C++ RSS
Mar 22nd, 2006
0

Validation Program

Expand Post »
How can i make a program in c++ which accept only the following:

1) Any positive number.
2)Character from [A to Z] -in upper case.


Note: this program won't work as desired because idon't know how to get the ASCII of any number or character-by using int().

#include<iostream.h>
void main()
{
int a;
cout<<"Enter either number or character to check it's availability "<<endl;

cin>>a;

if(int ('a')>=65 && int('a')<=90) // int('a') always will be 97-the ASCII of character a.
cout<<"It's valid"<<endl;
Similar Threads
Sun
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sun is offline Offline
2 posts
since Mar 2006
Mar 22nd, 2006
0

Re: Validation Program

Get the input as a char and then try validating it's ascii code.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 22nd, 2006
0

Re: Validation Program

In that way i will reserve abig string to sore any length for input because idon't know how to store characters in dynamic array at the first of program so in that way the program will take a useless space
Sun
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sun is offline Offline
2 posts
since Mar 2006
Mar 22nd, 2006
0

Re: Validation Program

Giving you the solution is easier than trying to understand what you are saying.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {<blockquote>char a;
  5. cout<<"Enter either number or character to check it's availability "<<endl;
  6. cin>>a;
  7. if( ( 'a' <= a && a <= 'z' ) ||( 'A' <= a && a <= 'Z' )|| ( '0' <= a && a <= '9' ) )
  8. {<blockquote>cout<<"It's valid"<<endl;</blockquote>}
  9. return 0;</blockquote>}
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 22nd, 2006
0

Re: Validation Program

>Giving you the solution is easier than trying to understand what you are saying.


I second that! Sometimes I have no idea what they mean?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 22nd, 2006
0

Re: Validation Program

>idon't know how to get the ASCII of any number or character
char is a little int, so you don't need to do anything special to get the numeric value of the character (ASCII isn't always used). However, since ASCII isn't always used, and not all character sets are required to have contiguous values for anything but the numeric digit characters, you're much better off using isalpha and isdigit from <cctype>:
C++ Syntax (Toggle Plain Text)
  1. if ( std::isdigit ( c ) || std::isalpha ( c ) )
  2. std::cout<<"Valid";
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Data loss in Type Conversion.
Next Thread in C++ Forum Timeline: HELP!!! Weird problem...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC