Computer Science Canada Is it possible for a variable to be two or more types? |
Author: | qmanjr5 [ Tue Dec 01, 2009 8:59 pm ] | ||
Post subject: | Is it possible for a variable to be two or more types? | ||
Such as
or whatever. |
Author: | Tony [ Tue Dec 01, 2009 9:04 pm ] |
Post subject: | RE:Is it possible for a variable to be two or more types? |
just to try to understand what it is you are actually looking for... if I was to have such a variable, and it held a value of "qmanjr5", but I used it as an int, what int value would you expect to have? |
Author: | qmanjr5 [ Tue Dec 01, 2009 9:06 pm ] |
Post subject: | RE:Is it possible for a variable to be two or more types? |
Erm, no idea? ![]() I have no idea what you mean. An int is a number, not a string. The reason I ask this is for my quiz I'm making. If I enter "lsmfpowe" for a question who's variable is an int, it screws up and won't continue. I realized this is because an int can't have letters (as far as I know?) so, I was wondering if I could make the variable be an integer and a string. Or would I just put it as a string, since you can input numbers in a string? |
Author: | Tony [ Tue Dec 01, 2009 9:14 pm ] |
Post subject: | Re: RE:Is it possible for a variable to be two or more types? |
qmanjr5 @ Tue Dec 01, 2009 9:06 pm wrote: Erm, no idea?
![]() That's exactly why I ask ![]() qmanjr5 @ Tue Dec 01, 2009 9:06 pm wrote: Or would I just put it as a string, since you can input numbers in a string? Indeed.
You might also be interested in strintok and strint functions. |
Author: | SNIPERDUDE [ Tue Dec 01, 2009 9:22 pm ] |
Post subject: | RE:Is it possible for a variable to be two or more types? |
Use strings for your answers. When you come across questions that require integers, Turing has built-n features such as strint (which converts a string to an integer), and strintok (which checks to see if a string can be converted to an integer, returning a boolean). Just look them up in the Turing documentation. EDIT: Tony and your fast posting |
Author: | Kharybdis [ Tue Dec 01, 2009 9:31 pm ] |
Post subject: | RE:Is it possible for a variable to be two or more types? |
A variable can't be an int and a string at the same time... that's just not possible. I mean you can have the number 5 as a string.. but it would be declared as a string, not as an int. You can interchange them if that's what you are seeking using the strintok, strreal, strint, and other functions. EDIT: Wow, i'm such a slowpoke... |
Author: | Tony [ Tue Dec 01, 2009 9:38 pm ] |
Post subject: | Re: RE:Is it possible for a variable to be two or more types? |
Kharybdis @ Tue Dec 01, 2009 9:31 pm wrote: A variable can't be an int and a string at the same time... that's just not possible
It is! It's called a Union. A variable is just a memory location that is holding some bits. Those bits could be interpreted as an integer, or as a string, or whatever else. In Turing, you can perform something similar via cheat |
Author: | SNIPERDUDE [ Wed Dec 02, 2009 11:54 am ] |
Post subject: | RE:Is it possible for a variable to be two or more types? |
Well, that explains my query of was cheat is. Thanks Tony! |