Cant understand Referencing

I have been looking at referencing (& symbol) and i cant seem to figure it out, in my code i want to get the plrname and scname from MAINPROGRAM so i can save the data but how do i do it, can someone please show me what im doing wrong.

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
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <fstream>

void MAINPROGRAM(int &scname, int &plrname);

using namespace std;

int main()
{
    string choice;

    cout << "New" << endl;
    cout << "Load" << endl;
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        MAINPROGRAM();
    }
    else if(choice == "Load" || choice == "load")
    {

    }
}

void MAINPROGRAM()
{
    string &scname;
    string &plrname;

    if(cin.peek() == '\n')
    {
        cin.ignore();
    }

    cout << "Welcome to Shipping Supplies" << endl;
    cout << "Please enter the name of your shipping company" << endl;
    getline(cin, scname);
    cout << "\n";
    cout << "Ok so the name of your company is " << scname << endl;
    cout << "\n";
    cout << "And what is your name?" << endl;
    getline(cin, plrname);
    cout << "Ok " << plrname << " welcome to the game! lets get started" << endl;
    cout << "\n";

}

void SAVEGAME()
{
    ofstream file("SS.txt");

    file << &plrname << endl;
    file << &scname << endl;
}
& gets the address of a variable. You don't need it.

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
void MAINPROGRAM( ); //Prototype should have same parameters as declaration

using namespace std;

int main()
{
    string choice;

    cout << "New" << endl;
    cout << "Load" << endl;
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        MAINPROGRAM();
    }
    else if(choice == "Load" || choice == "load")
    {

    }
}

void MAINPROGRAM()
{
    string scname; //Don't need the address of variable you're declaring. That makes no sense.
    string plrname;

    if(cin.peek() == '\n')
    {
        cin.ignore();
    }

    cout << "Welcome to Shipping Supplies" << endl;
    cout << "Please enter the name of your shipping company" << endl;
    getline(cin, scname);
    cout << "\n";
    cout << "Ok so the name of your company is " << scname << endl;
    cout << "\n";
    cout << "And what is your name?" << endl;
    getline(cin, plrname);
    cout << "Ok " << plrname << " welcome to the game! lets get started" << endl;
    cout << "\n";

}

void SAVEGAME()
{
	string plrname; //Make these 2 variables. Otherwise you have nowhere to load plrname and filename in from file
	string scname;

    ofstream file("SS.txt");

    file << plrname << endl;
    file << scname << endl;

	//Call the game function using plrname and scname as parameters
}
ok so could you actually do it so i can see it, cause thats the part im confused with.
Sorry about that, I misunderstood what you wanted. Here you go.
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
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <fstream>

using namespace std;

void MAINPROGRAM( ); //Prototype should have same parameters as declaration
void SAVEGAME( string , string );


int main()
{
    string choice;

    cout << "New" << endl;
    cout << "Load" << endl;
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        MAINPROGRAM();
    }
    else if(choice == "Load" || choice == "load")
    {

    }

	return 0;
}

void MAINPROGRAM()
{
    string scname; //Don't need the address of variable you're declaring. That makes no sense.
    string plrname;

    if(cin.peek() == '\n')
    {
        cin.ignore();
    }

    cout << "Welcome to Shipping Supplies" << endl;
    cout << "Please enter the name of your shipping company" << endl;
    getline(cin, scname);
    cout << "\n";
    cout << "Ok so the name of your company is " << scname << endl;
    cout << "\n";
    cout << "And what is your name?" << endl;
    getline(cin, plrname);
    cout << "Ok " << plrname << " welcome to the game! lets get started" << endl;
    cout << "\n";

	SAVEGAME( scname, plrname ); //call save game using these paremeters
}

void SAVEGAME( string scname, string plrname )
{
    ofstream file("SS.txt");

    file << plrname << endl;
    file << scname << endl;

}
Last edited on
ok i sort of get it now, but my question is what if i have like 12 different things i want to reference? the parameters would get too crouded so is there an alternative or something??

also i got some errors:

C:\Users\Chay Hawk\Desktop\Guess\main.cpp|9|error: variable or field 'SAVEGAME' declared void|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|9|error: 'string' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|9|error: 'string' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'void MAINPROGRAM()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|43|error: 'SAVEGAME' was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings ===|


code:

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
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <fstream>
#include <windows.h>

void MAINPROGRAM();
void SAVEGAME(string, string);

using namespace std;

int main()
{
    string choice;

    cout << "New" << endl;
    cout << "Load" << endl;
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        MAINPROGRAM();
    }
    else if(choice == "Load" || choice == "load")
    {

    }
}

void MAINPROGRAM()
{
    string scname;
    string plrname;

    if(cin.peek() == '\n')
    {
        cin.ignore();
    }

    if(WM_QUIT)
    {
        SAVEGAME(scname, plrname);
    }

    cout << "Welcome to Shipping Supplies" << endl;
    cout << "Please enter the name of your shipping company" << endl;
    getline(cin, scname);
    cout << "\n";
    cout << "Ok so the name of your company is " << scname << endl;
    cout << "\n";
    cout << "And what is your name?" << endl;
    getline(cin, plrname);
    cout << "Ok " << plrname << " welcome to the game! lets get started" << endl;
    cout << "\n";

}

void SAVEGAME(string scname, string plrname)
{
    ofstream file("SS.txt");

    file << plrname << endl;
    file << scname << endl;
}


you said something about making them variables, but their strings, or wont that matter? i dont know what im trying to say...
Last edited on
bump
References... they're aliases (alternative names, if you will) to a variable. You "feed" them the variable they're referencing at declaration.
1
2
int a; // regular variable
int &ra = a; // reference to `a' 

You cannot change what variable ra is referencing, it will be a forever.

Now, when you use references as function parameters, it means that the variable you call the function with can be changed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

void regular_function(int i)
{
    i=5; // the outside `i' doesn't change, because `i' is a local copy
}

void smart_function(int &i)
{
    i=5; // the outside `i' changes to 5, because `i' is a reference
}

int main()
{
    int var = 10;

    smart_function(var);
    std::cout << var << std::endl;
}


http://ideone.com/A08H7
The errors are coming from the compiler not know what a string is. Put your declare namespace std before your SAVEGAME prototype. And to get around having too many variables, if they're all the same type, then put them in an array or a vector and pass that.
Arrays are easier, but you can't change the size of them after you make them. Vectors are a memory hog, but can be resized upon will.
Last edited on
ok it works now, but the name of the company and player name arent saving in the text file?

UPDATED CODE:

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
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <fstream>
#include <windows.h>

using namespace std;

void MAINPROGRAM();
void SAVEGAME(string, string);

int main()
{
    string choice;

    cout << "New" << endl;
    cout << "Load" << endl;
    cin >> choice;

    if(choice == "New" || choice == "new")
    {
        MAINPROGRAM();
    }
    else if(choice == "Load" || choice == "load")
    {

    }
}

void MAINPROGRAM()
{
    string scname;
    string plrname;

    if(cin.peek() == '\n')
    {
        cin.ignore();
    }

    if(WM_QUIT)
    {
        SAVEGAME(scname, plrname);
    }

    cout << "Welcome to Shipping Supplies" << endl;
    cout << "Please enter the name of your shipping company" << endl;
    getline(cin, scname);
    cout << "\n";
    cout << "Ok so the name of your company is " << scname << endl;
    cout << "\n";
    cout << "And what is your name?" << endl;
    getline(cin, plrname);
    cout << "Ok " << plrname << " welcome to the game! lets get started" << endl;
    cout << "\n";

}

void SAVEGAME(string scname, string plrname)
{
    ofstream file("SS.txt");

    file << plrname << endl;
    file << scname << endl;
}
Last edited on
also can i use a struct for the variables instead of a vector or an array?
Topic archived. No new replies allowed.