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?
This is a discussion on having a little trouble with "too many characters in constant" within the C++ Programming forums, part of the General Programming Boards category; I'm adding a sort of cheatcode system to the switch_case example, you know something like "if (cheat = 'kevin') but ...
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?
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.
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
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.
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.
You should read the forum guidelines. Then you'd know how to use code tags. Here, this'll get you started:
Originally Posted by 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.
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.
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