Valhalla Legends Forums Archive | C/C++ Programming | How to define variables

AuthorMessageTime
Dyndrilliac
I'm making a small number game in c++ in a win32 console app and want to know how to tell the program what an integer value is without having to use the cin statment, like, i want to make the following:

[code]int main()
{
int a;
(line telling what number "a" is without using an equation or user input)
*rest of code*
}[/code]

can anyone give me a brief explanation? thanks.
January 9, 2004, 4:47 PM
Grok
[code]
int a = 3;

// or

int a;
a = 3;
[/code]

Is this what you meant?
January 9, 2004, 5:09 PM
Dyndrilliac
yea :D Thanks!
January 9, 2004, 5:24 PM
MoNksBaNe_Agahnim
or you can make a global constant variable...such as

[code]

#include yadda yadda

const int a = 2;

int main()
{
blah here

return 0;

}
[/code]

this is not only setting the variable 'a' to the value of 2 for the whole program, meaning it will always equal two can't change it, but also other functions outside of int main() can use it as well, usefull when you get into making your own functions

or you can make it a local constant variable...

[code]
#include blah blah

int main()
{
const int a = 2;

//whatever you want in the rest of this function

return 0;
}
[/code]

now a is = to 2 for the whole program like above, but I made it a local variable so that only int main() can use it

I'm sure ive confused you by now hahah so reaaaaaaaaal quick a brief recap for this is...

-variable declared outside of a function, such as int main() can be used by all functions in the program, example 1

-variable declared within a function can be used only by that function, example 2

hope this helps
January 9, 2004, 9:21 PM
Adron
You can also use

[code]
#define a 2
[/code]

which will make a equivalent to two from that point to the end of the file, or a redefinition.
January 9, 2004, 11:59 PM
CrAzY
Does he really need a constant... He said int, not const int, int a = 1; or
int a;
a = 1;

Read what he post...
January 14, 2004, 1:37 PM
Grok
*regrets unbanning you already*
January 14, 2004, 2:33 PM
Adron
[quote author=CrAzY link=board=30;threadid=4660;start=0#msg39569 date=1074087474]
Does he really need a constant... He said int, not const int, int a = 1; or
int a;
a = 1;

Read what he post...
[/quote]

Doesn't matter what he needs, someone else reading this post may want constant variables or definitions. His original question was answered long ago.
January 14, 2004, 9:04 PM

Search