Hey guys with the folowing protoype and function,
I am getting this error, which says...
In function `void getHuffmangetHuffman(BTree*, std::string%26amp;, std::string*)':
error: invalid initialization of non-const reference of type 'std::string%26amp;'
from a temporary of type 'std::basic_string%26lt;char, std::char_traits%26lt;char%26gt;,
std::allocator%26lt;char%26gt; %26gt;'
can someone please help me out......
here is my code below!
void getHuffman(BTree* node, string%26amp; s, string sa[]);
void getHuffmangetHuffman(BTree* node, string%26amp; s,string sa[])
{
if (node != NULL)
{
if (node-%26gt;getLeftChild() == NULL %26amp;%26amp; node-%26gt;getRightChild() ==
NULL)
{
char buf[2];
buf[0] = (char)node-%26gt;getLetter();
buf[1] = 0;
string s2 = buf;
sa[node-%26gt;getLetter() - 'a'] = s2 + " = " + s;
}
getHuffman(node-%26gt;getLeftChild(), s + "1",sa);
getHuffman(node-%26gt;getRightChild(), s + "0",sa);
}
}
Help with c++ Huffman BTree!?
The second parameter of getHuffman is a parameter of type std::string%26amp;. Reference parameters need need to use actual variables in the function call. If you use a literal instead, it'll complain.
For example:
myFunction(int %26amp;nValue);
you can call it as myFunction(nX);
but not myFunction(5);
or even myFunction(nX+1);
Because 5 does not have an address, so %26amp;nValue can not be set to anything. When this happens, the compiler normally puts 5 in a temporary variable and passes the temp variable to the function instead. Same with nX+1.
You have two options:
1) make the std::string %26amp; parameter a const std::string %26amp; instead.
2) When you call the function, pass it an actual variable instead of an rvalue.
land survey
Tuesday, July 14, 2009
Help on c++?
I wrote a program that compute %26amp; print the Area of triangle :
#include%26lt;iostream.h%26gt;
main( )
{
const 1/2;
int b,h;
cout%26lt;%26lt;"enter b,h"%26lt;%26lt;endl;
cin%26gt;%26gt;b;
cin%26gt;%26gt;h;
cout%26lt;%26lt;"Area of Triangle="%26lt;%26lt;1/2*b*h;
}
please can any one tell me what's wrong with it .
Help on c++?
1) Remove const 1/2
2) Write b*h*1/2 instead of 1/2*b*h while printing
Your program will run
The MAIN REASON your program isn't giving right answer is you are printing 1/2*b*h. As the compiler reads from left to right and as the priority of '/' and '*' is equal, so it is taking this expression as 1/(2*b*h).
As you haven't mentioned any data type for the answer, compiler is treating it as an integer output. As 2*b*h becomes a large value, 1/(2*b*h) becomes zero (0). So your output is 0.
Reply:First remove const 1/2
It doesnt make sense
Now... since u are using 1/2.. it is a floating point value.
So just add this statement:
float area = 0.5 * b * h;
cout %26lt;%26lt; "Area of Triangle = " %26lt;%26lt; area;
Thats it ;)
Reply:It will give you error at
const 1/2
Sicne C++ does not allow "/" operator in the declaration of Const. If at all you are interested in so, try to use
() in the const declaration.
But I dont see any other error apart from this in the above code.
#include%26lt;iostream.h%26gt;
main( )
{
const 1/2;
int b,h;
cout%26lt;%26lt;"enter b,h"%26lt;%26lt;endl;
cin%26gt;%26gt;b;
cin%26gt;%26gt;h;
cout%26lt;%26lt;"Area of Triangle="%26lt;%26lt;1/2*b*h;
}
please can any one tell me what's wrong with it .
Help on c++?
1) Remove const 1/2
2) Write b*h*1/2 instead of 1/2*b*h while printing
Your program will run
The MAIN REASON your program isn't giving right answer is you are printing 1/2*b*h. As the compiler reads from left to right and as the priority of '/' and '*' is equal, so it is taking this expression as 1/(2*b*h).
As you haven't mentioned any data type for the answer, compiler is treating it as an integer output. As 2*b*h becomes a large value, 1/(2*b*h) becomes zero (0). So your output is 0.
Reply:First remove const 1/2
It doesnt make sense
Now... since u are using 1/2.. it is a floating point value.
So just add this statement:
float area = 0.5 * b * h;
cout %26lt;%26lt; "Area of Triangle = " %26lt;%26lt; area;
Thats it ;)
Reply:It will give you error at
const 1/2
Sicne C++ does not allow "/" operator in the declaration of Const. If at all you are interested in so, try to use
() in the const declaration.
But I dont see any other error apart from this in the above code.
Any decent c++ programmers out there to translate this code?
here is doom source code for a file called d_items. Need to know what it means. Is the variables = to a graphic or animation image? Notice upstate, downstate etc, are these animations of the character shooting the gun?
static const char
rcsid[] = "$Id:$";
// We are referring to sprite numbers.
#include "info.h"
#ifdef __GNUG__
#pragma implementation "d_items.h"
#endif
#include "d_items.h"
// PSPRITE ACTIONS for waepons.
// This struct controls the weapon animations.
//
// Each entry is:
// ammo/amunition type
// upstate
// downstate
// readystate
// atkstate, i.e. attack/fire/hit frame
// flashstate, muzzle flash
//
weaponinfo_t weaponinfo[NUMWEAPONS] =
{
{
// fist
am_noammo,
S_PUNCHUP,
S_PUNCHDOWN,
S_PUNCH,
S_PUNCH1,
S_NULL
},
{
// pistol
am_clip,
S_PISTOLUP,
S_PISTOLDOWN,
S_PISTOL,
S_PISTOL1,
S_PISTOLFLASH
},
{
// shotgun
am_shell,
S_SGUNUP,
S_SGUNDOWN,
S_SGUN,
S_SGUN1,
S_SGUNFLASH1
}
Any decent c++ programmers out there to translate this code?
There is no actual code here, it does nothing. These are purely structures containing definitions. Just as you would define a variable. It doesn't do anything until you reference it again and do something with it.
Reply:It's kinda chopped off, but what it looks like to me is a set of enumerations that define the state of a weapon animation. Without more of the code, I can't tell you if this is related to graphics or animations. My guess is that it describes the entire state of a weapon at a given point in time.
Reply:can't really tell by the code you posted. It's standard coding convention that those ALL_UPPERCASE variables are constants they never get changed. S_SGUN represents the same thing in this function as it will in any other function.
Judging by the comments about the upstate and downstate, it sounds like these are arrays holding the address or something similar to the animation code. They might be pointers to it.
Reply:This code is purely definitions, it does nothing. The images are stored somewhere else, probably in an external file.
static const char
rcsid[] = "$Id:$";
// We are referring to sprite numbers.
#include "info.h"
#ifdef __GNUG__
#pragma implementation "d_items.h"
#endif
#include "d_items.h"
// PSPRITE ACTIONS for waepons.
// This struct controls the weapon animations.
//
// Each entry is:
// ammo/amunition type
// upstate
// downstate
// readystate
// atkstate, i.e. attack/fire/hit frame
// flashstate, muzzle flash
//
weaponinfo_t weaponinfo[NUMWEAPONS] =
{
{
// fist
am_noammo,
S_PUNCHUP,
S_PUNCHDOWN,
S_PUNCH,
S_PUNCH1,
S_NULL
},
{
// pistol
am_clip,
S_PISTOLUP,
S_PISTOLDOWN,
S_PISTOL,
S_PISTOL1,
S_PISTOLFLASH
},
{
// shotgun
am_shell,
S_SGUNUP,
S_SGUNDOWN,
S_SGUN,
S_SGUN1,
S_SGUNFLASH1
}
Any decent c++ programmers out there to translate this code?
There is no actual code here, it does nothing. These are purely structures containing definitions. Just as you would define a variable. It doesn't do anything until you reference it again and do something with it.
Reply:It's kinda chopped off, but what it looks like to me is a set of enumerations that define the state of a weapon animation. Without more of the code, I can't tell you if this is related to graphics or animations. My guess is that it describes the entire state of a weapon at a given point in time.
Reply:can't really tell by the code you posted. It's standard coding convention that those ALL_UPPERCASE variables are constants they never get changed. S_SGUN represents the same thing in this function as it will in any other function.
Judging by the comments about the upstate and downstate, it sounds like these are arrays holding the address or something similar to the animation code. They might be pointers to it.
Reply:This code is purely definitions, it does nothing. The images are stored somewhere else, probably in an external file.
REctangle code C++ 2005 express?
Im having a hard time, can some one help me with this program??? This is my assignment, im lost
Write a program that displays a rectangle of given the length (an integer size less
than or equal to 21) and the width (an integer less than or equal to 79). Your
program should accept as input from the keyboard the character used to form the
rectangle, and the two values for the length and width of the rectangle.
Out of range input values should result in an informative error message.
Your program should process only one input case, legal or not. Termination occurs
after either the error message is printed or the diamond is drawn.
It is necessary that the program be written using loops.
Sample runs:
Rectangle drawing program
Enter the character, and length and width (eg. x 7):A 7 12
AAAAAAAAAAAA
A A
A A
A A
A A
A A
AAAAAAAAAAAA
Rectangle drawing program
Enter the character, and length and width (eg. x 7):R 3 5
RRRRR
R R
RRRRR
Rectangle drawing program
Enter the character, and length and width (eg. x 7):A 27 32
You have entered an illegal value for the length. It must be between 1 and 21.
You should use a named constant (const) to define and refer to the box size limits.
This will make it easy to modify the program at a later date if the size restriction is
changed. Write a class called Rectangle. Have the appropriate data members for
that class. Also have a constructor and a function to output the rectangle.
REctangle code C++ 2005 express?
So you need to have 2 functions, one for the header and footer block, and the other for the middle pieces. Then using math figure out how many times to do it.
psuedo code:
gets(userinput);
for ( int i = 0; i %26lt; userinput[2]; i++ )
{
int length = userinput[4];
if (userinput[2] == i || i == 0 )
drawFullCharacterRowOf ( userinput[0], length );
else
drawPartialCharacterRowOf( userinput[0] );
}
void drawFullCharacterRowOf ( char val, int length)
{
for ( int i = 0; i %26lt; length; i++ )
cout %26lt;%26lt; val %26lt;%26lt; endl;
}
This is extremely pseudo code, but you see the point I would imagine.
Reply:// TestCPPConsole.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define MAX_LENGTH 21
#define MAX_WIDTH 79
class Rectangle
{
private:
char m_cUseThisChar;
int m_iHeight;
int m_iWidth;
public:
Rectangle( char cUseThisChar, int iHeight, int iWidth )
{
m_cUseThisChar = cUseThisChar;
m_iHeight = iHeight;
m_iWidth = iWidth;
};
void DrawRectangle( char * szRectangle )
{
char * szTemp = ( char * ) malloc( ( m_iWidth + 2 ) * sizeof( char ) );
try
{
szRectangle[ 0 ] = 0;
memset( szTemp, 0, m_iWidth + 2 );
memset( szTemp, m_cUseThisChar, m_iWidth );
szTemp[ m_iWidth ] = '\n';
for( int iCnt = 1; iCnt %26lt;= m_iHeight; iCnt++ )
strcat( szRectangle, szTemp );
}
catch(...)
{
strcpy( szRectangle, "Error" );
}
if( szTemp != NULL )
{
free( szTemp );
szTemp = NULL;
}
};
};
int _tmain(int argc, _TCHAR* argv[])
{
char cUseThisChar;
int iHeight, iWidth;
printf( "Enter the character, and length and width (eg. x %d %d) ):\n", MAX_LENGTH, MAX_WIDTH );
scanf( "%c %d %d", %26amp;cUseThisChar, %26amp;iHeight, %26amp;iWidth );
if( iHeight %26gt; MAX_LENGTH )
{
printf( "You have entered an illegal value for the length. It must be between 1 and %d.", MAX_LENGTH );
getch();
return 1;
}
if( iWidth %26gt; MAX_WIDTH )
{
printf( "You have entered an illegal value for the width. It must be between 1 and %d.", MAX_WIDTH );
getch();
return 1;
}
Rectangle rect( cUseThisChar, iHeight, iWidth );
char * szBuffer = ( char * ) malloc( ( ( ( iWidth + 2 ) * iHeight ) + 10 ) * sizeof( char ) );
rect.DrawRectangle( szBuffer );
printf( szBuffer );
free( szBuffer );
szBuffer = NULL;
getch();
return 0;
}
======================================...
Disclaimers:
1. Content of stdafx.h file is:
//------------------------------------...
#pragma once
#define WIN32_LEAN_AND_MEAN
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;tchar.h%26gt;
#include %26lt;conio.h%26gt;
//------------------------------------...
2. Error handling must be improved
3. Due to deprecation, functions like getch(), strcpy() etc will get warnings. Use other library methods if you are picky.
Write a program that displays a rectangle of given the length (an integer size less
than or equal to 21) and the width (an integer less than or equal to 79). Your
program should accept as input from the keyboard the character used to form the
rectangle, and the two values for the length and width of the rectangle.
Out of range input values should result in an informative error message.
Your program should process only one input case, legal or not. Termination occurs
after either the error message is printed or the diamond is drawn.
It is necessary that the program be written using loops.
Sample runs:
Rectangle drawing program
Enter the character, and length and width (eg. x 7):A 7 12
AAAAAAAAAAAA
A A
A A
A A
A A
A A
AAAAAAAAAAAA
Rectangle drawing program
Enter the character, and length and width (eg. x 7):R 3 5
RRRRR
R R
RRRRR
Rectangle drawing program
Enter the character, and length and width (eg. x 7):A 27 32
You have entered an illegal value for the length. It must be between 1 and 21.
You should use a named constant (const) to define and refer to the box size limits.
This will make it easy to modify the program at a later date if the size restriction is
changed. Write a class called Rectangle. Have the appropriate data members for
that class. Also have a constructor and a function to output the rectangle.
REctangle code C++ 2005 express?
So you need to have 2 functions, one for the header and footer block, and the other for the middle pieces. Then using math figure out how many times to do it.
psuedo code:
gets(userinput);
for ( int i = 0; i %26lt; userinput[2]; i++ )
{
int length = userinput[4];
if (userinput[2] == i || i == 0 )
drawFullCharacterRowOf ( userinput[0], length );
else
drawPartialCharacterRowOf( userinput[0] );
}
void drawFullCharacterRowOf ( char val, int length)
{
for ( int i = 0; i %26lt; length; i++ )
cout %26lt;%26lt; val %26lt;%26lt; endl;
}
This is extremely pseudo code, but you see the point I would imagine.
Reply:// TestCPPConsole.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define MAX_LENGTH 21
#define MAX_WIDTH 79
class Rectangle
{
private:
char m_cUseThisChar;
int m_iHeight;
int m_iWidth;
public:
Rectangle( char cUseThisChar, int iHeight, int iWidth )
{
m_cUseThisChar = cUseThisChar;
m_iHeight = iHeight;
m_iWidth = iWidth;
};
void DrawRectangle( char * szRectangle )
{
char * szTemp = ( char * ) malloc( ( m_iWidth + 2 ) * sizeof( char ) );
try
{
szRectangle[ 0 ] = 0;
memset( szTemp, 0, m_iWidth + 2 );
memset( szTemp, m_cUseThisChar, m_iWidth );
szTemp[ m_iWidth ] = '\n';
for( int iCnt = 1; iCnt %26lt;= m_iHeight; iCnt++ )
strcat( szRectangle, szTemp );
}
catch(...)
{
strcpy( szRectangle, "Error" );
}
if( szTemp != NULL )
{
free( szTemp );
szTemp = NULL;
}
};
};
int _tmain(int argc, _TCHAR* argv[])
{
char cUseThisChar;
int iHeight, iWidth;
printf( "Enter the character, and length and width (eg. x %d %d) ):\n", MAX_LENGTH, MAX_WIDTH );
scanf( "%c %d %d", %26amp;cUseThisChar, %26amp;iHeight, %26amp;iWidth );
if( iHeight %26gt; MAX_LENGTH )
{
printf( "You have entered an illegal value for the length. It must be between 1 and %d.", MAX_LENGTH );
getch();
return 1;
}
if( iWidth %26gt; MAX_WIDTH )
{
printf( "You have entered an illegal value for the width. It must be between 1 and %d.", MAX_WIDTH );
getch();
return 1;
}
Rectangle rect( cUseThisChar, iHeight, iWidth );
char * szBuffer = ( char * ) malloc( ( ( ( iWidth + 2 ) * iHeight ) + 10 ) * sizeof( char ) );
rect.DrawRectangle( szBuffer );
printf( szBuffer );
free( szBuffer );
szBuffer = NULL;
getch();
return 0;
}
======================================...
Disclaimers:
1. Content of stdafx.h file is:
//------------------------------------...
#pragma once
#define WIN32_LEAN_AND_MEAN
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;tchar.h%26gt;
#include %26lt;conio.h%26gt;
//------------------------------------...
2. Error handling must be improved
3. Due to deprecation, functions like getch(), strcpy() etc will get warnings. Use other library methods if you are picky.
Doubt in c program?
volatile constant. this a a datatype right?? eg. const int n=1 means the value of n is going to b constant for the whole program. ant volatile just opposite.but can these exist like i mentioned above???
help if yes means how??
Doubt in c program?
first of all volatile is a C++ key word and its not for what u said..
see Wikipedia:
http://en.wikipedia.org/wiki/Volatile_va...
Reply:Welcome the famous old question. volatile and const are not mutually exclusive to each other. That is to say, these keywords can be used together to qualify a type. Both fall under the 'type qualifier' category of keywords.
const %26lt;type%26gt; var; means var is read only type for the user. the user(in this case, programmer) may not edit a const type.
volatile %26lt;type var; means that everytime something non-trivial needs to be done with the var's value, it has to be read in at that point. Its value may change in unpredictable ways thanks to 'interrupts'(%26lt;--note: this is the keyword here).
volatile const %26lt;type%26gt; var; means, var's value can change in unpredictable ways thanks to 'interrupts' but the user(programmer, in this case) may not change it by hand.
wondering where this kind of thing would be used? look at some (Linux)OS or device drivers code. they use this kind of declarations in many places.
Reply:Yes.the volatile constant may exist given that there is a separate declaration of the number else where in the program.The local scope is of most priority,more than the public and the private scope.So when decl as a constant in public or private and when in the local it is re-declared.,these r caled volatile constant
To declare a costant:
const var_name = var_value;
to decl volatile const
data_type const var_name= var_value;
survey software
help if yes means how??
Doubt in c program?
first of all volatile is a C++ key word and its not for what u said..
see Wikipedia:
http://en.wikipedia.org/wiki/Volatile_va...
Reply:Welcome the famous old question. volatile and const are not mutually exclusive to each other. That is to say, these keywords can be used together to qualify a type. Both fall under the 'type qualifier' category of keywords.
const %26lt;type%26gt; var; means var is read only type for the user. the user(in this case, programmer) may not edit a const type.
volatile %26lt;type var; means that everytime something non-trivial needs to be done with the var's value, it has to be read in at that point. Its value may change in unpredictable ways thanks to 'interrupts'(%26lt;--note: this is the keyword here).
volatile const %26lt;type%26gt; var; means, var's value can change in unpredictable ways thanks to 'interrupts' but the user(programmer, in this case) may not change it by hand.
wondering where this kind of thing would be used? look at some (Linux)OS or device drivers code. they use this kind of declarations in many places.
Reply:Yes.the volatile constant may exist given that there is a separate declaration of the number else where in the program.The local scope is of most priority,more than the public and the private scope.So when decl as a constant in public or private and when in the local it is re-declared.,these r caled volatile constant
To declare a costant:
const var_name = var_value;
to decl volatile const
data_type const var_name= var_value;
survey software
I have c++ problem here is the function and its error how can I change my function?
void codfile(int *a,int n,int codlength)
{
const int memory=n*codlength/8;
int finalCod [memory]={0};
int k=0,fptr=0,cptr=0;
for(int i=0;i%26lt;n;i++)
{
int temp=a[i];
while(cptr%26lt;codlength)
{
if(fptr==8){ fptr=0;
k++;
}
finalCod [k]=finalCod[k] | temp/2;
finalCod[k]%26gt;%26gt;1;
temp/=2;
cptr++;
fptr++;
}
error in line 4: constant expression required
I have c++ problem here is the function and its error how can I change my function?
The statement
int finalCod [memory]={0};
is illegal because "memory" is a variable.
You need dynamic memory allocation:
either
int *finalCod = new int[memory];
or
int *finalCod = calloc( memory, 1 );
Do not forget to free the memory: for "new" use "delete", for "calloc" use "free()".
Reply:Yeah, what the heck is {0}?
You mean 0? Or 0th element of some array?
Reply:the code seems fine to me. chk whether the main program which generates a function call to codfile changes teh value of codlength? is codlength of type const?? u can try doing two things to remove ther error....first is directly initialize teh array to NULL and second u may try removing the const keyword.
Reply:The error is in this line:
const int memory=n*codlength/8;
memory is a const type. You cannot decide what goes into a constant type at run time. It has to be pre-decided.
Reply:Yeah, just remove const in line 3. The reason why it can't work is because you would be changing the value of memory every time you call the function, so just get rid of const. Or maybe in order for your array to work (int finalCod) with memory, it has to be also constant. So try getting rid of const when declaring memory.
Good Luck
Reply:the problem with ur function is that ur allocating memory statically without knowing exactly how much memory to allocate. this is not possible in c++. try the same thing dynamically using the 'new' operator.for eg int n=new int[];
{
const int memory=n*codlength/8;
int finalCod [memory]={0};
int k=0,fptr=0,cptr=0;
for(int i=0;i%26lt;n;i++)
{
int temp=a[i];
while(cptr%26lt;codlength)
{
if(fptr==8){ fptr=0;
k++;
}
finalCod [k]=finalCod[k] | temp/2;
finalCod[k]%26gt;%26gt;1;
temp/=2;
cptr++;
fptr++;
}
error in line 4: constant expression required
I have c++ problem here is the function and its error how can I change my function?
The statement
int finalCod [memory]={0};
is illegal because "memory" is a variable.
You need dynamic memory allocation:
either
int *finalCod = new int[memory];
or
int *finalCod = calloc( memory, 1 );
Do not forget to free the memory: for "new" use "delete", for "calloc" use "free()".
Reply:Yeah, what the heck is {0}?
You mean 0? Or 0th element of some array?
Reply:the code seems fine to me. chk whether the main program which generates a function call to codfile changes teh value of codlength? is codlength of type const?? u can try doing two things to remove ther error....first is directly initialize teh array to NULL and second u may try removing the const keyword.
Reply:The error is in this line:
const int memory=n*codlength/8;
memory is a const type. You cannot decide what goes into a constant type at run time. It has to be pre-decided.
Reply:Yeah, just remove const in line 3. The reason why it can't work is because you would be changing the value of memory every time you call the function, so just get rid of const. Or maybe in order for your array to work (int finalCod) with memory, it has to be also constant. So try getting rid of const when declaring memory.
Good Luck
Reply:the problem with ur function is that ur allocating memory statically without knowing exactly how much memory to allocate. this is not possible in c++. try the same thing dynamically using the 'new' operator.for eg int n=new int[];
In C++, What use do we have for constants instread of variables?
an example would be
const double PI=3.14;
for(float x=PI;whatever;x++){
cout%26lt;%26lt;x%26lt;%26lt;endl;
}
that was just an example of using constants;
but you could do the same without using constants;
double PI=3.14;
for(int x=PI;whatever;x++){
//This would also work
}
so what use do we have of using constants for somethings instread of variables
In C++, What use do we have for constants instread of variables?
Constants don't change, variables can change.
You can't mistakingly change a constant. For example like this:
if(pi=3.00){
//do something
}
If pi is a constant, you'll get an error message. If pi is a variable, you won't.
Reply:It's just so that no one else changes them. It also indicates to other programmers that the variable is never intended to change.
Reply:Variables use more memory. So if you have a large program you need to consider this.
Reply:Its just validation, it eliminates problems caused by changing variables.
It prevents you from accidentally changing its value and messing everything up. Its very professional. I must admit, I dont use them but I should do.
const double PI=3.14;
for(float x=PI;whatever;x++){
cout%26lt;%26lt;x%26lt;%26lt;endl;
}
that was just an example of using constants;
but you could do the same without using constants;
double PI=3.14;
for(int x=PI;whatever;x++){
//This would also work
}
so what use do we have of using constants for somethings instread of variables
In C++, What use do we have for constants instread of variables?
Constants don't change, variables can change.
You can't mistakingly change a constant. For example like this:
if(pi=3.00){
//do something
}
If pi is a constant, you'll get an error message. If pi is a variable, you won't.
Reply:It's just so that no one else changes them. It also indicates to other programmers that the variable is never intended to change.
Reply:Variables use more memory. So if you have a large program you need to consider this.
Reply:Its just validation, it eliminates problems caused by changing variables.
It prevents you from accidentally changing its value and messing everything up. Its very professional. I must admit, I dont use them but I should do.
Subscribe to:
Posts (Atom)