• Welcome to Valhalla Legends Archive.
 

How to define variables

Started by Dyndrilliac, January 09, 2004, 10:47 AM

Previous topic - Next topic

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:

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


can anyone give me a brief explanation? thanks.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Grok


  int a = 3;

// or

  int a;
  a = 3;


Is this what you meant?

Dyndrilliac

Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

MoNksBaNe_Agahnim

or you can make a global constant variable...such as



#include yadda yadda

const int a = 2;

int main()
{
blah here

return 0;

}


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


#include blah blah

int main()
{
  const int a = 2;

 //whatever you want in the rest of this function

  return 0;
}


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

Adron

You can also use


#define a 2


which will make a equivalent to two from that point to the end of the file, or a redefinition.

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

Grok

*regrets unbanning you already*

Adron

Quote from: CrAzY on January 14, 2004, 07:37 AM
Does he really need a constant... He said int, not const int, int a = 1; or
int a;
a = 1;

Read what he post...

Doesn't matter what he needs, someone else reading this post may want constant variables or definitions. His original question was answered long ago.