LNK2019

I am new to C++ and I am trying to solve this error:

Error LNK2019 unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl flipCoin(void)" (?flipCoin@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main

My code is below:

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

string flipCoin();
int rollDice(int);

int main()
{
int numberOfTosses, numberOfSides, rolls, i;
char userChoice = ' ';

unsigned seed = time(0);
srand(seed);

while (userChoice != 'E')
{
cout << "Do you want to flip a coin, roll a die, or exit? ";
}
if (userChoice == 'C')
{
cout << "How many times do you want to flip the coin";
cin >> numberOfTosses;

for (i = 1; i <= userChoice; i++)
{
flipCoin();

cout << "";
}
if (userChoice == 'D')
{
cout << "How many sides does the die have? ";
cin >> numberOfSides;
cout << "How many times do you want to roll the die? ";
cin >> rolls;
for (i = 0; i <= userChoice; i++)
{
rollDice;

cout << "";
}
}
else
{
cout << "Thanks for playing! ";
}

}

string flipCoin();
{
int toss = 1 + rand() % 2;

if (toss == 1)
{
cout << "tails";
}
else
{
cout << "heads";
}
}

int rollDice(int);
{
int roll = 1 + rand() % 2;

if (roll == 1)
{
cout << roll;
}
}


}
This is why it's so important to match your indentation with the actual block structure. The problem is that flipCoin and rollDice are "defined" inside the main() program and they have syntax errors, here's how the compiler interprets the last few lines of your program:
1
2
3
4
5
6
7
8
9
int rollDice(int);   // re-declares function rollDice inside main()
{    // starts a new block of code.
    int roll = 1 + rand() % 2;
    if (roll == 1)
    {
        cout << roll;
    }
}    // ends the block begun at line 2
} // end of main()  


Here is your code properly indented and with some syntax errors fixed. See the comments that begin with "dmh"
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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

string flipCoin();
int rollDice(int);

int
main()
{
    int numberOfTosses, numberOfSides, rolls, i;
    char userChoice = ' ';

    unsigned seed = time(0);
    srand(seed);

    while (userChoice != 'E') {
	cout << "Do you want to flip a coin, roll a die, or exit? ";
    }
    if (userChoice == 'C') {
	cout << "How many times do you want to flip the coin";
	cin >> numberOfTosses;

	for (i = 1; i <= userChoice; i++) {
	    flipCoin();

	    cout << "";
	}
	if (userChoice == 'D') {
	    cout << "How many sides does the die have? ";
	    cin >> numberOfSides;
	    cout << "How many times do you want to roll the die? ";
	    cin >> rolls;
	    for (i = 0; i <= userChoice; i++) {
		rollDice(numberOfSides); // dmh - pass number of sides

		cout << "";
	    }
	} else {
	    cout << "Thanks for playing! ";
	}

    }
}

string
flipCoin()
{
    int toss = 1 + rand() % 2;

    if (toss == 1) {
	cout << "tails";
    } else {
	cout << "heads";
    }
    // dmh - what should flipCoin() return?  If nothing then
    // declare it as void instead of string
}

int
rollDice(int)
{
    int roll = 1 + rand() % 2;

    if (roll == 1) {
	cout << roll;
    }
    // dmh  - same as flipCoin: what should rollDice return?
}

Thank you for that clarification on the indention. I am still working on the return for rollDice

I am working with this pseudocode:

Main Function
Declare the number of tosses, number of sides, and user choice.
Seed the random number generator

while user choice !=E
Ask the user if they want to flip a coin, roll a die, or exit
If the choice is C
Ask the user how many times to flip the coin.
For i=1 to number of flips step 1
Call flipCoin()
Print result
If the choice is D
Ask the user how many sides the die has.
Ask the user how many times to roll the die
For i=1 to number of rolls step 1
Call rollDice()
Print result
Else
Display the closing message


flipCoin Function
generate an integer random number between 1 and 2.
If a 1 is generated return “heads”, else return “tails”
rollDice Function
generate a random number between 1 and the number of sides
return the result of the roll.

Last edited on
I am working with this pseudocode:

