A little help?
by Joshua Cruikshank · in Technical Issues · 05/15/2002 (6:52 pm) · 12 replies
Well this is probly a dumb question but I am learning C++ and my book did not mention why you use constants or enumerated constants instead of some sort of integer. I was just wondering why you use what and what for. dunno maby it will explain in a later chapter.
-thanks
-thanks
#2
If you mean why use enum over an int, sometimes you need the flexibility of that data type. If you mean 'why use a constant', it's so you can make sure a value stays the same, which could potentially be a problem, for instance if more than one person are working on a particular bit of code and the other one writes a function that screws with the value of a variable that he shouldn't screw with. If it's not a constant, it will just change the value and the program will continue to execute, and you'll either get some very confusing errors or confusing output or both.
btw, welcome to C++. :)
05/15/2002 (7:36 pm)
Well, not sure if this is what u mean, but if you mean why use a variable at all when you could just type in the number you need, the answer is that in some very rare instances that is ok, but the point with constants is that you need to have a constant value that you can reference from wherever, and you can change its value easily when editing, instead of editing every function where you use that number . . . kinda the reason we have variables at all. :)If you mean why use enum over an int, sometimes you need the flexibility of that data type. If you mean 'why use a constant', it's so you can make sure a value stays the same, which could potentially be a problem, for instance if more than one person are working on a particular bit of code and the other one writes a function that screws with the value of a variable that he shouldn't screw with. If it's not a constant, it will just change the value and the program will continue to execute, and you'll either get some very confusing errors or confusing output or both.
btw, welcome to C++. :)
#3
and enum is there so you dont have to write a line for every veriable. am I right?
05/16/2002 (3:08 pm)
Oh so constants are unchangeable no matter what you do while just normal integers like int x=2 are changable.and enum is there so you dont have to write a line for every veriable. am I right?
#4
there are many use's for enumerations.
they are a handy storage place, with some features.
I dont understand the question I think.
05/16/2002 (4:12 pm)
Indeed hence the name Constant :)Quote:enum is there so you dont have to write a line for every veriable. am I right?umm...
there are many use's for enumerations.
they are a handy storage place, with some features.
I dont understand the question I think.
#5
const int sat=1;
const int sun=2;
const int mon=3;
etc...
if that helps explain what I main......
PS anyone know why the following does not work?
#include
int ADD()
{
using std::cout;
unsigned const long int SMALLER=142;
unsigned const long int BIGGER=523;
unsigned int AWNSER;
return 0;
}
int main()
{
using std::cout;
signed int AWNSER;
int SMALLER;
int BIGGER;
cout << "wanna know what 523 pluss 142 is??\n";
ADD();
AWNSER=SMALLER+BIGGER;
cout << AWNSER;
cout << "wanna know a few other numbers\n" << std::endl;
cout << ++AWNSER;
cout << ++AWNSER;
cout << ++AWNSER;
cout << ++AWNSER;
return 0;
}
its diferant from when i made it because i was trying to fix it anyway it returns -1717986919 as the awnser to everything. If you know whats wrong please post here.
05/16/2002 (9:20 pm)
I dunno im just confusing everyone including myself. whatever I think I got it thanks for the help. The book sead that when you go enum {sat, sun, mon} it set integers 1 2 and 3 for those so.... what i got from it is that it makes it so you dont have to goconst int sat=1;
const int sun=2;
const int mon=3;
etc...
if that helps explain what I main......
PS anyone know why the following does not work?
#include
int ADD()
{
using std::cout;
unsigned const long int SMALLER=142;
unsigned const long int BIGGER=523;
unsigned int AWNSER;
return 0;
}
int main()
{
using std::cout;
signed int AWNSER;
int SMALLER;
int BIGGER;
cout << "wanna know what 523 pluss 142 is??\n";
ADD();
AWNSER=SMALLER+BIGGER;
cout << AWNSER;
cout << "wanna know a few other numbers\n" << std::endl;
cout << ++AWNSER;
cout << ++AWNSER;
cout << ++AWNSER;
cout << ++AWNSER;
return 0;
}
its diferant from when i made it because i was trying to fix it anyway it returns -1717986919 as the awnser to everything. If you know whats wrong please post here.
#6
C/C++ give you lots of power. In this case it means that they don't initialize variables for you. Your AWNSER variable seems to be uninitialized - meaning that it just has some random value in it. Make it =0; somewhere and all will be well.
05/16/2002 (10:03 pm)
Check your variable initialization.C/C++ give you lots of power. In this case it means that they don't initialize variables for you. Your AWNSER variable seems to be uninitialized - meaning that it just has some random value in it. Make it =0; somewhere and all will be well.
#7
05/17/2002 (5:58 am)
ok cool thanks.
#8
There are three scopes: Global, Member and Local.
Member is to do with Classes in object oriented programming. I will not confuse you with this one.
The Global scope is part of the program that is not delimited within braces - '{}' or is not inside a function or a class etc. Variables declared in the Global scope is accessible from any part of the program that knows about the global (i.e. any part that #includes the file containing the global.).
Then you have local the local scope. This is the area of the source code delimited within a pair of braces - '{}' or within a function. Variables declared in this scope can only be used within this scope. Nothing outside the scope knows of the variable.
The problem here is with scope. You've declared and initialised the variables in the scope of the ADD() functions (remember: local scope). Nothing outside the ADD() function knows how to use those variables.
You then declared variables bearing the same name, but of different type in the main() function. These variables may be of the same name, but they are totally different and have nothing to do with the ones in ADD. remember, ADD() doesn't know that these even exist in main() and vice versa.
And since you haven't initialised the variables in main(), they have a bizarre value. Understood?
Now if you want to send of your variables to another function to be modified in any way, you need to do it by pointers or references. preferably by the latter. But I'm not sure you're ready for that.
If on the other hand you just want to get the some of the two numbers, than do this:
Initialise the two numbers (SMALLER and BIGGER) in main().
Change the ADD() function to the following:
and change the following lines from
to
That should do it.
you do know about function parameters, dont you? They're just a way of passing values from one place of the program to another. As you have seen, the names of the parameter variables do not have to be the same as the variables of which the values are being sent.
05/17/2002 (12:59 pm)
New word for the day: SCOPEThere are three scopes: Global, Member and Local.
Member is to do with Classes in object oriented programming. I will not confuse you with this one.
The Global scope is part of the program that is not delimited within braces - '{}' or is not inside a function or a class etc. Variables declared in the Global scope is accessible from any part of the program that knows about the global (i.e. any part that #includes the file containing the global.).
Then you have local the local scope. This is the area of the source code delimited within a pair of braces - '{}' or within a function. Variables declared in this scope can only be used within this scope. Nothing outside the scope knows of the variable.
The problem here is with scope. You've declared and initialised the variables in the scope of the ADD() functions (remember: local scope). Nothing outside the ADD() function knows how to use those variables.
You then declared variables bearing the same name, but of different type in the main() function. These variables may be of the same name, but they are totally different and have nothing to do with the ones in ADD. remember, ADD() doesn't know that these even exist in main() and vice versa.
And since you haven't initialised the variables in main(), they have a bizarre value. Understood?
Now if you want to send of your variables to another function to be modified in any way, you need to do it by pointers or references. preferably by the latter. But I'm not sure you're ready for that.
If on the other hand you just want to get the some of the two numbers, than do this:
Initialise the two numbers (SMALLER and BIGGER) in main().
Change the ADD() function to the following:
int ADD(int num1, int num2)
{
return num1 + num2;
}and change the following lines from
ADD(); AWNSER=SMALLER+BIGGER;
to
ANSWER = ADD(SMALLER, BIGGER);
That should do it.
you do know about function parameters, dont you? They're just a way of passing values from one place of the program to another. As you have seen, the names of the parameter variables do not have to be the same as the variables of which the values are being sent.
#9
05/17/2002 (3:14 pm)
OOOOH! cool thanks I had tryed do do somthing like what you showed there but it did not work and you just cleared it up for me. in the end I just got rid of ADD all together and it worked fine even though it was not made how I had wanted. but now I think I will remake it using the info you just gave me. thanks!
#10
#include
int Area(int length, int width);
int main()
{
using namespace std;
int lengthOfYard;
int widthOfYard;
int areaOfYard;
cout << "\nHow wide is your yard?";
cin >> widthOfYard;
cout << "\nHow long is your yard?";
cin >> lengthOfYard;
areaOfYard= Area(lengthOfYard,widthOfYard);
cout << "\nYour yard is ";
cout << areaOfYard;
cout << "square feet\n\n";
return 0;
}
int Area(int 1, int w)
{
return 1*w;
}
--------------------Configuration: Function Declaration - Win32 Debug--------------------
Compiling...
MAIN.cpp
C:\Windows\Function Declaration\MAIN.cpp(32) : error C2143: syntax error : missing ')' before 'constant'
C:\Windows\Function Declaration\MAIN.cpp(32) : error C2143: syntax error : missing ';' before 'constant'
C:\Windows\Function Declaration\MAIN.cpp(32) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
Function Declaration.exe - 3 error(s), 0 warning(s)
05/19/2002 (10:18 am)
this is right out of my book and i cant figure out why it gives me theses errors.#include
int Area(int length, int width);
int main()
{
using namespace std;
int lengthOfYard;
int widthOfYard;
int areaOfYard;
cout << "\nHow wide is your yard?";
cin >> widthOfYard;
cout << "\nHow long is your yard?";
cin >> lengthOfYard;
areaOfYard= Area(lengthOfYard,widthOfYard);
cout << "\nYour yard is ";
cout << areaOfYard;
cout << "square feet\n\n";
return 0;
}
int Area(int 1, int w)
{
return 1*w;
}
--------------------Configuration: Function Declaration - Win32 Debug--------------------
Compiling...
MAIN.cpp
C:\Windows\Function Declaration\MAIN.cpp(32) : error C2143: syntax error : missing ')' before 'constant'
C:\Windows\Function Declaration\MAIN.cpp(32) : error C2143: syntax error : missing ';' before 'constant'
C:\Windows\Function Declaration\MAIN.cpp(32) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
Function Declaration.exe - 3 error(s), 0 warning(s)
#11
int Area(int 1, int w)
{
return 1*w;
}
the 1 should be an l (as in L, for length). It probably just looked like a 1 from the book. Your variable names can't start with a number, they can have a number in them, just not as the first character.
05/19/2002 (10:30 am)
See where you wrote:int Area(int 1, int w)
{
return 1*w;
}
the 1 should be an l (as in L, for length). It probably just looked like a 1 from the book. Your variable names can't start with a number, they can have a number in them, just not as the first character.
#12
05/19/2002 (1:08 pm)
ok cool thanks. maby the book has a typo cuz it still looks like a 1 when i see it.... dunno.
Torque Owner Badguy
enum
{
Data0,
Data1,
Data2,
Data3
}
instead of
0
1
2
3
then it might be obvious ..
the name :)
it is a handle now in the code when operating data
we can say something like
is this data Data1?
instead of
is this data 1?
I dont know after re-reading it .. I only hope you can make sense of it :)
Edit :
oops :) I forgot Real programmers count from Zero!