Why isnt this working?

Pages: 12
That's good advice for basic types like int (which are not default constructed), but doing that for complex types is a waste. string's constructor will already make an empty string. Reassigning it to an empty string again when it's already empty is pointless.
Last edited on
oh, so what does it actually do? by initialize you mean it just gets them ready for use?
so the variable has some value in it at the start. it just helps with debugging.

I did not know the string type had a constructor for it until you said something. then I thought to myself... you have been using string member functions all this time and it didn't click that it was a class. lol im an idiot sometimes. thanks for correcting me.
its good programming practice but is it necessary?
Ch1156 wrote:
its good programming practice but is it necessary?

As long as you initialize the class members before use (in non-default constructed objects) then probably no. But think about code re-usability, you should want to write code you can re-use and (hopefully) others will use. If I where to use your class I would expect all class members to have default values, and if without them it only blows up in my face once in million runs I wont use your class. Its just safer to give defaults and usually doesn't cost much (time or energy).
Last edited on
so like this?

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

using namespace std;

class Company
{
public:
Company () // default constructor
{
scname = "";
plrname = "";
}
Company (string scname, string plrname)
{
scname = scname;
plrname = plrname;
}
void LOADGAME();
void SAVEGAME();
void MAINPROGRAM();
void NEWGAMESTART();
private:
string scname;
string plrname;
};

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;
}



you said do the void company:: functionname
thing to them but i cant do it to main can i?

errors


||=== Guess, Debug ===|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|44|error: 'NEWGAMESTART' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|48|error: 'MAINPROGRAM' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'void NEWGAMESTART()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|61|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|66|error: 'plrname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|72|error: 'SAVEGAME' 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|78|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|84|error: 'plrname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|84|error: 'SAVEGAME' 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|95|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|96|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|103|error: 'scname' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|104|error: 'plrname' was not declared in this scope|
||=== Build finished: 12 errors, 0 warnings ===|
Your function is
NEWGAMESTART()
but not
NEWGAMESTART(string a, string b)

Hence, the compiler would think that you have different functions, although you think that they're the same to you.

Same goes to the rest.
Ok i got it down to just 2 errors, but i dont get what they mean?


C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|44|error: cannot call member function 'void Company::NEWGAMESTART()' without object|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|48|error: cannot call member function 'void Company::MAINPROGRAM()' without object|
||=== Build finished: 2 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
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
#include <iostream>
#include <ctime>
#include <random>
#include <string>
#include <fstream>
#include <windows.h>

using namespace std;

class Company
{
public:
Company () // default constructor
{
scname = "";
plrname = "";
}
Company (string scname, string plrname)
{
scname = scname;
plrname = plrname;
}
void LOADGAME();
void SAVEGAME();
void MAINPROGRAM();
void NEWGAMESTART();
private:
string scname;
string plrname;
};

int main()
{
    string choice;

    Company CO2;
    CO2.NEWGAMESTART();

    Company CO;
    CO.MAINPROGRAM();

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

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

void Company::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();
    }
}

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


    if(WM_QUIT)
    {
        SAVEGAME();
    }

}

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

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

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

    file >> scname;
    file >> plrname;
}


I created objects for them but it still gives me the same errors?
Last edited on
closed account (zb0S216C)
Ch1156 wrote:
"by initialize you mean it just gets them ready for use?"

I guess you could say that. Initialisation is to give an object a default value in exchange for the value the compiler provides.

A class definition, such as Company is not an object, but a mere template for a class. To create an object, you must instantiate the class. In your case:

 
Company NewCompany;

When a class is instantiated, each instantiation is given its own copy of the members, except for static members. To access the object, you need to member-access operator AKA "The Dot Operator". For example:

 
NewCompany.NEWGAMESTART();

Wazzak
Like this?

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

using namespace std;

class Company
{
public:
Company () // default constructor
{
scname = "";
plrname = "";
}
Company (string scname, string plrname)
{
scname = scname;
plrname = plrname;
}
void LOADGAME();
void SAVEGAME();
void MAINPROGRAM();
void NEWGAMESTART();
private:
string scname;
string plrname;
};

int main()
{
    string choice;
    Company NewCompany;

    NewCompany.NEWGAMESTART();
    NewCompany.MAINPROGRAM();

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

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

void Company::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();
    }
}

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


    if(WM_QUIT)
    {
        SAVEGAME();
    }

}

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

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

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

    file >> scname;
    file >> plrname;
}

}


I still got errors?


C:\Users\Chay Hawk\Desktop\Guess\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|46|error: cannot call member function 'void Company::NEWGAMESTART()' without object|
C:\Users\Chay Hawk\Desktop\Guess\main.cpp|50|error: cannot call member function 'void Company::MAINPROGRAM()' without object|
||=== Build finished: 2 errors, 0 warnings ===|
Last edited on
closed account (zb0S216C)
Ch1156 wrote:
Company::NEWGAMESTART();

No, this is not correct, unless Company::NEWGAMESTART() is declared static. Company is a class, not an object. You need to use NewCompany (or any other Company instance) to access Company::NEWGAMESTART().

Wazzak
Hmm i still get errors. Im going to go read more about classes and do a bunch of practice excercizes then maybe i'll understand this better.
its pretty much the same thing as saying:
string hello;
you are trying to call your functions off the type, like string.sort() (can't think of any member functions off the top of my head right now), of which you can't do. you need to call the function from the type name like, hello.sort(); that's what they mean.
Topic archived. No new replies allowed.
Pages: 12