Thread: having a little trouble with "too many characters in constant"

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    12

    having a little trouble with "too many characters in constant"

    I'm adding a sort of cheatcode system to the switch_case example, you know something like "if (cheat = 'kevin') but it's telling me there's too many characters in the constant. any idea why it says that?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you have more than one character it's called a string.

    You can either use the datatype string or a character array. Sooner or later you will have to use character arrays so I'll give you an example of those:

    // this is an array of characters to store text
    char text[50];

    // the function strcmp compares two texts and returns 0 if they are equal. Note the double = used for comparison.
    if( strcmp( text, "kevin" ) == 0 )
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    also, you can't use string comparisons as a switch case.

    edit:
    well not in C/C++ anyway.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    An important concept in C++ is that a variable represents the contents at a single memory location (address)*. If you have a whole string of characters, you cannot store it in a single variable. You must use an array of variables.

    Character arrays are sometimes called null-terminated strings or C-style strings to differentiate them from C++ string-class objects. The syntax for C++ string objects may fool you into thinking that they are a single variable... Don't be fooled.

    Now, because arrays are stored sequentially in memory, you can "get to" the whole array by using a pointer to the first character. Sometimes pointer syntax is tricky, and sometimes you might not realize you are using a pointer.

    The single quote syntax ( 'a' ) gives the ASCII value of a single character ( 'a' = 97). It is a syntax error to put single-quotes around more than one character.

    Regular double-quotes ( "a" ) around a single-character generates a two-element array... one-character plus the null-termination-zero ( "a" = 97, 0 ).

    *There's an exception to everything... Some variable types may take-up a fixed number of sequential memory locations. (A long int takes-up more than one address on a 16-bit machine.) In these cases, the compiler is smart-enough that you usually don't have to be concerned about it.
    Last edited by DougDbug; 05-24-2005 at 01:07 PM.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    12
    Ok, now i'm having trouble with something else. I'm adding multiple switch_case in my app, but it tells me that there's illegal uses of cases and default.

    It still wont let me post my code for some reason and I did what you said to put but that didn't work either, if u guy want to see the source code just let me know.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should read the forum guidelines. Then you'd know how to use code tags. Here, this'll get you started:

    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

    It appears that you are posting a homework assignment or other project.

    Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

    Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.


    On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

    Feel free to PM me with any questions.

    Good Luck,

    Kermi3

    Read and follow the above instructions, and try your post (in the same thread) again.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    you could also use the strcmp function, in the library string.h, just see how it works, i dont remember exactly, but I think it returns a 1 if the strings are the same, also post your code so I can see why the mistakes with the case.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    12
    Nm, sorry, i did it my self. I hate it when I need to figure out something and I can then figure it out. If it makes any sence

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. problem with reading characters
    By csvraju in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:59 AM
  3. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM
  4. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  5. How to pull 3 characters out of a larger string?
    By Whoanswers in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2001, 01:41 AM