Im a bit confused

Pages: 123
References in this case, will do the job. I would also recommend to stay away from pointers as much as possible as they are confusing.
So you use pointers to point to a memory address at first and a reference to just reference it later on?
closed account (o3hC5Di1)
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.

All the best,
NwN
So i dont need to use pointers at all, i can just reference?
No, pointers are a must in many cases. You cannot allocate memory on the heap using references, for example.
closed account (o3hC5Di1)
... but for this application, using pointers is unnecessary and asking for trouble.

NwN
... but for this application, using pointers is unnecessary and asking for trouble.


I was talking in general, not about this program.
closed account (o3hC5Di1)
I know - but the OP might not, he's confused enough as it is :)

All the best,
NwN
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 &?
Last edited on
closed account (o3hC5Di1)
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 ;)

All the best,
NwN
Ok, so one last question. How do i change my program from pointers to references, just change all the * to &'s?
closed account (o3hC5Di1)
In stead of doing:

void start(int* S, string* N)

You do:

void start(int& S, string& N)

All the best,
NwN
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?
Last edited on
closed account (o3hC5Di1)
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 :)

All the best,
NwN
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.
closed account (o3hC5Di1)
Best to ask that in the Lounge forum.

There is this section in the articles though: http://cplusplus.com/articles/sourcecode/

All the best,
NwN
Ok well we got off topic on what i was asking:

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <time.h>
#include <random>

using namespace 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);
    }
    else if(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;
        }

        else if(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(&current) << 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?
It seems that you do not clear the input buffer. Try adding cin.ignore(80, '\n') after cin.get() at line 29.

You should start a new thread too.
Hmm that didnt work either
Ah, i got it, i put cin.sync after cin >> choice and it worked. sweeeeeet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "1) New" << endl;
    cout << "2) Load" << endl;
    cin >> choice;
    cin.sync();

    if(choice == "1")
    {
        cout << "Enter your name" << endl;
        getline(cin, name);
        start(score, name);
    }
    else if(choice == "2")
    {
        load(score, name);
    }
    return 0;
Pages: 123