Thread: Newbie: how do you make a char a constant?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Newbie: how do you make a char a constant?

    Is it possible to make a character a constant and a digit a variable somehow?

    this program won't compile as it takes the C for a variable.

    Code:
    int aa()
    { 
        char y;
        
        cout<<" 1  - Intro to C++ The basics of C++\n";
        cout<<"\n";
        cout<<"1. What is the correct value to return to the operating system upon the successful completion of a program?\n";
        cout<<"A. -1\n";
        cout<<"B. 1\n";
        cout<<"C. 0\n";
        cout<<"D. Programs do not return a value.\n";
        cin>> y;
        cin.ignore();
    
    if ( y = c )
        {
        cout<<"That's correct.\n";
        cin.get();
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if ( y = c )
    1. Use == for comparion.
    2. If you meant the letter c, then it would be
    if ( y == 'c' )
    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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You probably want this:

    Code:
    if( y == 'c' || y == 'C' )
    {
        cout<<"That's correct.\n";
        cin.get();
    }
    Remember, = is the assignment operator (assign A the value of B), == is the equality testing operator (is A equal to B?).
    "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

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    ohh yea, forgot the ==.

    ok so 'c' it is, thanks.

    and about the || does it means that if any of the statements on either side are true it will result in true? and could you then make it with an unlimited number of those like:
    Code:
    if ( x == 'y' || x == 'Y' || x == 3 ) {
        cout<<"That's correct.\n";
        cin.get();
    }
    ?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yep. There's also && for AND.

    Although if you're just checking for uppercase and lowercase letters, you could look into toupper() or tolower() from <cctype>. Search cppreference.com for details.

    [edit] See also this tutorial: http://www.cprogramming.com/tutorial/lesson2.html [/edit]
    Last edited by dwks; 12-21-2006 at 06:21 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM