Author Topic: i got a question about c++  (Read 1374 times)

0 Members and 1 Guest are viewing this topic.

Offline Fear

  • 26
i got a question about c++
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?
« Last Edit: August 09, 2004, 09:07:12 am by 2039 »

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
i got a question about c++
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"
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Martinus

  • Aka Maeglamor
  • 210
    • Hard Light Productions
i got a question about c++
[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]

 

Offline Fear

  • 26
i got a question about c++
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......

Quote
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.

 

Offline Arc

  • 23
i got a question about c++
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.
« Last Edit: August 09, 2004, 11:35:31 am by 885 »

 
i got a question about c++
Quote
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

 

Offline Martinus

  • Aka Maeglamor
  • 210
    • Hard Light Productions
i got a question about c++
[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]

 

Offline Fear

  • 26
i got a question about c++
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?

 

Offline Martinus

  • Aka Maeglamor
  • 210
    • Hard Light Productions
i got a question about c++
[color=66ff00]A constant by it's very nature is a fixed value so it won't ever change.
[/color]

 

Offline Fear

  • 26
i got a question about c++
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);
« Last Edit: August 09, 2004, 12:25:47 pm by 2039 »

 

Offline aldo_14

  • Gunnery Control
  • 213
i got a question about c++
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.

 

Offline aldo_14

  • Gunnery Control
  • 213
i got a question about c++
Quote
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.

 

Offline Martinus

  • Aka Maeglamor
  • 210
    • Hard Light Productions
i got a question about c++
[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]

 

Offline aldo_14

  • Gunnery Control
  • 213
i got a question about c++
Quote
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

 

Offline Fear

  • 26
i got a question about c++
great now i cant understand it at all:lol: .

about that other question... some 1?
Quote
what does line means( < int > ?!?):
strips_per_roll = static_cast< int >(rolllength/height);

 

Offline aldo_14

  • Gunnery Control
  • 213
i got a question about c++
Quote
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....)

Quote

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

 

Offline Cyker

  • 28
i got a question about c++
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.

 

Offline aldo_14

  • Gunnery Control
  • 213
i got a question about c++
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.

 

Offline Martinus

  • Aka Maeglamor
  • 210
    • Hard Light Productions
i got a question about c++
[color=66ff00]Don't get the 'Learn C++ in 24 hours' book, the 21 days books are better though.
[/color]

 

Offline Fear

  • 26
i got a question about c++
i understand the const now,though i still cant undetstand static operator . what is supose to do change  1 int to another int?