What am I doing wrong?

This is a discussion on What am I doing wrong? within the C++ Programming forums, part of the General Programming Boards category; What am I doing wrong? I need this program to print out the ASCII code. If i enter a symbol ...

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

    What am I doing wrong?

    What am I doing wrong? I need this program to print out the ASCII code. If i enter a symbol for exampl "& or % or /" I need a message to appear please try again, but if I put "q or Q" it automatically kicks me out. Please, help... heres my program...

    Code:
    #include <iostream>
    #include <cctype>
    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";
    		
    	
    	if (isalnum(ch))
    	cout<<"Please enter a correct number or letter: \n";
    	}
    		while ((ch != 'q') && (ch !='Q'));
    					
    }
    Please help... thanks a bunch!

  2. #2
    Not just a squid...
    Join Date
    Sep 2004
    Posts
    25
    isalnum() returns true if the character is alphanumeric.

    This will fix it:
    Code:
    if (isalnum(ch))
       cout<<ch<<" "<<asc<<"\n";
    else 
       cout<<"Please enter a correct number or letter: \n";
    Now they wont't see the ASCII code for a character they weren't suppsed to enter either.
    Last edited by TheSquid; 02-10-2005 at 12:17 PM. Reason: I was over complicating things, this works better.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    82
    after you read in the letter, try something like this:
    Code:
    if( !isLetter(ch) )
    {
        cout << "Enter a letter" << endl;
    }
    else
    {
        // convert to ascii code
    }
    You just need to implement the isLetter() to return a bool, true if the arguement ch is found in a list of valid letters. You could do that using a vector of all valid letters, or a really long switch statement.

    EDIT: ah, squid's works. Just replace isLetter with isalnum.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by MrDestiny
    but if I put "q or Q" it automatically kicks me out.

    Code:
     while ((ch != 'q') && (ch !='Q'));
    Because you told it to do that. Once ch is either 'q' or 'Q', the condition in that while loop becomes false and the loop exits.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    I want that when I hit 'q or Q' that it kicks me out... so I dont want to change that...

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you could also use isgraph(), but that would include a tab and newline, so you may want to check for those and replace them if you're printing a table...
    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

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if (isalnum(ch))
    > cout<<"Please enter a correct number or letter: \n";
    Code:
        if (!isalnum(ch))
           cout<<"Please enter a correct number or letter: \n";

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    It seems using q or Q as the sentinel is a bad choice considering the design of this little program. ie get a letter or number print its ASCII code then repeat.

    if it isn't a letter or number print an error message

    in other words the functionality is consistent with all letters and numbers but one, which is not even an exceptional case

    I would choose to use one of the other chars such as a space as the sentinel
    a space would be very convient for quitting but you would have to change your
    error logic.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Just an FYI - this post violates the thread naming policies of this board. Please be more specific with your titles.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > void main ()
    int main
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    82
    I'm thinking we really need some javascript/php to prompt people to use int main(). Would not be hard too implement.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Hehe, do a preg_replace for all void mains. Secretly (or, well, not so secretly) forcing int main into the minds of all! *evil grin*

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    int main (void)
    {
        bool done = false;
        char ch;
        int asc;
    
        while(!done)
        {
    
        cout<<"Enter a number or letter: ";
        cin >> ch;
        cout << endl;
    
        if ((ch != 'q') && (ch !='Q'))
        {
          
          
          if (!isalnum(ch))
          {
             cout<<"Please enter a correct number or letter: \n";
          }
          else
          {
          asc=(int)ch;
          cout<<ch<<" in ascii is "<<asc<<"\n";
          }  
        
        }
        else 
        {
          done = true;
        }
    
        }
            
                        
    }
    I think this does what the original question asked

  14. #14
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    The code is functional, no doubt. However I find this much easier to read...
    Code:
    if ( toupper(ch) != 'Q' )

  15. #15
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    I agree

    I agree but the trouble seemed to be less about what functions to use but how while loops and if statements affect program flow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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