First Time trying to create Functions - Crazy Error Messages

Hey guys. I just started learning about functions in my class recently. I've been getting some really strange error messages when I try to compile. I can't even decipher what it's saying really. If someone could please point me in the right direction, I'd really appreciate it.

The assignment is to rewrite our Rock, Paper, Scissors game by incorporating some Functions my professor listed. I'm sure there's a lot wrong with it, but I've truly done my best to research it and figure it out on my own. Thanks again.

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
  //Prototype declarations
int getPlayerTurn ();
int doComputerTurn ();
void showOutcome (int, int);

// The main function
int main () {
int choice = getPlayerTurn();
int comp = doComputerTurn();
showOutcome(choice, comp);
            }


//
// getPlayerTurn
//    Reads in the players choice and displays what they chose.
//
// Parameters:
//    There are no parameters for this function.
// Returns:
//    Returns the players choice.
//
int getPlayerTurn (){
   int choice;
   cout << "************ Rock, Paper, Scissors ************ \n";
   cout << "Enter 1 for Rock, 2 for Paper, 3 for Scissors: ";
   cin >> choice;
   cout << "You chose ";
       if (choice == 1)
        cout << "Rock. \n";
   else if (choice == 2)
        cout << "Paper. \n";
   else if (choice == 3)
        cout << "Scissors. \n";
   return (choice);
                    }
// doComputerTurn
//    Calculates the computer turn and displays what it chose.
//
// Parameters:
//    There are no parameters for this function.
//
// Returns:
//    Returns the computers choice.
//
int doComputerTurn(){
   int comp;
   comp = rand() % 3 + 1;
   cout << "The computer chose ";
   if (comp == 1)
        cout << "Rock. \n";
   else if (comp == 2)
        cout << "Paper. \n";
   else if (comp == 3)
        cout << "Scissors. \n";
   return (comp);
                    }
//
// showOutcome
//    Calculates and displays the winner of the current game as well as the current score.
//
// Parameters:
//    choice - The game selection made by the player.
//    comp - The game selection made by the computer.
//
// Returns:
//    This function does not have a return value.
//
void showOutcome (int choice, int comp){
int lose=0, win=0,tie=0;
   if (choice == comp){
        cout << "It's a tie! \n";
        tie++;
                      }
   else if ((choice == 1)&&(comp == 2)){
        cout << "Paper beats Rock, you lose. \n";
        lose++;
                                      }
   else if ((choice == 1)&&(comp == 3)){
        cout << "Rock beats Scissors, you win! \n" ;
        win++;
                                      }
   else if ((choice == 2)&&(comp == 1)){
        cout << "Paper beats Rock, you win! \n" ;
        win++;
                                      }
   else if ((choice == 2)&&(comp == 3)){
        cout << "Scissors beats Paper, you lose. \n";
        lose++;
                                      }
   else if ((choice == 3)&&(comp == 1)){
        cout << "Rock beats Scissors, you lose. \n";
        lose++;
                                      }
   else if ((choice == 3)&&(comp == 2)){
        cout << "Scissors beats Paper, you win! \n";
        win++;
                                   }
cout << "Wins: " << win << "   Ties: " << tie << "   Losses: " << lose << endl;
                                        }
Last edited on
> I've been getting some really strange error messages
Consider to post them.
@Frenzy, Please don't double post, it's ultimately a time waster for those who reply.

http://www.cplusplus.com/forum/general/185377/
closed account (Ey6Cko23)
you didn't include any libraries but you were calling them.
cout, cin and endl aren't in c++.
to get them you need to write #include <iostream> at the start of the code.
if you're using visual studio to write the program you also need to add this #include "stdafx.h" .

the corrected version of your program would be:
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
#include <iostream>

//Prototype declarations
int getPlayerTurn();
int doComputerTurn();
void showOutcome(int, int);

// The main function
int main() {
    int choice = getPlayerTurn();
    int comp = doComputerTurn();
    showOutcome(choice, comp);
}


//
// getPlayerTurn
//    Reads in the players choice and displays what they chose.
//
// Parameters:
//    There are no parameters for this function.
// Returns:
//    Returns the players choice.
//
int getPlayerTurn() {
    int choice;
    std::cout << "************ Rock, Paper, Scissors ************ \n";
    std::cout << "Enter 1 for Rock, 2 for Paper, 3 for Scissors: ";
    std::cin >> choice;
    std::cout << "You chose ";
    if (choice == 1)
        std::cout << "Rock. \n";
    else if (choice == 2)
        std::cout << "Paper. \n";
    else if (choice == 3)
        std::cout << "Scissors. \n";
    return (choice);
}
// doComputerTurn
//    Calculates the computer turn and displays what it chose.
//
// Parameters:
//    There are no parameters for this function.
//
// Returns:
//    Returns the computers choice.
//
int doComputerTurn() {
    int comp;
    comp = rand() % 3 + 1;
    std::cout << "The computer chose ";
    if (comp == 1)
        std::cout << "Rock. \n";
    else if (comp == 2)
        std::cout << "Paper. \n";
    else if (comp == 3)
        std::cout << "Scissors. \n";
    return (comp);
}
//
// showOutcome
//    Calculates and displays the winner of the current game as well as the current score.
//
// Parameters:
//    choice - The game selection made by the player.
//    comp - The game selection made by the computer.
//
// Returns:
//    This function does not have a return value.
//
void showOutcome(int choice, int comp) {
    int lose = 0, win = 0, tie = 0;
    if (choice == comp) {
        std::cout << "It's a tie! \n";
        tie++;
    }
    else if ((choice == 1) && (comp == 2)) {
        std::cout << "Paper beats Rock, you lose. \n";
        lose++;
    }
    else if ((choice == 1) && (comp == 3)) {
        std::cout << "Rock beats Scissors, you win! \n";
        win++;
    }
    else if ((choice == 2) && (comp == 1)) {
        std::cout << "Paper beats Rock, you win! \n";
        win++;
    }
    else if ((choice == 2) && (comp == 3)) {
        std::cout << "Scissors beats Paper, you lose. \n";
        lose++;
    }
    else if ((choice == 3) && (comp == 1)) {
        std::cout << "Rock beats Scissors, you lose. \n";
        lose++;
    }
    else if ((choice == 3) && (comp == 2)) {
        std::cout << "Scissors beats Paper, you win! \n";
        win++;
    }
    std::cout << "Wins: " << win << "   Ties: " << tie << "   Losses: " << lose << std::endl;
}


I actually had all of my #Include files and "Using namespace std" at the top, I just missed it when I was copying and pasting.

Does anyone know why this might be compiling for everyone else who tries but not for me?

Not sure if this makes a difference but my teacher taught us to compile like this...

g++ Filename.cpp -o Filename
Last edited on
So I just tried compiling this morning and it worked like magic... No clue what I changed to make it work either.

Thanks everyone
Not sure if this makes a difference but my teacher taught us to compile like this...
g++ Filename.cpp -o Filename



Try this, much better:

g++ -std=c++14 -Wall -Wextra -pedantic-errors Filename.cpp -o Filename 


Use -std=c++11 if you don't have c++14
Topic archived. No new replies allowed.