A wonderful way to write code is to wriite the pseudocode as comments, then add the actual code under the comments. I've added the pseudocode to your code below. When I did this, several problems became immediately apparent. I've highlighted those with "<<<" in the comments. Try to fix these problems. If you get stuck for more than 30 minutes, post your code and describe the problem.

Good luck!

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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

string flipCoin();
int rollDice(int);

int
main()
{

    // Declare the number of tosses, number of sides, and user choice.
    int numberOfTosses, numberOfSides, rolls, i;
    char userChoice = ' ';

    // Seed the random number generator
    unsigned seed = time(0);
    srand(seed);


    // while user choice !=E
    while (userChoice != 'E') {
	// Ask the user if they want to flip a coin, roll a die, or exit
	cout << "Do you want to flip a coin, roll a die, or exit? ";
    }
    // If the choice is C
    if (userChoice == 'C') {
	// Ask the user how many times to flip the coin.
	cout << "How many times do you want to flip the coin";
	cin >> numberOfTosses;

	// For i=1 to number of flips step 1
	for (i = 1; i <= userChoice; i++) { // <<<< Is that the number of flips?
	    // Call flipCoin()
	    flipCoin();

	    cout << "";		// <<< What's this?
	}
	// Print result <<< You aren't doing this yet
	// If the choice is D  
	if (userChoice == 'D') { // <<< Wait a sec. You're still inside
       				 // the if userChoice == 'C' section!
	    // Ask the user how many sides the die has.
	    cout << "How many sides does the die have? ";
	    cin >> numberOfSides;
	    // Ask the user how many times to roll the die
	    cout << "How many times do you want to roll the die? ";
	    cin >> rolls;
	    // For i=1 to number of rolls step 1
	    for (i = 0; i <= userChoice; i++) { // <<< is that the number of rolls?
		// Call rollDice()
		rollDice(numberOfSides); // dmh - pass number of sides

		cout << "";	// <<< what's this?
	    }
	    // Print result
	    // <<< You aren't printing anything
	} else {
	    // Else
	    // Display the closing message
	    cout << "Thanks for playing! ";
	}

    }
}

// flipCoin Function
string
flipCoin()
{
    // generate an integer random number between 1 and 2.
    int toss = 1 + rand() % 2;

    // If a 1 is generated return “heads”, else return “tails”
    // <<< You aren't returning "heads" or "tails", you're print it.
    // <<< So now we have the answer to "what should you print?"
    // <<< in main: print the return value of each call
    if (toss == 1) {
	cout << "tails";
    } else {
	cout << "heads";
    }
}

// rollDice Function
int
rollDice(int)
{
    // generate a random number between 1 and the number of sides
    // <<< This isn't the number of sides.  You need to name the
    // <<< parameter to rollDice above and then use it here.
    int roll = 1 + rand() % 2;

    // return the result of the roll.
    // <<< You' arenlt returning the result, you're printing it.
    // <<< I think you're supposed to return the value here and print
    // <<< it in main
    if (roll == 1) {
	cout << roll;
    }
}

Thank you for that tip with the pseudocode that helps out a lot. But I am still confused in some areas, I do apologize for my novice behavior. I indented if (userChoice == D) from if (userChoice == C). And I trying to get the rollDice function correct with generating the random number between 1 and the number of sides.

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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

string flipCoin();
int rollDice(int);

