Thread: the entered character is a lowercase, uppercase, or a real number

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    Why do I have to enclose A, Z, 0, and 9 within single quotes? Otherwise my code didn't work.
    Good question. The reason is that when you want the actual character, J, for example, the compiler needs to know that you mean J the character and not J the variable. E.g.:

    Code:
    int main()
    {
        int J = 3;
        cout << J << endl; // outputs 3
        cout << 'J' << endl; // outputs J
        return 0;
    }
    Of course it's conceivable you could make a language in which if there was no variable J available then
    Code:
    cout << J << endl;
    would output J the character instead. However, this makes code difficult to read because someone reading it who hasn't seen the whole program will probably expect J stores some value that is being printed. For this reason, in C++ and other languages, you need to put quotes around the letter whenever you mean the letter itself and not a variable.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Mozza314 View Post
    Good question. The reason is that when you want the actual character, J, for example, the compiler needs to know that you mean J the character and not J the variable. E.g.:

    Code:
    int main()
    {
        int J = 3;
        cout << J << endl; // outputs 3
        cout << 'J' << endl; // outputs J
        return 0;
    }
    Of course it's conceivable you could make a language in which if there was no variable J available then
    Code:
    cout << J << endl;
    would output J the character instead. However, this makes code difficult to read because someone reading it who hasn't seen the whole program will probably expect J stores some value that is being printed. For this reason, in C++ and other languages, you need to put quotes around the letter whenever you mean the letter itself and not a variable.
    Yes, I think I understand it now. Thanks Mozza. You have been very helpful through out without any air of arrogance!

    Okay, when I use double quotation marks I have text
    When I simply enter something, such as A, B, C, etc, it is expected to be a variable.
    When I use single quotes then it's a character constant - a constant of special type which has a particular symbol and recognized by the system. But what if I try to use some self-invented character symbol (I see '+' is also taken as a character) which has been standardized by ASCII, then would it give error?

    Is this correct? Please correct me if I'm wrong.


    Now some more questions. My questions will appear in the code following "//". It would be nice of you if you could shed some light. (sorry, if there is a big issue with indentation, when I copy the code here something goes wrong)

    Code:
    #include <iostream>
    # include <cmath>
    #include <cstdlib> // these are header files?
    
    using namespace std; // this is "using" statement?
    
    int main () // this is the function which we are using?
    
    {
        float a, b, c, R1, R2; // this is the declaration statement?
        
        system("Color 1A"); // this is what? I use it to change the color of the console.
        
        cout << "Enter a, b, c of the equation ax^2 + bx + c = 0" << endl;
        cout << "Enter a = ";
        cin >> a; // what are cout and cin statements called?
        
        cout << "Enter b = ";
        cin >> b;
        
        cout << "Enter c = ";
        cin >> c;
        
    	float D = b*b - 4*a*c; // this both declaration and processing statement?
        if (D >= 0)
        {
            cout << "R1 is = " << ( -b + sqrt(D) )/( 2*a ) << endl;
            cout << "R2 is = " << ( -b - sqrt(D) )/( 2*a ) << endl;
        } // this is if statement?
        
    	else
        {
            cout << "no real roots exist" << endl;
        }
        
        system("PAUSE"); // what would called this? "system" specific statement?
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  3. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    When I simply enter something, such as A, B, C, etc, it is expected to be a variable.
    When I use single quotes then it's a character constant - a constant of special type which has a particular symbol and recognized by the system. But what if I try to use some self-invented character symbol (I see '+' is also taken as a character) which has been standardized by ASCII, then would it give error?
    What do you mean "self-invented"? '+' is a perfectly normal character. The single quotes don't just apply to letters. In the case of '+', you're telling the compiler that you mean '+' the character and not + the operator that adds numbers together.


    Quote Originally Posted by jackson6612 View Post
    Code:
    #include <iostream>
    # include <cmath>
    #include <cstdlib> // these are header files?
    // Yes
    
    using namespace std; // this is "using" statement?
    Yes; it makes everything in the namespace std accessible without prefixing it with "std::".
    // E.g. without it you'd have to write "std::cout" in place of "cout".
    
    int main () // this is the function which we are using?
    // Yes; main() is a special function which is marks the start of execution of the program
    {
        float a, b, c, R1, R2; // this is the declaration statement?
        // This is where you create the float variables, it's known as "declaration", yes
        
        system("Color 1A"); // this is what? I use it to change the color of the console.
        // What's your question? It's a call to the function system().
        
        cout << "Enter a, b, c of the equation ax^2 + bx + c = 0" << endl;
        cout << "Enter a = ";
        cin >> a; // what are cout and cin statements called?
        // They don't really have a special name. ">>" is sometimes called the
        // extraction operator and "<<" the insertion operator if that's what you're asking.
        
        cout << "Enter b = ";
        cin >> b;
        
        cout << "Enter c = ";
        cin >> c;
        
        float D = b*b - 4*a*c; // this both declaration and processing statement?
        // Yes, the value b*b - 4*a*c is calculated and then D is initialised with that value
        if (D >= 0)
        {
            cout << "R1 is = " << ( -b + sqrt(D) )/( 2*a ) << endl;
            cout << "R2 is = " << ( -b - sqrt(D) )/( 2*a ) << endl;
        } // this is if statement? it's the end of the block associated with the if statement
        else
        {
            cout << "no real roots exist" << endl;
        }
        
        system("PAUSE"); // what would called this? "system" specific statement?
        // Could you rephrase that? I don't understand what you're asking.
    }
    Last edited by Mozza314; 04-03-2011 at 05:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck making part of my code work for testing input
    By KevinP in forum C Programming
    Replies: 6
    Last Post: 01-25-2011, 08:52 AM
  2. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  3. Replies: 7
    Last Post: 01-01-2008, 12:30 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. number to a character
    By steven in forum C Programming
    Replies: 2
    Last Post: 09-06-2001, 02:45 PM