Validation Program

This is a discussion on Validation Program within the C++ Programming forums, part of the General Programming Boards category; How can i make a program which accept only the following: 1) Any positive number. 2)Character from [A to Z] ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Validation Program

    How can i make a program 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().

    Code:
    #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;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Integers don't accept characters. Essentially, ASCII is a 1 byte integer value of a character. The only way to do this is to input a string and then check the string to see if it's valid.

    Also, main() should be declared as an int, not void.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    use ctype.h library functions.
    check these out:
    isalpha(),islower(),isdigit(),isupper()

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Echooo
    Code:
    if(int ('a')>=65 && int('a')<=90) // int('a') always will be 97-the ASCII of character a.
    cout<<"It's valid"<<endl;
    You said it yourself... 'a' is the ASCII character of the english letter A in lowercase... and int('a') is the numerical ASCII representation. You don't want to be using either, because ASCII values are constants.

    on the other hand, a (without quotes) is your variable - so get rid of those single quotes. Also, you don't need the cast, because your variable a is already an int.
    Code:
    if( (a>=65) && (a<=90) ) //...

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by SlyMaelstrom
    Integers don't accept characters.
    That's not quite true - characters are just a representation of an integer. The difference is how cin and cout will interpret the integer.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int x('c');
        cout << x << endl;
        cout << char(x) << endl;
        cin.get();
    }

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by Bench82
    That's not quite true - characters are just a representation of an integer. The difference is how cin and cout will interpret the integer.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int x('c');
        cout << x << endl;
        cout << char(x) << endl;
        cin.get();
    }
    Of course it's true because the assignment gets cast before it's assigned. Sure you could say an integer equals 'c' or a character equals 65, but regardless, it's changed before it's assignment. Anyway, the OP wanted to be able to accept character and integers, in otherwords, he'd want to accept 'A' and 65 as different values.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21