int
main()
{

	// Declare the number of tosses, number of sides, and user choice.
	int numberOfTosses, numberOfSides, numberOfRolls, i;
	char userChoice = ' ';

	// Seed the random number generator
	unsigned seed = time(0);
	srand(seed);


	// while user choice !=E
	while (userChoice != 'E') {
		// Ask the user if they want to flip a coin, roll a die, or exit
		cout << "Do you want to flip a coin, roll a die, or exit? ";
	}
	// If the choice is C
	if (userChoice == 'C') {
		// Ask the user how many times to flip the coin.
		cout << "How many times do you want to flip the coin";
		cin >> numberOfTosses;

		// For i=1 to number of flips step 1
		for (i = 1; i <= numberOfTosses; i++) { // <<<< Is that the number of flips?
											// Call flipCoin()
			flipCoin();

			cout << "";		// <<< What's this?
		}
		// Print result <<< You aren't doing this yet
		// If the choice is D  
			if (userChoice == 'D') { // <<< Wait a sec. You're still inside
								 // the if userChoice == 'C' section!
								 // Ask the user how many sides the die has.
				cout << "How many sides does the die have? ";
				cin >> numberOfSides;
				// Ask the user how many times to roll the die
				cout << "How many times do you want to roll the die? ";
				cin >> numberOfRolls;
				// For i=1 to number of rolls step 1
				for (i = 0; i <= numberOfRolls; i++) { // <<< is that the number of rolls?
												// Call rollDice()
					rollDice(numberOfSides); // dmh - pass number of sides
				}
			// Print result
				cout << "";// ddl - I am unsure, I am thinking the flips needs to be printed here 
		}
		else {
			// Else
			// Display the closing message
			cout << "Thanks for playing! ";
		}

	}
}

// flipCoin Function
string
flipCoin()
{
	// generate an integer random number between 1 and 2.
	int toss = 1 + rand() % 2;

	// If a 1 is generated return “heads”, else return “tails”
	// <<< You aren't returning "heads" or "tails", you're print it.
	// <<< So now we have the answer to "what should you print?"
	// <<< in main: print the return value of each call
	if (toss == 1) {
		return "tails";
	}
	else {
		return "heads";
	}
}

// rollDice Function
int
rollDice(int)
{
	// generate a random number between 1 and the number of sides
	// <<< This isn't the number of sides.  You need to name the
	// <<< parameter to rollDice above and then use it here.
	int roll = 1 + numberOfSides;

	// return the result of the roll.
	// <<< You' aren't returning the result, you're printing it.
	// <<< I think you're supposed to return the value here and print
	// <<< it in main
	if (roll == 1) {
		return roll;
	}
}
Last edited on
No apology necessary. Most people asking questions here are novices.

When posting code, please use code tags. Highlight your code and then click the <> button to the right of the edit window. That way the code gets line numbers and syntax highlighting.

You still need to fix the userChoice == 'D' case. Just move the brace that closes if (userChoice == 'C') to right before if (userChoice == 'D')

In rollDice(), you have to name the parameter:
int rollDice(int sides) ...
Then you can use sides inside the body of rollDice. You may need to review parameter passing. Once you have sides, you can compute a random number between 1 and sides. You do this just like you computed a random number between 1 and 2 in flipCoin:
int roll = 1 + rand() % sides;

I'd generate the coin flipping output like this:
1
2
3
4
5
6
7
        // For i=1 to number of flips step 1
        cout << "The flips are";
        for (i = 1; i <= numberOfTosses; i++) {
            // Call flipCoin()
            cout << ' ' << flipCoin();
        }
        cout << '\n';


Use similar code to output the results of rolling the dice.
I really appreciate all of your help and tips. I have the following code below, I am seeing errors for the two functions string flipCoin(); and int rollDice(int numberOfSides); it is telling me I need a ; after I call the function.
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
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

string flipCoin();
int rollDice(int);

