the entered character is a lowercase, uppercase, or a real number

This is a discussion on the entered character is a lowercase, uppercase, or a real number within the C++ Programming forums, part of the General Programming Boards category; Hi I'm trying to write a simple programme to find if the entered character is a lowercase, uppercase, or a ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    the entered character is a lowercase, uppercase, or a real number

    Hi

    I'm trying to write a simple programme to find if the entered character is a lowercase, uppercase, or a real number. Please don't forget I'm a beginner and know very little. So, please explain in as much detail as possible. I have a rough draft. I know there are two or more ways to do this but someone wants me to do this 'terrible' way. So, please help me. I can understand the look of the code might frustrate you. It did me. I'm sorry for that.

    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    
    {
    	char r;
    	cout << "Enter the letter" << endl;
    	cin >> r;
    	if ( (r=='A')||(r=='B')||(r=='C')||(r='D')||(r=='E')||(r=='F')||(r=='G')||(r=='H')||          
            (r=='I')||(r=='J')||(r=='K')||(r=='L')||(r=='M')||(r=='N')||(r=='O')||(r=='P')||(r=='Q')||    
            (r=='R')||(r=='S')||(r=='T')||(r=='U')||(r=='V')||(r=='W')||(r=='X')||(r=='Y')||(r=='Z') )
    	cout << "r is uppercase" << endl;
    	
            else
    	if ( (r=='a')||(r=='b')||(r=='c')||(r='d')||(r=='e')||(r=='f')||(r=='g')||(r=='h')||(r=='i')|| 
            (r=='j')||(r=='k')||(r=='l')||(r=='m')||(r=='n')||(r=='o')||(r=='p')||(r=='q')||(r=='r')||
            (r=='s')||(r=='t')||(r=='u')||(r=='v')||(r=='w')||(r=='x')||(r=='y')||(r=='z') )
    	cout << "r is lowercase" << endl;
    	
            else
    	if (r is a real number)
    	else
    	???
    	getch();
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    As I said in the other thread, you shouldn't use conio.h, you should replace getch() with cin.get(), add return 0; to the end of main() and fix your indentation.

    Now, what stands out to me here is that you can use <, <=, >, >= on char values to test ranges just like with numbers, but it appears you don't know this. It means you can replace:

    Code:
    if ( (r=='A')||(r=='B')||(r=='C')||(r='D')||(r=='E')||(r=='F')||(r=='G')||(r=='H')||          
            (r=='I')||(r=='J')||(r=='K')||(r=='L')||(r=='M')||(r=='N')||(r=='O')||(r=='P')||(r=='Q')||    
            (r=='R')||(r=='S')||(r=='T')||(r=='U')||(r=='V')||(r=='W')||(r=='X')||(r=='Y')||(r=='Z') )
    with

    Code:
    if ('A' <= r && r <= 'Z')
    Also, where you have "???", don't you want to print an error message about invalid input?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Mozza314.

    It is an incomplete code. The problem is the person who is instructing is actually 'forcing' me to use his old ways. What can I do then? I have been told my codes contain many things which are now outdated. Next week I'm going to argue with that person.

    I will come to your simple and neat method later. First I want to do it this way in all its nastiness.

    At the top I declare "r" a character. Isn't it wrong? Because I'm trying to write a programme which tell if "r" is lowercase, uppercase, or a real number. Can a "char" be a real number?

    Bold part is incomplete. I need your help there.

    One thing more, let's say, when I write the blue line in Dev-C++ it never ends. I have to scroll left and right to see the line. Isn't there some way that I could wrap this lengthy line into sections as they appear here? Do you get what I'm saying?

    Please now help me.

    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    
    {
    	char r;
    	cout << "Enter the letter" << endl;
    	cin >> r;
    	if ( (r=='A')||(r=='B')||(r=='C')||(r='D')||(r=='E')||(r=='F')||(r=='G')||(r=='H')||          
            (r=='I')||(r=='J')||(r=='K')||(r=='L')||(r=='M')||(r=='N')||(r=='O')||(r=='P')||(r=='Q')||    
            (r=='R')||(r=='S')||(r=='T')||(r=='U')||(r=='V')||(r=='W')||(r=='X')||(r=='Y')||(r=='Z') )
    	        cout << "r is uppercase" << endl;
    	
            else if ( (r=='a')||(r=='b')||(r=='c')||(r='d')||(r=='e')||(r=='f')||(r=='g')||(r=='h')||  
            (r=='i')||(r=='j')||(r=='k')||(r=='l')||(r=='m')||(r=='n')||(r=='o')||(r=='p')||(r=='q')|| 
            (r=='r')||(r=='s')||(r=='t')||(r=='u')||(r=='v')||(r=='w')||(r=='x')||(r=='y')||(r=='z') )
    	        cout << "r is lowercase" << endl;
    	
            else
    	if (r is a real number)
    	else
    	???
    	getch();
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    A char is actually a small numeric type. You may want to take a look at this link Variables go to the section titled "Fundamental data types". Also this link about the ASCII Characters will show the numeric values for the alphabet.

    Jim

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612
    Can a "char" be a real number?
    Not really. And "real number" is the wrong term even if you're talking about something stored in a double. You'd need infinite precision to store real numbers. The correct term for these values in computer science is "floating point value".

    Anyway, there's two ways I see to do this within the limits of where you're programming knowledge is - either use peek() or figure out how to convert a string into a floating point value.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jackson6612 View Post
    Thanks a lot, Mozza314.

    It is an incomplete code. The problem is the person who is instructing is actually 'forcing' me to use his old ways. What can I do then? I have been told my codes contain many things which are now outdated. Next week I'm going to argue with that person.

    I will come to your simple and neat method later. First I want to do it this way in all its nastiness.

    At the top I declare "r" a character. Isn't it wrong? Because I'm trying to write a programme which tell if "r" is lowercase, uppercase, or a real number. Can a "char" be a real number?

    Bold part is incomplete. I need your help there.

    One thing more, let's say, when I write the blue line in Dev-C++ it never ends. I have to scroll left and right to see the line. Isn't there some way that I could wrap this lengthy line into sections as they appear here? Do you get what I'm saying?

    Please now help me.

    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    
    {
    	char r;
    	cout << "Enter the letter" << endl;
    	cin >> r;
    	if ( (r=='A')||(r=='B')||(r=='C')||(r='D')||(r=='E')||(r=='F')||(r=='G')||(r=='H')||          
            (r=='I')||(r=='J')||(r=='K')||(r=='L')||(r=='M')||(r=='N')||(r=='O')||(r=='P')||(r=='Q')||    
            (r=='R')||(r=='S')||(r=='T')||(r=='U')||(r=='V')||(r=='W')||(r=='X')||(r=='Y')||(r=='Z') )
    	        cout << "r is uppercase" << endl;
    	
            else if ( (r=='a')||(r=='b')||(r=='c')||(r='d')||(r=='e')||(r=='f')||(r=='g')||(r=='h')||  
            (r=='i')||(r=='j')||(r=='k')||(r=='l')||(r=='m')||(r=='n')||(r=='o')||(r=='p')||(r=='q')|| 
            (r=='r')||(r=='s')||(r=='t')||(r=='u')||(r=='v')||(r=='w')||(r=='x')||(r=='y')||(r=='z') )
    	        cout << "r is lowercase" << endl;
    	
            else
    	if (r is a real number)
    	else
    	???
    	getch();
    }

    Take a look in the C headers for isalpha(), isupper(), islower(), isdigit() and atof().
    Might make your life a little easier.

  7. #7
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    One thing more, let's say, when I write the blue line in Dev-C++ it never ends. I have to scroll left and right to see the line. Isn't there some way that I could wrap this lengthy line into sections as they appear here? Do you get what I'm saying?
    There's nothing stopping you from wrapping it like you have in the code you posted here. There's nothing in C++ that says the condition in an if statement has to be on one line.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi again

    This is a better and simple program which I've written after guidance from you guys. Why do I have to enclose A, Z, 0, and 9 within single quotes? Otherwise my code didn't work. (Mozza, I'm sorry if there is a problem with indentation. I don't know something goes wrong when I paste the code here.).


    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    
    {
    	char r;
    	cout << "Input = " ;
    	cin >> r;
    	if ( r >= 'A' && r <= 'Z')
    	   cout << "the entry is a capital letter" << endl;
            else if ( r >= 'a' && r <= 'z' )
               cout << "the entry is a small letter" << endl;
            else if ( r >= '0' && r <= '9' )
              cout << "the entry is a digit" << endl;
            else 
             cout << "Input error" << endl;
     
        getch();
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    Why do I have to enclose A, Z, 0, and 9 within single quotes?
    The single quotes denote a char constant.

    Example
    Code:
    char ch = 'A';
    Jim

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by jimblumberg View Post
    The single quotes denote a char constant.

    Example
    Code:
    char ch = 'A';
    Jim
    Thanks, Jim.

    In the following processing statement "3.142" is also constant.
    Code:
    A = 3.142*r*r
    How does a character constant differ from a general constant? Perhaps, a character constant is special standardized constant which has a symbol, used with single quotes and recognized by the system.

    I suspect ASCII standardizes these constants and allots them symbols.
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Please correct me. Thank you.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  11. #11
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    Why do I have to enclose A, Z, 0, and 9 within single quotes? Otherwise my code didn't work.
    Good question. The reason is that when you want the actual character, J, for example, the compiler needs to know that you mean J the character and not J the variable. E.g.:

    Code:
    int main()
    {
        int J = 3;
        cout << J << endl; // outputs 3
        cout << 'J' << endl; // outputs J
        return 0;
    }
    Of course it's conceivable you could make a language in which if there was no variable J available then
    Code:
    cout << J << endl;
    would output J the character instead. However, this makes code difficult to read because someone reading it who hasn't seen the whole program will probably expect J stores some value that is being printed. For this reason, in C++ and other languages, you need to put quotes around the letter whenever you mean the letter itself and not a variable.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Mozza314 View Post
    Good question. The reason is that when you want the actual character, J, for example, the compiler needs to know that you mean J the character and not J the variable. E.g.:

    Code:
    int main()
    {
        int J = 3;
        cout << J << endl; // outputs 3
        cout << 'J' << endl; // outputs J
        return 0;
    }
    Of course it's conceivable you could make a language in which if there was no variable J available then
    Code:
    cout << J << endl;
    would output J the character instead. However, this makes code difficult to read because someone reading it who hasn't seen the whole program will probably expect J stores some value that is being printed. For this reason, in C++ and other languages, you need to put quotes around the letter whenever you mean the letter itself and not a variable.
    Yes, I think I understand it now. Thanks Mozza. You have been very helpful through out without any air of arrogance!

    Okay, when I use double quotation marks I have text
    When I simply enter something, such as A, B, C, etc, it is expected to be a variable.
    When I use single quotes then it's a character constant - a constant of special type which has a particular symbol and recognized by the system. But what if I try to use some self-invented character symbol (I see '+' is also taken as a character) which has been standardized by ASCII, then would it give error?

    Is this correct? Please correct me if I'm wrong.


    Now some more questions. My questions will appear in the code following "//". It would be nice of you if you could shed some light. (sorry, if there is a big issue with indentation, when I copy the code here something goes wrong)

    Code:
    #include <iostream>
    # include <cmath>
    #include <cstdlib> // these are header files?
    
    using namespace std; // this is "using" statement?
    
    int main () // this is the function which we are using?
    
    {
        float a, b, c, R1, R2; // this is the declaration statement?
        
        system("Color 1A"); // this is what? I use it to change the color of the console.
        
        cout << "Enter a, b, c of the equation ax^2 + bx + c = 0" << endl;
        cout << "Enter a = ";
        cin >> a; // what are cout and cin statements called?
        
        cout << "Enter b = ";
        cin >> b;
        
        cout << "Enter c = ";
        cin >> c;
        
    	float D = b*b - 4*a*c; // this both declaration and processing statement?
        if (D >= 0)
        {
            cout << "R1 is = " << ( -b + sqrt(D) )/( 2*a ) << endl;
            cout << "R2 is = " << ( -b - sqrt(D) )/( 2*a ) << endl;
        } // this is if statement?
        
    	else
        {
            cout << "no real roots exist" << endl;
        }
        
        system("PAUSE"); // what would called this? "system" specific statement?
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  13. #13
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    When I simply enter something, such as A, B, C, etc, it is expected to be a variable.
    When I use single quotes then it's a character constant - a constant of special type which has a particular symbol and recognized by the system. But what if I try to use some self-invented character symbol (I see '+' is also taken as a character) which has been standardized by ASCII, then would it give error?
    What do you mean "self-invented"? '+' is a perfectly normal character. The single quotes don't just apply to letters. In the case of '+', you're telling the compiler that you mean '+' the character and not + the operator that adds numbers together.


    Quote Originally Posted by jackson6612 View Post
    Code:
    #include <iostream>
    # include <cmath>
    #include <cstdlib> // these are header files?
    // Yes
    
    using namespace std; // this is "using" statement?
    Yes; it makes everything in the namespace std accessible without prefixing it with "std::".
    // E.g. without it you'd have to write "std::cout" in place of "cout".
    
    int main () // this is the function which we are using?
    // Yes; main() is a special function which is marks the start of execution of the program
    {
        float a, b, c, R1, R2; // this is the declaration statement?
        // This is where you create the float variables, it's known as "declaration", yes
        
        system("Color 1A"); // this is what? I use it to change the color of the console.
        // What's your question? It's a call to the function system().
        
        cout << "Enter a, b, c of the equation ax^2 + bx + c = 0" << endl;
        cout << "Enter a = ";
        cin >> a; // what are cout and cin statements called?
        // They don't really have a special name. ">>" is sometimes called the
        // extraction operator and "<<" the insertion operator if that's what you're asking.
        
        cout << "Enter b = ";
        cin >> b;
        
        cout << "Enter c = ";
        cin >> c;
        
        float D = b*b - 4*a*c; // this both declaration and processing statement?
        // Yes, the value b*b - 4*a*c is calculated and then D is initialised with that value
        if (D >= 0)
        {
            cout << "R1 is = " << ( -b + sqrt(D) )/( 2*a ) << endl;
            cout << "R2 is = " << ( -b - sqrt(D) )/( 2*a ) << endl;
        } // this is if statement? it's the end of the block associated with the if statement
        else
        {
            cout << "no real roots exist" << endl;
        }
        
        system("PAUSE"); // what would called this? "system" specific statement?
        // Could you rephrase that? I don't understand what you're asking.
    }
    Last edited by Mozza314; 04-03-2011 at 05:48 AM.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Mozza. It was really helpful and have to say it again that you are very nice. My best wishes.

    What do you mean "self-invented"? '+' is a perfectly normal character. The single quotes don't just apply to letters. In the case of '+', you're telling the compiler that you mean '+' the character and not + the operator that adds numbers together.
    What about the character '++'. Isn't it self-invented?

    In one of the post above I used this:
    Code:
    if ( r >= 'A' && r <= 'Z')
    The above if condition is valid because ASCII has the values of "A" and "Z" in a sequence (decimal values: 65-90). Instead if I had written something like:
    Code:
    if ( r >= 'A' && r <= '8')
    Then it isn't a valid condition because "8" has decimal value of 56 on ASCII table. It's like saying something like this: r >= 65 and r <= 56. I hope you understand what I'm trying to ask. Please correct me where I'm wrong or missing something.

    But this also begs another question. What if I know the ASCII code of some character but don't have a key for that character on the keyboard, let's say PI which has ASCII decimal code of 227?

    ASCII table: http://www.asciitable.com/

    int main() is a function and so is system() as you say. But wouldn't it make "system()" a sub-function because it's included in the body - the body being {...} - of "int main()"?

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by Mozza314
    Now, what stands out to me here is that you can use <, <=, >, >= on char values to test ranges just like with numbers, but it appears you don't know this. It means you can replace:
    Incidentally, though it does not matter in practice these days, in theory the English letters in the character set need not be in contiguous and in alphabetical order. You do have this guarantee of order and contiguous placement for the digits though.

    Quote Originally Posted by jackson6612
    But what if I try to use some self-invented character symbol (I see '+' is also taken as a character) which has been standardized by ASCII, then would it give error?
    Quote Originally Posted by jackson6612
    What about the character '++'. Isn't it self-invented?
    Turn up your compiler warning level and attempt to compile some code like that. Then, if the compiler does not react as you expect, ask us why.

    Quote Originally Posted by jackson6612
    Then it isn't a valid condition because "8" has decimal value of 56 on ASCII table. It's like saying something like this: r >= 65 and r <= 56.
    It is still a valid condition, except that it always evaluates to false.

    Quote Originally Posted by jackson6612
    What if I know the ASCII code of some character but don't have a key for that character on the keyboard, let's say PI which has ASCII decimal code of 227?
    Then you do something depending on what you are trying to do.

    Hint: "what if" questions are nice for you to think out loud, but they require the reader to guess what your thoughts are if you don't actually think out loud your entire train of thoughts. Imagine going to a shop to buy a drink, and you ask the storekeeper "what if I bought this can of coke?" The storekeeper would have to figure out if he should say "then you have to pay me $1" or if he should say "then you would be missing out on this beer" (i.e., he has to figure out whether you are really asking how much it costs, or if you are asking his opinion on what drink to buy).

    Quote Originally Posted by jackson6612
    int main() is a function and so is system() as you say. But wouldn't it make "system()" a sub-function
    The term "sub-function" does not exist in standard C++, so you should explain what you understand by that term.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck making part of my code work for testing input
    By KevinP in forum C Programming
    Replies: 6
    Last Post: 01-25-2011, 07:52 AM
  2. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  3. Replies: 7
    Last Post: 01-01-2008, 11:30 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. number to a character
    By steven in forum C Programming
    Replies: 2
    Last Post: 09-06-2001, 02:45 PM

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