CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Unhappy really need your help

    I just started to study the C++ Programming Language. I'm done with the condition statements and jump statements. An idea came to my mind to create a simple program
    that will determine if the input is a character or integer.
    For example, the program asks you "How old are you?" and then you input a character (A-Z), the program will output "Your input is a Character."
    Same as, if the program asks you "Choose a letter from A-Z" and then you input a integer (0-9), the program will output "Your input is a Integer."

    Your reply will be a really big help to me as I study C++.

    Thanks..

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: really need your help

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: really need your help

    [ removed duplicate thread ]

    Please do not create multiple threads about the same issue.

    And please try to give a meaningful title to your threads.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Apr 2010
    Posts
    20

    Re: really need your help

    Code:
    #include <iostream>
    
    int main(int argc,char * argv[])
    {
        char chInput = 0;
    
        cin << chInput;
    
        if ( (chInput >= 'A' && chInput <= 'Z') || (chInput >= 'a' && chInput <= 'z') )
        {
            std::cout << "Your input is a Character.";
        }
        else if ( chInput >= '0' && chInput <= '9' )
        {
            std::cout << "Your input is an Integer.";
        }
        else
        {
            std::cout << "Invalid input.";
        }
    
        return 0;
    }
    To explain that code example to a beginner at C++, a character holds ASCII text values. In the ASCII code table the characters 'A' and 'Z' are arranged in order from A to Z. Same goes for '0' to '9'. So you just compare the character input against the literal character values.

    If input is greater than or equal to 'A' and less than or equal to 'Z', or if input is greater than or equal to 'a' and less than equal to 'z', then it's a character.

    If the input is greater than or equal to '0' and less than or equal to '9', then it is an integer.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: really need your help

    Quote Originally Posted by CppCoder2010 View Post
    To explain that code example to a beginner at C++,
    http://www.codeguru.com/forum/showpo...18&postcount=2

    Stick to explaining without the code. We don't do homework for others here.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured