When you pass by reference you are working on the actual string that you are passing into the function, as opposed to working on a copy of the string when you use.
It comes down to acting the same as using a pointer, but allows for less confusion and possible mistakes, because you could change the pointer to point to an entirely different variable - but you can't do that passing by reference.
As I said, leave pointers for what they are until you get a stronger grasp of the language - it's all about using the right tools for the right occasion, in this case, passing by reference is the best option.
Ok so what would be a good example of a program that needs to use pointers? also passing by reference is what i want to do anyways because i want the variable itself changed, not a copy. so i just replace all my * with &?
They are required, for example, for dynamic memory allocation.
When you've used arrays, you actually already have used pointers.
In char arr; arr is actually a pointer to the first element of he array.
The brackets are called the subscript dereferencing operator, because they return the value of the position of the array you request.
You will also use pointers with classes sometimes as the keyword this is actually a pointer to the current object. They can also be used to pass pointers to large objects rather than passing the large objects around.
As I said, don't worry about it too much in the beginning, even though you're eager to learn, you'll know when to use them when you start needing them.
Now then, unless you have any more questions on the original question of this thread - I suggest you create a new one as we're way off the topic again ;)
cool ,i got it all changed and it works. So the & operator gives direct access to the variable allowing it to be changed instead of making copies of it correct? and the * points to the location of the variable in the memory right?
Yes, to be entirely precise, a pointer is a variable (so a space of memory) that holds the address of another variable.
Exactly that (the fact that they are variables) makes them very versatile, this means however that because it's a variable, it can also be changed. This is not necessarily a problem, but you can run into unstability and even security issues when the wrong stuff is written to pointers.
It can go from just unexpected variable contents to buffer overflow exploits and system crashes, which is why I'm so pertinent advising against their use until you know a little bit more.
Keep at it though, you seem very keen to learn, good on you :)
Awesome. Thank you i am extreamly keen on learning C++. I hope to make games someday so I need to learn all I can before i begin :). btw when is the source codes section going to be up. It has said coming soon for like a year or more now.
void start(int &S, string &N);
void save(int &S, string &N);
void load(int &S, string &N);
int score = 0;
cout << "1) New" << endl;
cout << "2) Load" << endl;
cin >> choice;
if(choice == "1")
cout << "Enter your name" << endl;
elseif(choice == "2")
void start(int &S, string &N)
cout << "Ok " << N << " Enter the sentances exactly as you see them to earn points\n" << endl;
string val = RanF();
cout << val << endl;
if(choice == val)
S += 10;
cout << "Current Score: " << S << endl;
elseif(choice == "quit")
}while(choice != "quit");
void save(int &S, string &N)
time_t current = time(0);
file << ctime(¤t) << endl;
file << "";
file << "Score: " << S << endl;
file << "Name: " << N << endl;
file << "" << endl;
void load(int &S, string &N)
file >> S;
file >> N;
TIME = rand() % 6;
sent = "Grand Theft Auto San Andreas";
sent = "GTA IV";
sent = "Dead Island";
sent = "Minecraft";
sent = "Borderlands";
sent = "Crackdown 2";
cout << "Error" << endl;
When i enter 1 for new i enter my name and when it goes to start fucntion, it doesnt show my name and it shows 2 of the random sentance choices. Then when i click load it does the same thing, why is that?