Need help with syntax of functions

Basically I am writing a cootie game for a c++ class and I have the basic idea down but I keep getting a wide array of errors every time I try to fix it pertaining to my parameters.

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
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

void game (int diceValue, int body, int head, int ant, int eyes, int tongue, int legs);
int rollDice();
bool checkForWin(int body, int head, int ant, int eyes, int tongue, int legs);


int main(int argc, char *argv[])
{
    srand(time(0));
    int body = 0, int head = 0, int ant = 0, int eyes = 0, int tongue = 0, int legs = 0;
    int diceValue;

    while (!checkForWin(int &body, int &head, int &ant, int &eyes, int &tongue, int &legs))
    {
        diceValue = rollDice();
        int game(int diceValue, int &body, int &head, int &ant, int &eyes, int &tongue, int &legs);
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

int rollDice() {
     cout << "Rolling Dice" << endl;
     int dice = rand() % 6 + 1;
     cout << "You rolled a: " << dice << endl;
     return dice;
}

void game (int &body=0, int &head=0, int &ant=0, int &eyes=0, int &tongue=0, int &legs=0, int diceValue) {

    // Getting a body
    if (diceValue == 1 && body < 1 ) {
        cout << "You got a body!! " << endl;
        body++;
    }
    else {
        cout << "You already have a body..." << endl;
    }
    // Getting a head
    if (diceValue == 2 && body == 1) {
        cout << "You got a head!!! " << endl;
        head++;
    }
    else {
        cout << "You must get a body first! " << endl;
    }
    // Getting antannae
    if (diceValue == 3 && head == 1) {
        cout << "You got an Antannae" << endl;
    }
    else {
        cout << "You can't get an Antannae. You have no place to put it!";
    }
}

bool checkForWin(int &body, int &head, int &ant, int &eyes, int &tongue, int &legs) {
            // return true if you win
            // return false otherwise
    if (int &body=1 && int &head=1 && int &ant=1 && int &eyes=1 && int &tongue=1 && int &legs=6){
        return true;
    }
        else {
            return false;
        }
}


Try reading over this and see if it helps you understand how they work:
http://cplusplus.com/doc/tutorial/functions/

Post again if you still don't understand it and we'll see if we can help.
I do have a basic understanding how they work, but I feel my syntax is off, especially when using the pass by reference operator. I am pretty sure my logic is set up correctly, but unfortunately I cant even get the thing to debug.

Specifically these two functions

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
void game (int &body=0, int &head=0, int &ant=0, int &eyes=0, int &tongue=0, int &legs=0, int diceValue) {

    // Getting a body
    if (diceValue == 1 && body < 1 ) {
        cout << "You got a body!! " << endl;
        body++;
    }
    else {
        cout << "You already have a body..." << endl;
    }
    // Getting a head
    if (diceValue == 2 && body == 1) {
        cout << "You got a head!!! " << endl;
        head++;
    }
    else {
        cout << "You must get a body first! " << endl;
    }
    // Getting antannae
    if (diceValue == 3 && head == 1) {
        cout << "You got an Antannae" << endl;
    }
    else {
        cout << "You can't get an Antannae. You have no place to put it!";
    }
}

bool checkForWin(int &body, int &head, int &ant, int &eyes, int &tongue, int &legs) {
            // return true if you win
            // return false otherwise
    if (int &body=1 && int &head=1 && int &ant=1 && int &eyes=1 && int &tongue=1 && int &legs=6){
        return true;
    }
        else {
            return false;
        }
}
Last edited on
I may be wrong, but I don't think you can assign default values to references.
@hanst99: I'm pretty sure you are right.

@TC: When calling functions, you need to pass existing variables as parameters; on lines 18 and 21, for example, you are not calling the functions properly. Types should not be on parameters when you are calling the function. What you are doing is really equivalent to something like:
1
2
3
4
5
6
7
8
int add(int a, int b) {
    return a+b;
}

int main() {
    add(int 1, int 2); // <-- why would you put int there?
    return 0;
}


Your if statement on line 31 of your more recent post is also incorrect. When comparing variables, you don't put in type information.
if(int a == 1) { // <-- incorrect if(a == 1) { //<-- correct
Additionally, you want to use == rather than =, as = is for assignment, not comparison.
Ok you guys have been awesome. I have cleaned up some but I have one more error I cant seem to get around.

At line 18 it says that all the variables in the function are are not declared in the scope. I do not understand why this is happening

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
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

void game (int diceValue, int body, int head, int ant, int eyes, int tongue, int legs);
int rollDice();
bool checkForWin(int body, int head, int ant, int eyes, int tongue, int legs);


int main(int argc, char *argv[])
{
    srand(time(0));
    int body = 0, int head = 0, int ant = 0, int eyes = 0, int tongue = 0, int legs = 0;
    int diceValue;

    while (!checkForWin(body, head, ant, eyes, tongue, legs))
    {
        diceValue = rollDice();
        int game(int diceValue, int body, int head, int ant, int eyes, int tongue, int legs);
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

int rollDice() {
     cout << "Rolling Dice" << endl;
     int dice = rand() % 6 + 1;
     cout << "You rolled a: " << dice << endl;
     return dice;
}

void game (int diceValue, int body, int head, int ant, int eyes, int tongue, int legs) {

    // Getting a body
    if (diceValue == 1 && body < 1 ) {
        cout << "You got a body!! " << endl;
        body++;
    }
    else {
        cout << "You already have a body..." << endl;
    }
    // Getting a head
    if (diceValue == 2 && body == 1) {
        cout << "You got a head!!! " << endl;
        head++;
    }
    else {
        cout << "You must get a body first! " << endl;
    }
    // Getting antannae
    if (diceValue == 3 && head == 1) {
        cout << "You got an Antannae" << endl;
    }
    else {
        cout << "You can't get an Antannae. You have no place to put it!";
    }
}

bool checkForWin(int body, int head, int ant, int eyes, int tongue, int legs) {
            // return true if you win
            // return false otherwise
    if (body==1 && head==1 && ant==1 && eyes==1 && tongue==1 && legs==6){
        return true;
    }
        else {
            return false;
        }
}
You know, on line 15, you don't need all those ints. Just one at the beginning will do. :)

-Albatross
Got it to debug thanks everyone!
Topic archived. No new replies allowed.