Thread: Multi Character Constant.. how to correct the error? 3:

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Exclamation Multi Character Constant.. how to correct the error? 3:

    The problem is multi-character constants. how can I assign/compare multiple characters in a variable?
    Code:
    	if (ElemSym=='He')
    			{
    				system("CLS");
    				cout<<"Name: Helium"<<endl;  
    				cout<<"Symbol: He "<<endl;
    			}
    anything to do with the declaration? I used the simple "char" declaration..
    Thank you in advance to whoever will help..

    I'm using Dev c++, since the Visual Studio c++ returns an error due to a limit on the codes.

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by Tinray Reyes View Post
    Code:
    	if (ElemSym=='He') // single quotes
    			{
    				system("CLS"); // double quotes
    Notice the difference. C++ isn't JavaScript and the two aren't interchangeable. Single quotes go around a single character to make a char, double quotes go around any number of characters to make a const char* string. Also if you're using "the simple "char" declaration" you'll need to do one of these things:
    1) Use strcmp to compare the strings instead of ==
    2) Change ElemSym to be a std::string

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    A char can only hold 1 character, to hold more than one character you would need to use std::string (preferred) or a C-string. If ElemSym was defined as a std::string then you could compare the strings using the comparison operator '=='. To use C-strings you will need to use the comparison functions available with the cstring header.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error:unterminated character constant
    By Figen in forum C Programming
    Replies: 6
    Last Post: 01-31-2011, 02:08 PM
  2. warning: multi-character character constant
    By aadil7 in forum C Programming
    Replies: 2
    Last Post: 12-12-2010, 10:02 PM
  3. "warning: multi-line character constant"
    By Benzakhar in forum C++ Programming
    Replies: 7
    Last Post: 08-24-2008, 03:29 PM
  4. multi-character character constant error
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 07-15-2007, 12:12 AM
  5. empty character constant error
    By Sway in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 08:27 PM

Tags for this Thread