Hi
I have an assignment that is an expansion of previous posts, my payroll program. I'm having a very hard time understanding the text (Deitel, FWIW), assignments, and discussions. It's too verbose. I need examples.
What is a global variable? Is that when at the beginning I say int hoursWorked? If so, and the assignment says use only local variables only, how do I make them only locally-accessible?
What is an array? Would that be how I show each employee's pay in a list? i.e. c[ 0 ] $1000; c[ 1 ] $1250 etc. etc., for employee #s 0 and 1?
I hate reading this stuff. I wish the text just showed some code and said "line 12 is ____. They do this:____"
A global variable is a variable that does not reside in a function or a class/struct.
1 2 3 4 5 6 7 8
// example program
int global_var; // global because it's not in a function
int main()
{
int local_var; // variable local to main. it is not global
}
If so, and the assignment says use only local variables only, how do I make them only locally-accessible?
Pick one function which is the "owner" of the variable (most likely this will be main for a simple program). Any other functions that need to access it can take it in the form of a parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
void examplefunc( int param )
{
std::cout << param; // prints "5", which is the contents of main's 'foo' variable
}
int main()
{
int foo = 5; // variable local to main. "owned by" main
examplefunc( foo ); // pass it to examplefunc as a parameter
// in example func, this would be like saying:
// int param = mains_foo;
// except the compiler does that for you
}
What is an array?
An array is a group of variables that share a common name and type, and which can be access with an "index". Each "element" in the array is a unique variable and has a unique index.
1 2 3 4 5
int myarray[10]; // an array of 10 elements
myarray[0] = 5; // set the first element to 5
myarray[1] = 6; // set the next one to 6
// ... etc
Would that be how I show each employee's pay in a list?
Probably. But try not to use the word "list" because that means something else, and you'll get confused later when you have to learn about lists. It's an array. Call it that.
I hate reading this stuff. I wish the text just showed some code and said "line 12 is ____. ____s do ____."
I am also a learn-by-example guy. If you've seen my other posts on this forum, 90% of the time I try to include examples even for trivial problems just because I think they help the clarity. So I can understand your position.
It's important to understand the underlying concepts are. Arrays, global variables, etc are very fundamental concepts that you have to burn into your brain. They will be used so frequently in the future that having an example presented every time they're used would be absurd.
Disch, I'm definitely a learn-by-example person as well! That post really helped, thanks!
Meerkat, thanks for those links. I think I've had enough of the text, I know more about arrays after reading the first paragraph of the tutorial than I did after reading 40 pages of the array chapter in my text.