Hard Light Productions Forums
Off-Topic Discussion => General Discussion => Topic started by: Fear on August 09, 2004, 08:59:15 am
-
hey just started to learn. now i cant see what do constat do(const). i know what does it mean but i cant see what it do in this script:
const double rollwidth =21.0;
const double rolllength = 12.*33.;
and another question. im trying to do a script that caculte the road that u passed, the problem with my script is that u can acctually wirte words insteed of numbers and if u wirte a road the result will say 681000. and so on.... can i do something the u cannot wirte words?.
Plus i couldnt understand what does this text means!:
"Note that an assignment is not the equivalent of the equations you saw in high school algebra. It specifies an action to be carried out rather than a statement of fact. The statement,
A = A + 1;
means, 'add 1 to the current value stored in A and then store the result back in A'. As a normal algebraic statement it wouldn't make sense.
"
its mean every vaule the program make it +1?
-
const means your not alowed to change the vallue of the variable.
no idea what your talking about here, keep in mind every time you substitute the letter 'u' for the word "you" my reading of you post comes to a halt as I come to terms with yours choice of grammer, and I lose my concentration.
it means that in c a = b + 1 is not a fact, it's explaining that these aren't algebraic terms but rather a bunch of instructions, every line of code gets borken down into a bunch of instructions, in the order you write it, so "a = b + c" gets turned into "the value of b and c added, gets stored into the variable a"
-
[color=66ff00]Don't you guys think that programming related questions, even general ones would be answered faster and more thoroughly in the SCP forum?
[/color]
-
bob i love u i trully do ;7
const mean im not allowed? but if the user wirte the vaule it will make an error? i cant see where it will work on...
so if i wirte
const double alpha=3;
im not allowed to change it later on? wierd...
and for the question u couldnt understand i mean im trying to do that the user that uses the program cant wirte letters only numbers......
Don't you guys think that programming related questions, even general ones would be answered faster and more thoroughly in the SCP forum?
sorry about that.
-
He's asking how to prevent a user from entering anything except numbers. Fear, depending on how you're getting the user input, you'll need to do input validation to check what the user has typed and get the program to ask again if it's not valid (ie if they haven't typed a number).
const is good for storing data that you know won't change in the lifetime of the program. It prevents you or some code you use from accidentally changing it. eg. The value of pi (3.14...) in a geometry program. You wouldn't want to change that value as it would mess up any calculation you did with it. It's an anit-f****up measure.
-
and for the question u couldnt understand i mean im trying to do that the user that uses the program cant wirte letters only numbers......
Simple way is to scan the input string for invalid characters and discard it if they appear, then prompt the user to enter a valid value. Or you could prevent the user from typing invalid characters in the first place but that's assuredly more complex
-
[color=66ff00]Not admonishing you Fear, Hard light forum is for everything and anything it's just that more 'specialists' hang out in the SCP forum and would likely get back to you faster.
If it did actually bother me I'd have moved the thread straight away after all. :)
[/color]
-
i wasnt expecting so many comments! dam this fourm rule;)
so if i type
const alpha=5
it means it wont change atall? even by the user?
-
[color=66ff00]A constant by it's very nature is a fixed value so it won't ever change.
[/color]
-
ok thnx.
for now i used only int/long/double , i know the diffrent btween them is 4 bytes but the question is...why should i waste more bytes(or is it bites?). is there any other diffrent?
+
what does line means( < int > ?!?)
strips_per_roll = static_cast< int >(rolllength/height);
-
Constants are useful to specify a fixed fact which will not change throughout the runtime of the program - i.e. an invariant - and which is specified at/before compile time in the code.
-
Originally posted by Fear
ok thnx.
for now i used only int/long/double , i know the diffrent btween them is 4 bytes but the question is...why should i waste more bytes(or is it bites?). is there any other diffrent?
It depends on the size / precision of the number you want to specify within that variable.
-
[color=66ff00]Someone will probably correct me on this, it's been a while since I programmed anything.
int can only be a whole number i.e. no decimals and thus has a very finite level of accuracy when used. it can be negative or positive and ranges from 0 to 65535 for a signed integer or -32768 to 32767 for an unsigned integer.
If you add 1 to 65535 it loops back around to zero in a signed int, likewise if you add one to 32767 in an unsigned int you get -32768.
You have to be careful that you don't exceed the available range otherwise your program will start throwing out weird values.
also as far as which one to use; IIRC the smaller the value, the less processor time it takes to manipulate.
[/color]
-
Originally posted by Maeglamor
[color=66ff00]Someone will probably correct me on this, it's been a while since I programmed anything.
int can only be a whole number i.e. no decimals and thus has a very finite level of accuracy when used. it can be negative or positive and ranges from 0 to 65535 for a signed integer or -32768 to 32767 for an unsigned integer.
If you add 1 to 65535 it loops back around to zero in a signed int, likewise if you add one to 32767 in an unsigned int you get -32768.
You have to be careful that you don't exceed the available range otherwise your program will start throwing out weird values.
also as far as which one to use; IIRC the smaller the value, the less processor time it takes to manipulate.
[/color]
:nod:
For ref, in an 8-bit number;
1111 1111 + 0000 0001 = 1 0000 0000
BUT the 8 bit precision means the MSB is lost, so you get 0000 0000 as a result
In a signed int, the MSB is for the sign, so adding will 'flip' it.
(MSB = Most Significant Bit, i.e. leftmost)
NB: My binary / boolean algebra is a bit rusty, so correct me if I'm wrong. Been a fair number of years since I used this
-
great now i cant understand it at all:lol: .
about that other question... some 1?
what does line means( < int > ?!?):
strips_per_roll = static_cast< int >(rolllength/height);
-
Originally posted by Fear
great now i cant understand it at all:lol: .
Think of it as if you were counting up to 10 on your fingers....and then having to add 1. :)
(and no, you can;t use any other body parts....)
what does line means( < int > ?!?)
strips_per_roll = static_cast< int >(rolllength/height);
I'm stabbing in the dark here - I don;t do C++ - but I think this means that strips_per_roll will be set as the result of (rollength/height) where the result is 'cast' as an int value.
I.e. the result generated will be regarded as an int value
Remember that all variables boil down to a set of binary digits, so you can choose to interpret them in multiple ways... casting is a way of specifying what you 'treat' that stored value in memory as. i.e. if you cast a stored String as an int, you'll probably get the int representation.
I'm not sure exactly what 'static_cast' means, tho - gimme a sec and I'll check.
EDIT;
static_cast Operator
static_cast < type-id > ( expression )
The static_cast operator converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion.
The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe.
In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead. See dynamic_cast for more details.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_74.asp
-
const makes a variable 'fixed' with whatever value it's been initialised with. I've only ever used it in Java because Java doesn't have a #define command.
Example use of const:
int intTest1=5; //Here we assign the variable intTest1 the value of 5
const int intTest2=5; //Here we also assign the variable intTest2 the value of 5, but with the const keyword.
Now, if, later in the code, we tried:
++intTest1 //Increment the value of intTest1 by 1
++intTest2 //Increment the value of intTest2 by 1
The first one will work, but the second one will complain because we are trying to add one to a value which has been fixed.
This is tricky to explain in so little space. TBH you might be better off investing in a C/C++ book.
The one we got forced to use in Uni was Teach Yourself C by Herbert Schildt, which is not bad.
-
The C++ Programming Language
by Bjarne Stroustrup
http://www.amazon.com/exec/obidos/tg/detail/-/0201700735/qid=1092073446/sr=1-3/ref=sr_1_3/104-4918594-4199961?v=glance&s=books
Was one which was always recommended to me. Albeit, I've still never got round to learning C++ and didn't buy the book, but what i did read seemed pretty nice & clear.
-
[color=66ff00]Don't get the 'Learn C++ in 24 hours' book, the 21 days books are better though.
[/color]
-
i understand the const now,though i still cant undetstand static operator . what is supose to do change 1 int to another int?
-
Originally posted by Fear
i understand the const now,though i still cant undetstand static operator . what is supose to do change 1 int to another int?
No, just to ensure whatever result is returned is treated as an int. i.e. you don't know what you get back, but the cast 'turns' it (logically) into an int value.
i.e.
Lets say the String 'blah'
is
1001 1110
in memory (its not, that's far too small and short a value....but ne'erming ;) )
Now - there's nothing 'stored' which says that binary value is a String - the only thing that indicates that, is that you reference it in the program as a String variable.
So, if you cast to an int, the program is simply reading that same memory, in a different way. Instead of working out 1001 1110 is 'blah' (as it would with a String) it's reading it as its integer value.
So - it doesn't change what's stored, but how the program 'looks' at it. In the case before, it's ensuring that whatever result that operation gives, it'll be modified and referenced as an int value.
-
so all its change is how the program will treat to it? dam what use this bring :p
-
Originally posted by Fear
so all its change is how the program will treat to it? dam what use this bring :p
Well, yeah. But bear in mind you can still make changes to that memory space - i.e. if you have 1 pointer referencing it as an int, and one as a String, then decrementing the 'int' reference will affect what you get as a String value. Etc.
-
got it... but i cant think of the situtasion that i will have to use it.
-
http://www.deitel.com/books/downloads.html#Cplus
-
its free?!
-
The Dive Into ones are.