Thread: 2 questions

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    2 questions

    Hello,

    I need to do 2 things. One program is supposed to check if the entered character is a letter or a digit. Can I use bool to do this? Or is there something better?
    I thought I could use the true/false to do this.



    And then....

    A program that determines the sum of ASCII code of each keyword given below.

    int, float, return, operator, const, virtual, if, do, while


    I don't know how to that.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your answer will be bool, certainly. You can't really "use" bool to find the answer, though.

    As to #2 I don't know how to do that either, given that none of those keywords have ASCII codes. Or do you mean sum the ASCII code of each individual letter?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Your answer will be bool, certainly. You can't really "use" bool to find the answer, though.

    As to #2 I don't know how to do that either, given that none of those keywords have ASCII codes. Or do you mean sum the ASCII code of each individual letter?


    Yeah, probably the letters.

    I was gonna use if/else and bool to get the answer if it's a letter or digit.

    Code:
    int main(void)
    {
        
      bool n;
    
    std::cout << "Input a character: ";
    
    cin >> n;
      
    
      if(n = false)
        cout << "It's a number!";
      else 
        cout << "It's a letter!";
    
        pause();
        return 0;
    }
    But the output is always " it's a letter". Sorry, I'm still kinda new to all this.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You cannot input a bool, nor do you want to. You want to input a character, and check whether that character is a letter, or a digit, or something else.

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by XodoX View Post
    Yeah, probably the letters.

    I was gonna use if/else and bool to get the answer if it's a letter or digit.

    Code:
    int main(void)
    {
        
      bool n;
    
    std::cout << "Input a character: ";
    
    cin >> n;
      
    
      if(n = false)
        cout << "It's a number!";
      else 
        cout << "It's a letter!";
    
        pause();
        return 0;
    }
    But the output is always " it's a letter". Sorry, I'm still kinda new to all this.
    == compares = assigns
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    oops, my bad! Thanks! It's == and works now. Tells me if it's a number or letter. But I left the bool.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by XodoX View Post
    oops, my bad! Thanks! It's == and works now. Tells me if it's a number or letter. But I left the bool.
    Sure. Try 0 and see what happens :-).

    You may want to just use isdigit() from <cctype>, see cctype (ctype.h) - C++ Reference

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by zacs7 View Post
    Sure. Try 0 and see what happens :-).

    You may want to just use isdigit() from <cctype>, see cctype (ctype.h) - C++ Reference
    uniary is the new binary
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM