Why isnt this working?

Pages: 12
This code isnt working, why is that? my compiler gives me one error.


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

using namespace std;

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

int main()
{
    string choice;

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

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

void NEWGAMESTART()
{
    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 MAINPROGRAM(string plrname, string scname)
{

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

}

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

    file << "Shipping Company Player Data" << endl;
    file << "\n";
    file << scname << endl;
    file << plrname << endl;
}

void LOADGAME(string scname, string plrname)
{
    ifstream file("SS.txt");

    file >> scname;
    file >> plrname;
}



error


||=== Guess, Debug ===|
obj\Debug\main.o||In function `main':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|29|undefined reference to `MAINPROGRAM()'|
||=== Build finished: 1 errors, 0 warnings ===|


why does it keep giving me this? i cant seem to fix it and i tried like 4 different things with no luck, some of them just made it much worse.
Last edited on
This is an interesting bug because it begs learning something important (unless you already know it then you will know how to fix the code). The term is called function overloading. Ill point you in a direction.

http://www.learncpp.com/cpp-tutorial/76-function-overloading/

after you read that, look how you are calling MAINPROGRAM();
Last edited on
But i dont understand why i cantuse it when ive used SAVEGAME the same exact way. i read the link and still dont quite understand.
You declared function MAINPROGRAM as

void MAINPROGRAM();

but defined it as

void MAINPROGRAM(string plrname, string scname)

So the compiler does not see the definition of

void MAINPROGRAM();
9 times out of 10, "undefined reference" means the linker cannot find the body for a function.

Here, that function is MAINPROGRAM(). You prototyped that function, but never gave it a body.

Note you made a separate MAINPROGRAM(string,string) function, but since that takes a different parameter list, it counts as a different function. The MAINPROGRAM you are calling from your main takes no parameters, and that function has no body.
just because two functions have the same name doesn't mean they are the same function. if in my code I did something like this:
int a;
char a;
I have just defined two very different variables. this is because int a != char a.
I can say the same thing about functions, the argument list is also used as an identifier along with the name. the first one is what you called while the second one is what you prototyped:

mainprogram() - we will call this variable a
mainprogram(string, string) - this is variable b

when I do a call to variable a the compiler says, "well that's great but you haven't defined a for me." this is because you have only defined variable b and since a != b the compiler starts crying.

you actually haven't used save game the same way. you have prototyped save game as:
savegame (string, string)
and called it in your program as:
savegame (string, string)
since savegame(string,string) = savegame(string, string) the compiler knows what to do with the call. remember the compiler is dumb so you have to be very explicit with it.
Last edited on
@slider57
if in my code I did something like this:
int a;
char a;
I have just defined two very different variables. this is because int a != char a.


Maybe you made a type but in any case you may not define two variables with the same name in the same declaration region as you showed.
Last edited on
Ok so i think i understand, I still got errors when i changed it but tell me if im headed in the right direction:

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

using namespace std;

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

int main()
{
    string choice;

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

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

void NEWGAMESTART()
{
    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 MAINPROGRAM(string plrname, string scname)
{

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

}

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

    file << "Shipping Company Player Data" << endl;
    file << "\n";
    file << scname << endl;
    file << plrname << endl;
}

void LOADGAME(string scname, string plrname)
{
    ifstream file("SS.txt");

    file >> scname;
    file >> plrname;
}


errors


||=== Guess, Debug ===|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|29|error: 'plrname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|29|error: 'scrname' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|
You can read the error messages right? In main, there are no variables defined or declared with the names plrname or scrname. Perhaps you should rectify that.
You use these variables but they were not defined in the main

1
2
3
4
    else if(choice == "Load" || choice == "load")
    {
        MAINPROGRAM(plrname, scrname);
    }
But they need to be in mainprogram because thats where they will be used. but i think i know of a way, so i puit those variables in main then reference them to newgamestart i believe.
ok nbow it works
theres so many references its sort of overwelming and im going to have a ton more, if it was just these two or one it would be perfectly ok but i dont like having more than 4 references in each parameter, so what is another way to do it? someone said something about vectors and arrays, but can i use a struct?
Last edited on
bump
@ vald from moscow - Yea thats the kind of mistakes that happen at four in the morning when im thinking about functions but writing something entirely different. Oh well it happens.

@ch1156 - Well you could write a class I guess. It would look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Company
{
public:
Company ();
Company (string scname1, string plrname);
void loadGame();
void saveGame();
void mainProgram();
void newGameStart();
private:
string scname;
string plrname;
};

This way as far as this program is right now, you wouldn't have to pass any parameters because they are all contained within the object. As far as the program goes right now I dont think using an array or vector would make much sense.
Ok i have never used classes but this is a good time to learn . I set it up in my code so what do i do now, i got a ton of errors, what am i 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
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
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <fstream>
#include <windows.h>

using namespace std;

class Company
{
public:
Company ();
Company (string scname, string plrname);
void LOADGAME();
void SAVEGAME();
void MAINPROGRAM();
void NEWGAMESTART();
private:
string scname;
string plrname;
};

void MAINPROGRAM();
void NEWGAMESTART();
void SAVEGAME();
void LOADGAME();

int main()
{
    string choice;
    string scname;
    string plrname;

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

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

void NEWGAMESTART()
{
    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";

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

void MAINPROGRAM()
{
    cout << scname << endl;
    cout << "\n";


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

}

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

    file << "Shipping Company Player Data" << endl;
    file << "\n";
    file << scname << endl;
    file << plrname << endl;
}

void LOADGAME()
{
    ifstream file("SS.txt");

    file >> scname;
    file >> plrname;
}


ERRORS:


C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|25|error: too many arguments to function 'void NEWGAMESTART()'|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|41|error: at this point in file|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|24|error: too many arguments to function 'void MAINPROGRAM()'|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|45|error: at this point in file|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'void NEWGAMESTART()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|58|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|63|error: 'plrname' 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|75|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|81|error: 'plrname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'void SAVEGAME()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|92|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|93|error: 'plrname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'void LOADGAME()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|100|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|101|error: 'plrname' was not declared in this scope|
||=== Build finished: 12 errors, 0 warnings ===|
what am i doing wrong?


Writing code and hoping it turns okay without understanding what you're doing.
Well first of all each of your functions that are in the class need to be modified a bit. When I do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Company
{
public:
Company ();
Company (string scname, string plrname);
void LOADGAME();
void SAVEGAME();
void MAINPROGRAM();
void NEWGAMESTART();
private:
string scname;
string plrname;
};

You are basically prototyping everything that is in the class. Next what you need to do is erase all your original prototypes because you already prototpyed them in the class. Lines to erase are 24-27. Add a couple more functions called constructors like this:
1
2
3
4
5
6
7
8
9
10
Company () // default constructor
{
scname = "";
plrname = "";
}
Company (string scname1, string plrname1)
{
scname = scname1;
plrname = plrname1;
}

Then go down to where you have your functions defined for the prototype and change it around a bit such as:
1
2
3
void Company :: MAINPROGRAM()
{
}

This tells the compiler, "hey, i have prototyped mainprogram() in the class Company, so use that." You must add the class and the scope resolution operator (::) to each of your function definitions (NOTE: do not change the prototyped functions inside your class). Once your done with that, in main
create the object like this:
 
Company company1;

after that you use the dot operator to call any of the 'member functions' like this: company1.savegame();
all the data is passed in object company1 to the member function savegame().

Next read a little about classes to help solidify what classes are and how to use them.
ok i'll change all of this and read up on classes but one last question for now, why do i need

scname = "";
plrname = "";

???
its just good practice to initialize your variables.
Pages: 12