Function Definition

I'm new to computer science and while writing this code I keep having the same error message at line 102-106 where it says function definition is not allowed here.

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

using namespace std;

const int ROCK          = 1;
const int PAPER         = 2;
const int SCISSORS      = 3;
const int QUIT      = 4;

int getUserChoice();
int getComputerChoice();
void determineOutcome(int, int);
void displayChoice(int);

int main()
{
    int userChoice;
    int computerChoice;
    computerChoice = getComputerChoice();
    userChoice = getUserChoice();
    while (userChoice != QUIT)
    {
        cout << "\nYou selected: ";
        displayChoice(userChoice);
        cout << "The computer selected: ";
        displayChoice(computerChoice);
        determineOutcome(userChoice, computerChoice);
        cout<<endl;
        computerChoice = getComputerChoice();
        userChoice = getUserChoice();
        
    }
    system("PAUSE");
    return 0;
    }
    int getComputerChoice()
    {
        srand(time(NULL));
        return rand() %3 + 1;
    }
    int getUserChoice()
    {
        int user_Choice;
        cout << "Game Menu " << endl;
        cout << "-----" << endl;
        cout << "1) ROCK" << endl;
        cout << "2) PAPER" << endl;
        cout << "3) SCISSORS" << endl;
        cout << "4) Quit" << endl;
        cout << "Enter your choice ";

        cin >> user_Choice;

            while (user_Choice != ROCK && user_Choice != PAPER && user_Choice != SCISSORS)
                cout << "Error. Enter 1, 2, 3, 4 ";
    }

    void determineOutcome(int user, int computer)
    {
        if (user != ROCK && computer != ROCK)
        {
            cout << "The Game is a Tie!" << endl;
    
        if (user != ROCK && computer != PAPER)
        {
            cout << "You Lose!" << endl;
        }
        if (user != ROCK && computer != SCISSORS)
        {
            cout << "The winner is You!" << endl;
        } 

        if (user != PAPER && computer != PAPER)
        {
            cout << "The Game is a tie" << endl;
        }
        if (user != PAPER && computer != ROCK)
        {
            cout << "The Winner is you!" << endl;
        }
        if (user != PAPER && computer != SCISSORS)
        {
            cout << "You lose!" << endl;
        } 

        if(user != SCISSORS && computer != SCISSORS)
        {
            cout << "The Game is a tie" << endl;
        }
        if (user != SCISSORS && computer != PAPER)
        {
            cout << "The Winner is you!" << endl;
        }
        if (user != SCISSORS && computer != ROCK)
        {
            cout << "You lose!" << endl;
        }
    }
  void displayChoice(int choice)
    {
      cout << "You: " << userChoice << endl;
      cout << "Computer: " << ComputerChoice << endl;
   
    }
You are missing an extra } after line 100. Without it you are trying to define displayChoice() inside the body of determineOutcome().

Your formatting kinda hides the problem.

Once that is corrected you still have two errors in displayChoice().

Neither userChoice or ComputerChoice are defined in the function.

Did you intend the passed choice variable to be named userChoice?

You should declare displayChoice() to take two variables, as you did with determineOutcome()
I fixed the problem and made displayChoice() to take two variables
1
2
3
4
5
6
7
 
  void displayChoice(int user, int computer)
    {
      cout << "You: " << user << endl;
      cout << "Computer: " << computer << endl;
   
    }


but after doing that I get a problem in line 58 where it states:
"warning: control reaches end of non-void function [-Wreturn-type]
}"
and the code wont run

Your function
int getUserChoice()
promises to return an integer, but doesn't return anything.

I should think you need
return user_Choice;
BTW, another fix you should make, after fixing getUserChoice().

Do NOT call srand() in getComputerChoice(). That resets the random number generator every time you call the function. Remove that line and move it up near the top of your main function, so it gets called only one time at the start of your program.

Personally I would avoid using the C library -- <cstdlib & <ctime> -- for generating random numbers, I'd use C++ <random> and <chrono>.

https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Topic archived. No new replies allowed.