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;

Get the input as a char and then try validating it's ascii code.

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

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

#include<iostream>
using namespace std;
int main()
{[INDENT]char a;
cout<<"Enter either number or character to check it's availability "<<endl;
cin>>a;
if( (  'a' <= a && a <= 'z' ) ||( 'A' <= a && a <= 'Z' )|| ( '0' <= a && a <= '9' ) )
{
[INDENT]cout<<"It's valid"<<endl;[/INDENT]
}
return 0;[/INDENT]}
Member Avatar for iamthwee

>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?

>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>:

if ( std::isdigit ( c ) || std::isalpha ( c ) )
  std::cout<<"Valid";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.