int main()
{
	//Declare the number of tosses, number of sides, and user choice.
	int numberOfTosses, numberOfSides, numberOfRolls, i;
	char userChoice = ' ';

	//Seed the random number generator
	unsigned seed = time(0);
	srand(seed);

	//while user choice !=E
	while (userChoice != 'E')
	{
		//Ask the user if they want to flip a coin, roll a die, or exit
		cout << "Do you want to flip a coin (C), roll a die (D), or exit (E): ";
		cin >> userChoice;

		//If the choice is C
		if (userChoice == 'C')
		{
			//Ask the user how many times to flip the coin
			cout << "How many times do you want to flip the coin? ";
			cin >> numberOfTosses;
			//For i=1 to number of flips step 1
			cout << "Flip ";
			for (i = 0; i < numberOfTosses; i++)
			{
				//Call flipCoin()
				cout << ' ' << flipCoin();
			}
			//Print result
			cout << " \n";

			//If the choice is D
			if (userChoice == 'D')
			{
				//Ask the user how many sides the die has
				cout << "How many sides does the die have? ";
				cin >> numberOfSides;
				//Ask the user how many times to roll the die
				cout << "How many times do you want to roll the die? ";
				cin >> numberOfRolls;

				//For i=1 to number of rolls step 1
				cout << "Roll ";
				for (i = 0; i < numberOfRolls; i++)
				{
					cout << ' ' << rollDice(numberOfRolls);
				}
				//Print result
				cout << " \n";
			}
			//Else
			else
			{
				//Display the closing message
				cout << "Thanks for playing! ";
			}

		}
	}
//flipCoin Function
string 
flipCoin()
{
	//generate an integer random number between 1 and 2.
	int toss = 1 + rand() % 2;
	//If a 1 is generated return “heads”, else return “tails”
	if (toss == 1)
	{
		return "tails";
	}
	else
	{
		return "heads";
	}
}
//rollDice Function
int rollDice(int numberOfSides)
{
		//generate a random number between 1 and the number of sides
		int roll = 1 + rand() % numberOfSides;
                return roll;
}

	
}
Last edited on
Thank you for all of your help!!
Last edited on
We're back to the indentation problem. Right now lines 73-94 are inside main. You can't define a function inside another function. To fix this, just move line 97 (the closing brace for main) to line 72.

You also still have the userChoice == 'D' case inside the case for 'C'. That means it will never execute. After all, if userChoice == 'C' then it will never equal 'D'.
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 "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

string flipCoin();
int rollDice(int);

int main()
{
	//Declare the number of tosses, number of sides, and user choice.
	int numberOfTosses, numberOfSides, numberOfRolls, i;
	char userChoice = ' ';

	//Seed the random number generator
	unsigned seed = time(0);
	srand(seed);

	//while user choice !=E
	while (userChoice != 'E')
	{
		//Ask the user if they want to flip a coin, roll a die, or exit
		cout << "Do you want to flip a coin (C), roll a die (D), or exit (E): ";
		cin >> userChoice;


		//If the choice is C
		if (userChoice == 'C')
		{
			//Ask the user how many times to flip the coin
			cout << "How many times do you want to flip the coin: ";
			cin >> numberOfTosses;
			//For i=1 to number of flips step 1
			cout << "Flips: ";
			for (i = 0; i < numberOfTosses; i++)
			{
				//Call flipCoin()
				cout << " " << flipCoin();
			}
			//Print result
			cout << " \n";
		}

		//If the choice is D
		if (userChoice == 'D')
		{
			//Ask the user how many sides the die has
			cout << "How many sides does the die have: ";
			cin >> numberOfSides;
			//Ask the user how many times to roll the die
			cout << "How many times do you want to roll the die: ";
			cin >> numberOfRolls;

			//For i=1 to number of rolls step 1
			cout << "Roll: ";
			for (i = 0; i < numberOfRolls; i++)
			{
				cout << " " << rollDice(numberOfRolls);
			}
			//Print result
			cout << " \n";
		}
		else
		{
			//Display the closing message
			cout << "Thanks for playing! ";
		}

	}
}
	
//flipCoin Function
string flipCoin()
{
	//generate an integer random number between 1 and 2.
	int toss = 1 + rand() % 2;
	//If a 1 is generated return “heads”, else return “tails”
	if (toss == 1)
	{
		return "tails";
	}
	else
	{
		return "heads";
	}
}
//rollDice Function
int rollDice(int numberOfSides)
{
	//generate a random number between 1 and the number of sides
	int roll = 1 + rand() % numberOfSides;
	return roll;


	
}
Last edited on
int rollDice(int numberOfSides)
{
//generate a random number between 1 and the number of sides
int roll = 1 + rand() % numberOfSides;
return roll; // FUNCTION FINISHES HERE


system("pause"); // THIS WILL NEVER HAPPEN
}


Do you understand that when a function returns, it stops? Nothing after the return in the function will execute.
Thank you for that information
Topic archived. No new replies allowed.