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[10]; 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.
It seems every time i make a menu something goes wrong. Now i have this menu and everything compiles correctly but the menu doesnt work as it should. everything else works fine though.
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <time.h>
#include <random>
usingnamespace std;
void start(int &S, string &N);
void save(int &S, string &N);
string RanF();
void load(int &S, string &N);
int main()
{
int score = 0;
string name;
string choice;
cout << "1) New" << endl;
cout << "2) Load" << endl;
cin >> choice;
if(choice == "1")
{
cout << "Enter your name" << endl;
getline(cin, name);
cin.get();
start(score, name);
}
elseif(choice == "2")
{
load(score, name);
}
return 0;
}
void start(int &S, string &N)
{
string choice;
cout << "Ok " << N << " Enter the sentances exactly as you see them to earn points\n" << endl;
do
{
string val = RanF();
cout << val << endl;
getline(cin, choice);
if(choice == val)
{
S += 10;
cout << "Current Score: " << S << endl;
}
elseif(choice == "quit")
{
save(S,N);
break;
}
}while(choice != "quit");
}
void save(int &S, string &N)
{
ofstream file;
file.open("file.txt", ios::app);
time_t current = time(0);
file << ctime(¤t) << endl;
file << "";
file << "Score: " << S << endl;
file << "Name: " << N << endl;
file << "" << endl;
file.close();
}
void load(int &S, string &N)
{
ifstream file;
file.open("file.txt");
file >> S;
file >> N;
file.close();
return start(S,N);
}
string RanF()
{
int TIME;
string sent;
time_t t;
ctime(&t);
srand(time(NULL));
TIME = rand() % 6;
switch(TIME)
{
case 0:
sent = "Grand Theft Auto San Andreas";
return sent;
case 1:
sent = "GTA IV";
return sent;
case 2:
sent = "Dead Island";
return sent;
case 3:
sent = "Minecraft";
return sent;
case 4:
sent = "Borderlands";
return sent;
case 5:
sent = "Crackdown 2";
return sent;
default:
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?