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;

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]}

>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";
This article has been dead for over six months. Start a new discussion instead.