Thread: First Time, Please review...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    19

    First Time, Please review...

    "Hello all - this is my first time on here but, I need some major help on my homework... yes, I know... I am not asking anyone to do it for me but, please explain to me how to do the program what I need it do. So let me break it down. Here's part of my program...

    Code:
    #include <iostream>
    using namespace std;
    
    void main ()
    {
        char ch;
        int asc;
    
        do
        {
    
        cout<<"Enter a number or letter: \n";
        cin >> ch;
        asc=(int)ch;
        cout<<ch<<" "<<asc<<"\n";
                    
        }
        while ((ch != 'q') && (ch !='Q'));
    }
    Okay this program is to display and ASCII code once a character is entered... but, here is my problem...

    How do I put in my program that if it is NOT a letter or a number that is entered for "ch", then a message comes up "Incorrect character, please try again!".

    I've been working on this problem for hours.... please help me.

    Thanks.

    MyDestiny

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You can use the isalpha function to determine if a character is an alphanumeric character or not.
    Code:
    int main()
    {
        char a[2];
        a[0] = '?';
        a[1] = 'R';
    
        if (isalpha(a[0]))
            cout << "is alpha ?" << endl;
        if (isalpha(a[1]))
            cout << "is alpha R" << endl;
        return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    gotta make your posts pretty...

    Quote Originally Posted by pianorain
    You can use the isalpha function to determine if a character is an alphanumeric character or not.
    wrong! isalpha() returns non-zero if it's a letter in the alphabet. to test if it's a number or character, you need to use isalnum(), also in <ctype.h>.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Oh, and just for reference, it is bad practice to use void main(). You should always use int main().
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Naughty Naughty no void main.
    My computer is awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 09:11 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM