Im a bit confused

Pages: 123
I remember asking this a long time ago but i forgot, i made a function that randomly chooses a sentance and the player must type it in exactly but i forget how to do it. I was thinking about using an array somehow but i'll see about that later. line 44 is the problem line.

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
#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);
void random();

struct str
{
    int Generic;
    string name;
    string sences;
};

int main()
{
    str s, n;
    s.Generic = 0;
    n.name;

    cout << "Enter your name" << endl;
    getline(cin, n.name);

    start(&s.Generic, &n.name);
}

void start(int *S, string *N)
{
    string choice;

    cout << "Ok " << *N << " type something in, the longer it is the more\npoints you get." << endl;

    do
    {
        random();
        getline(cin, choice);

        if(choice ==random())
        {
            cout << "Dadas" << 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 << *S << endl;
    file << *N << endl;

    return start(S,N);
}


void random()
{
    int TIME;
    str s1, s2;

    time_t t;
    ctime(&t);
    srand(time(NULL));

    TIME = rand() % 1;

    switch(TIME)
    {
        case 0:
            s1.sences = "The cat is retarded";
            cout << s1.sences << endl;
            break;
        case 1:
            s2.sences = "The dog is retarded";
            cout << s2.sences << endl;
            break;
    }
}




C:\Users\Chay Hawk\Desktop\Pointers\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp|25|warning: statement has no effect|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp||In function 'void start(int*, std::string*)':|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp|44|error: no match for 'operator==' in 'choice == random()'|
||=== Build finished: 1 errors, 1 warnings ===|
Last edited on
random() is void so no value is returned that can be compared with choice.
so what do i do. make it an int? or return random?
Last edited on
Yes, example:
int r = random() % h + a; // an integer between a and a+h

It would be better to seed random(), take a look at this:
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/


EDIT: Ah I see you already did seed random(). If I were you I would choose a different name for your random function.
Last edited on
Just so were clear we arent talking about random numbers but my function random right?
Last edited on
Ok i named it RanF(); so how do i get this working
What do you expect this line to do? if(choice ==random())
Edit: Also, TIME = rand() % 1; TIME will always be zero
Last edited on
well the program is supposed to choose a sentance to output and the player is supposed to type out whatever one the game chose.
See my comments.
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
do
    {
       string val = random();//random needs to return a string
        getline(cin, choice);

        if(choice == val)//so you can compare it to user input
        {
            cout << "Dadas" << endl;
        }
...
string random()
{
    int TIME;
    str s1, s2;//temp objects

    time_t t;
    ctime(&t);
    srand(time(NULL));

    TIME = rand() % 2;//value between 0 and 1 inclusive

    switch(TIME)
    {
        case 0:
            s1.sences = "The cat is retarded";// you set temps, not the objects in main
            //cout << s1.sences << endl;	    
            return s1.sences;
        case 1:
            s2.sences = "The dog is retarded";
            cout << s2.sences << endl;
            return s2.sences;
    }
}
Last edited on
Ok i got these errors:


C:\Users\Chay Hawk\Desktop\Pointers\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp|25|warning: statement has no effect|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp||In function 'void start(int*, std::string*)':|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp|41|error: conversion from 'void' to non-scalar type 'std::string' requested|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp||In function 'void RanF()':|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp|90|error: return-statement with a value, in function returning 'void'|
C:\Users\Chay Hawk\Desktop\Pointers\main.cpp|95|error: return-statement with a value, in function returning 'void'|
||=== Build finished: 3 errors, 1 warnings ===|


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
#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);
void RanF();

struct str
{
    int Generic;
    string name;
    string sences;
};

int main()
{
    str s, n;
    s.Generic = 0;
    n.name;

    cout << "Enter your name" << endl;
    getline(cin, n.name);

    start(&s.Generic, &n.name);
}

void start(int *S, string *N)
{
    string choice;

    cout << "Ok " << *N << " type something in, the longer it is the more\npoints you get." << endl;

    do
    {
        string val = RanF();
        getline(cin, choice);

        if(choice == val)
        {
            *S += 10;
        }

        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 << *S << endl;
    file << *N << endl;

    return start(S,N);
}


void RanF()
{
    int TIME;
    str s1, s2;

    time_t t;
    ctime(&t);
    srand(time(NULL));

    TIME = rand() % 2;

    switch(TIME)
    {
        case 0:
            s1.sences = "The cat is retarded";
            //cout << s1.sences << endl;
            return s1.sences;

        case 1:
            s2.sences = "The dog is retarded";
            //cout << s2.sences << endl;
            return s2.sences;
    }
}


Do i need to put something in the Parameters?
Ok i got it i changes the function to a string.
ok first why line 25? when an object is declared, it is declared with the objects so you can use it anywhere in the scope of that object. its as useless as an if with no test

line 41: you are still trying to return void objects. if you want to have it equal something then it needs to return something

and you are trying to return values in ranf. that doesnt work becuase u have it returning data type void
I already fixed all that.

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
#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();

struct str
{
    int Generic;
    string name;
    string sences;
};


int main()
{
    str s, n;
    s.Generic = 0;

    cout << "Enter your name" << endl;
    getline(cin, n.name);

    start(&s.Generic, &n.name);
}


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;

    return start(S,N);
}


string RanF()
{
    int TIME;
    str s1, s2, s3, s4, s5, s6;

    time_t t;
    ctime(&t);
    srand(time(NULL));

    TIME = rand() % 6;

    switch(TIME)
    {
        case 0:
            s1.sences = "Grand Theft Auto San Andreas";
            return s1.sences;
        case 1:
            s2.sences = "GTA IV";
            return s2.sences;
        case 2:
            s3.sences = "Dead Island";
            return s3.sences;
        case 3:
            s4.sences = "Minecraft";
            return s4.sences;
        case 4:
            s5.sences = "Borderlands";
            return s5.sences;
        case 5:
            s6.sences = "Crackdown 2";
            return s6.sences;
    }
}
Not entirely:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;

    return start(S,N); //function is of type void, it shouldn't return any data
}


Also:

1
2
3
4
5
6
7
8
9
10
11
12
int main()  //main is always of data type int
{
    str s, n;
    s.Generic = 0;

    cout << "Enter your name" << endl;
    getline(cin, n.name);

    start(&s.Generic, &n.name);

    return 0;  //so it should return an int (usually 0 for successful execution)
}


All the best,
NwN
I see, well i have another problem. 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
133
134
135
136
137
138
#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);

struct str
{
    int Generic;
    string name;
    string sences;
    string choice;
};


int main()
{
    str s, n, c;
    s.Generic = 0;

    cout << "1) New" << endl;
    cout << "2) Load" << endl;
    cin >> c.choice;

    if(c.choice == "1")
    {
        cout << "Enter your name" << endl;
        getline(cin, n.name);
        cin.get();
        start(&s.Generic, &n.name);
    }
    else if(c.choice == "2")
    {
        load(&s.Generic, &n.name);
    }
}


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;
    str s1, s2, s3, s4, s5, s6;

    time_t t;
    ctime(&t);
    srand(time(NULL));

    TIME = rand() % 6;

    switch(TIME)
    {
        case 0:
            s1.sences = "Grand Theft Auto San Andreas";
            return s1.sences;
        case 1:
            s2.sences = "GTA IV";
            return s2.sences;
        case 2:
            s3.sences = "Dead Island";
            return s3.sences;
        case 3:
            s4.sences = "Minecraft";
            return s4.sences;
        case 4:
            s5.sences = "Borderlands";
            return s5.sences;
        case 5:
            s6.sences = "Crackdown 2";
            return s6.sences;
        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?
Last edited on
Hi there,

I would suggest rethinking your program to not use those structs.
Your aren't using structs as they are supposed to be used, in your case you could just use plain string variables.
As an example, you store choice in struct c, and the name in struct n, why not just have int choice and string n?

Structs are used for data that is related, kind of like classes (in fact they are almost identical to classes), but usually people don't put any functions in them.

For instance:

1
2
3
4
5
6
7
8
struct sentence {
    string sentence;
    int points;
};

sentence s1;
s1.sentence = "Correct usage of structs";
s1.points = 456984;


The sentence and its points are related, so they can be stored in one struct.
Using them as you do is just unnecessarily cluttering your code.

Just try and clean that up - perhaps when your code is cleaned up you will find the problem by yourself.

All the best,
NwN
Is this better:

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
133
134
135
136
137
138
139
#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);

struct str
{
    int Generic;
};


int main()
{
    str s;
    s.Generic = 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(&s.Generic, &name);
    }
    else if(choice == "2")
    {
        load(&s.Generic, &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;
    }
}
Getting there :)

Why is int Generic still in a struct?
Having a single variable in a struct makes even less sense - using a simple int is enough.

Is there a particular reason you are using pointers in void save(int *S, string *N) (and load())?
If you want to pass the variables by reference, which would more than suffice for this use, you should do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void save(int& S, string& N) //passing by reference
{
    ofstream file;
    file.open("file.txt", ios::app);

    time_t current = time(0);

    file << ctime(&current) << endl;
    file << "";
    file << "Score: " << S << endl;  //regular S and N can be used now
    file << "Name: " << N << endl;
    file << "" << endl;

    file.close();
}

//call it like this (without the &):
save(s, n);


I would stay away from pointers until you fully understand them - they are very useful, but can put you through debugging jungles if you use them the wrong way.

All the best,
NwN
Idk about the Pass by reference part, why would i do that? I want to keep working with pointers and just keep helping me with them, thats the only way i'll ever get it.I know that the * is pointing to the memory address of whatever its pointing to, and & is just a reference. But why do i need to use it?
Last edited on
&(reference operator) used in a function is to pass a variable by reference, and also used to assign a variable to a pointer eg:
1
2
int x;
int* ptr=&x; //ptr is pointing to the address of x 


and *(dereference operator) is used to declare pointers, and it also means "value pointed by..." eg:
1
2
3
4
int x=4;
int* ptr=&x;

cout<<"X= "<<*ptr; //this will display the value pointed by ptr which is 4 


I highly recommend you to read about pointers in this website's tutorials:
http://cplusplus.com/doc/tutorial/pointers/
Last edited on
Pages: 123