Join 105,772 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,478 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
Good day, new at C++ and trying to get my program to compile; keep getting the following error - "[Linker error] undefined reference to 'computerTurn(int&)' can anybody help me understand. Thanks
do { printf("\nPlayer's turn to roll dice...\n"); playerTurn(pTotal);
if (pTotal >= 100) { printf("\nPlayer wins with a score of %d - life is good!\n", pTotal); return 0; } printf("\nComputer's turn to roll dice...\n"); computerTurn(cTotal);
if (cTotal >= 100) { printf("\nComputer wins with a score of %d - AI is great!\n", cTotal); return 0; } } while (pTotal < 100 || cTotal < 100);
return 0;
}
int playerTurn(int& pTotal) { srand(time(0)); int pDice1, pDice2, playerTurn = 0; char answer;
printf("\n");
do { printf("Player, do you wish to roll the dice or hold and pass (enter r or h)? "); scanf("%c\n"); if (answer == 'r') { pDice1 = (1 + rand() % 6); pDice2 = (1 + rand() % 6); playerTurn = playerTurn + (pDice1 + pDice2);
if (pDice1 == 1 || pDice2 == 1) { playerTurn = 0; printf("Die faces rolled are %d and %d, and player's turn score is: %d.\n", pDice1, pDice2, playerTurn); printf("Player's total score is: %d.\n", pTotal); return pTotal; } else if (pDice1 > 1 && pDice2 > 1) { printf("Die faces rolled are %d and %d, and player's turn score is: %d.\n", pDice1, pDice2, playerTurn); pTotal = pTotal + playerTurn; } } else if (answer == 'h') { printf("Player's total score is: %d.\n", pTotal); return pTotal; if (pTotal >= 100); } } while (pTotal < 100 || cTotal < 100);
return 0;
int computerTurn(int& cTotal); { srand(time(0)); int cDice1, cDice2, computerTurn = 0;
printf("\n"); do { cDice1 = (1 + rand() % 6); cDice2 = (1 + rand() % 6); computerTurn = computerTurn + (cDice1 + cDice2); if (cDice1 == 1 || cDice2 == 1) { computerTurn = 0; printf("Computer rolled %d and %d.", cDice1, cDice2); printf("Computer's turn score is: %d.\n", computerTurn); printf("Computer's total score is: %d.\n", cTotal); return cTotal; } else if (cDice1 > 1 && cDice2 > 1) { printf("Computer rolled %d and %d.", cDice1, cDice2); } } while (cTotal < 100 && computerTurn < 20); printf("Computer's turn score is: %d.\n", computerTurn); cTotal = cTotal + computerTurn; printf("Computer's total score is: %d.\n", cTotal); } system ("PAUSE"); return 0; }
basically when you are defining you computerTurn() you are putting a semi colon after it, you dont put semi colons so just change
int computerTurn(int& cTotal); { // code }
to
int computerTurn(int& cTotal) { // code }
im sure you know what i mean as you have defined playerTurn() correctly apart from you havnt used close curly bracket on the end.
dont worry errors like this can be commonplace when first learning, but now you know that a link error along the lines of "undefined reference to" means you havnt defined a function correctly.
do { printf("\nPlayer's turn to roll dice...\n"); playerTurn(pTotal);
if (pTotal >= 100) { printf("\nPlayer wins with a score of %d - life is good!\n", pTotal); return 0; } printf("\nComputer's turn to roll dice...\n"); computerTurn(cTotal);
if (cTotal >= 100) { printf("\nComputer wins with a score of %d - AI is great!\n", cTotal); return 0; } } while (pTotal < 100 || cTotal < 100);
return 0;
}
int playerTurn(int& pTotal) { srand(time(0)); int pDice1, pDice2, playerTurn = 0; char answer;
printf("\n");
do { printf("Player, do you wish to roll the dice or hold and pass (enter r or h)? "); scanf("%c\n"); if (answer == 'r') { pDice1 = (1 + rand() % 6); pDice2 = (1 + rand() % 6); playerTurn = playerTurn + (pDice1 + pDice2);
if (pDice1 == 1 || pDice2 == 1) { playerTurn = 0; printf("Die faces rolled are %d and %d, and player's turn score is: %d.\n", pDice1, pDice2, playerTurn); printf("Player's total score is: %d.\n", pTotal); return pTotal; } else if (pDice1 > 1 && pDice2 > 1) { printf("Die faces rolled are %d and %d, and player's turn score is: %d.\n", pDice1, pDice2, playerTurn); pTotal = pTotal + playerTurn; } } else if (answer == 'h') { printf("Player's total score is: %d.\n", pTotal); return pTotal; if (pTotal >= 100); } } while (pTotal < 100 || cTotal < 100);
//return 0; }
int computerTurn(int& cTotal); { srand(time(0)); int cDice1, cDice2, computerTurn = 0;
printf("\n"); do { cDice1 = (1 + rand() % 6); cDice2 = (1 + rand() % 6); computerTurn = computerTurn + (cDice1 + cDice2); if (cDice1 == 1 || cDice2 == 1) { computerTurn = 0; printf("Computer rolled %d and %d.", cDice1, cDice2); printf("Computer's turn score is: %d.\n", computerTurn); printf("Computer's total score is: %d.\n", cTotal); return cTotal; } else if (cDice1 > 1 && cDice2 > 1) { printf("Computer rolled %d and %d.", cDice1, cDice2); } } while (cTotal < 100 && computerTurn < 20); printf("Computer's turn score is: %d.\n", computerTurn); cTotal = cTotal + computerTurn; printf("Computer's total score is: %d.\n", cTotal); //} system ("PAUSE"); return 0; }//one bracket is in wrong place^it should go
On line 30 you call playerTurn. Now in your declaration you declare playerTurn with a return type of int. So for this to work you need a variable where you call playerTurn to "catch" the return value like "int i = playerTurn(pTotal);". If you don't want it to return a value then you should declare it void. As for the linking error, your probably receiving it because there was no closing bracket after the return 0 so it's not recognizing the computerTurn declaration.
That said it should get your program up and running wont work but should get you closer ...Almost missed line 94 take out the semicolon.
I would definitely rethink your code here though because you have unnecessary code and some paths in your two turn functions that don't return anything. "A programmers most used tools are pen and paper" so try mapping the game out first.
printf("\nComputer wins with a score of %d - RANDOMNESS is great!\n", cTotal);
basically when you are defining you computerTurn() you are putting a semi colon after it, you dont put semi colons so just change
int computerTurn(int& cTotal); { // code }
to
int computerTurn(int& cTotal) { // code }
im sure you know what i mean as you have defined playerTurn() correctly apart from you havnt used close curly bracket on the end.
dont worry errors like this can be commonplace when first learning, but now you know that a link error along the lines of "undefined reference to" means you havnt defined a function correctly.
do { printf("\nPlayer's turn to roll dice...\n"); playerTurn(pTotal);
if (pTotal >= 100) { printf("\nPlayer wins with a score of %d - life is good!\n", pTotal); return 0; } printf("\nComputer's turn to roll dice...\n"); computerTurn(cTotal);
if (cTotal >= 100) { printf("\nComputer wins with a score of %d - AI is great!\n", cTotal); return 0; } } while (pTotal < 100 || cTotal < 100);
return 0;
}
int playerTurn(int& pTotal) { srand(time(0)); int pDice1, pDice2, playerTurn = 0; char answer;
printf("\n");
do { printf("Player, do you wish to roll the dice or hold and pass (enter r or h)? "); scanf("%c\n"); if (answer == 'r') { pDice1 = (1 + rand() % 6); pDice2 = (1 + rand() % 6); playerTurn = playerTurn + (pDice1 + pDice2);
if (pDice1 == 1 || pDice2 == 1) { playerTurn = 0; printf("Die faces rolled are %d and %d, and player's turn score is: %d.\n", pDice1, pDice2, playerTurn); printf("Player's total score is: %d.\n", pTotal); return pTotal; } else if (pDice1 > 1 && pDice2 > 1) { printf("Die faces rolled are %d and %d, and player's turn score is: %d.\n", pDice1, pDice2, playerTurn); pTotal = pTotal + playerTurn; } } else if (answer == 'h') { printf("Player's total score is: %d.\n", pTotal); return pTotal; if (pTotal >= 100); } } while (pTotal < 100 || cTotal < 100);
//return 0; }
int computerTurn(int& cTotal); { srand(time(0)); int cDice1, cDice2, computerTurn = 0;
printf("\n"); do { cDice1 = (1 + rand() % 6); cDice2 = (1 + rand() % 6); computerTurn = computerTurn + (cDice1 + cDice2); if (cDice1 == 1 || cDice2 == 1) { computerTurn = 0; printf("Computer rolled %d and %d.", cDice1, cDice2); printf("Computer's turn score is: %d.\n", computerTurn); printf("Computer's total score is: %d.\n", cTotal); return cTotal; } else if (cDice1 > 1 && cDice2 > 1) { printf("Computer rolled %d and %d.", cDice1, cDice2); } } while (cTotal < 100 && computerTurn < 20); printf("Computer's turn score is: %d.\n", computerTurn); cTotal = cTotal + computerTurn; printf("Computer's total score is: %d.\n", cTotal); //} system ("PAUSE"); return 0; }//one bracket is in wrong place^it should go
On line 30 you call playerTurn. Now in your declaration you declare playerTurn with a return type of int. So for this to work you need a variable where you call playerTurn to "catch" the return value like "int i = playerTurn(pTotal);". If you don't want it to return a value then you should declare it void. As for the linking error, your probably receiving it because there was no closing bracket after the return 0 so it's not recognizing the computerTurn declaration.
That said it should get your program up and running wont work but should get you closer ...Almost missed line 94 take out the semicolon.
I would definitely rethink your code here though because you have unnecessary code and some paths in your two turn functions that don't return anything. "A programmers most used tools are pen and paper" so try mapping the game out first.
printf("\nComputer wins with a score of %d - RANDOMNESS is great!\n", cTotal);