Thread: Have a really basic easy question..

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Have a really basic easy question..

    im trying to get ready for my test tommorow. And well giong over my old labs....I came to this..
    I have these to function defs....

    Code:
    void CharSet::Add(char Value)
    {
       Contents[Value - 'A'] = true; 
    }
    void CharSet::Remove(char Value)
    {
       Contents[Value - 'A'] = false;
    And Although I used it I never fully understood....the Value -'A'...I was wondering if someone could help me out and give me an explanation into how it is used....I really don't recall using that or being taught that. I used it because the teacher in his instructions hinted for us to use it.
    thanks in advance

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Assuming that Value holds an uppercase character, the expression Value - 'A' would give you a zero-based index of the uppercase letters, with 'A' being 0 and 'Z' being 25.
    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
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    The contents of 'A' is the ascii value for the upper-case A... in this case, 'A' is being used as a numeric constant (in hex: 40h in dec: 64d)
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    ...

    oh ok i see....makes more sense now....thanks for the help...

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Another take: characters are stored internally as integer codes. An ASCII table will show you the list of integer codes. Since they are integers, you can do mathematical operations on them:
    Code:
    char ch = 'A';
    ch += 2;
    cout<<ch<<endl;  //C
    When you output a char type, C++ converts the interger code back to a character. So, if you actually want to output the integer code, then you have to convert a char type to an integer:
    Code:
    char ch = 'A';
    int code = ch;
    cout<<code<<endl; //65
    cout<<ch<<endl;  //A
    It better style to make an explicit cast, instead, in order to demonstrate that's what you intended:
    Code:
    char ch = 'A';
    int code = static_cast<int>(ch);
    cout<<code<<endl; //65

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Another take: characters are stored internally as integer codes. An ASCII table will show you the list of integer codes. Since they are integers, you can do mathematical operations on them:
    Code:
    char ch = 'A';
    ch += 2;
    cout<<ch<<endl;  //C
    When you output a char type, C++ converts the interger code back to a character. So, if you actually want to output the integer code, then you have to convert a char type to an integer:
    Code:
    char ch = 'A';
    int code = ch;
    cout<<code<<endl; //65
    cout<<ch<<endl;  //A
    It's better style to make an explicit cast, instead, in order to demonstrate that's what you intended:
    Code:
    char ch = 'A';
    int code = static_cast<int>(ch);
    cout<<code<<endl; //65
    Good luck on your test.
    Tests
    Last edited by 7stud; 03-01-2005 at 08:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help if basic question
    By Sshakey6791 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2009, 04:00 PM
  2. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 07:48 AM
  3. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  4. help for my exam. easy question
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-15-2008, 03:09 PM
  5. Visual Basic Question
    By Xeavor in forum Tech Board
    Replies: 5
    Last Post: 12-02-2004, 09:59 AM