Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!



dice game

 
Reply to this topicStart new topic

dice game, C++ programming

smartwater
post 18 Jul, 2008 - 05:09 PM
Post #1


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 2

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

CODE

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;

int playerTurn(int&);
int computerTurn(int&);
int pTotal = 0;
int cTotal = 0;

int main()
{


int pTotal = 0;
int cTotal = 0;

printf("\n");
printf("***************************\n");
printf("*                         *\n");
printf("*            Project      *\n");
printf("*                         *\n");
printf("***************************\n\n");

  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;
}

User is offlineProfile CardPM

Go to the top of the page


stayscrisp
post 18 Jul, 2008 - 07:44 PM
Post #2


D.I.C Head

**
Joined: 14 Feb, 2008
Posts: 181



Thanked 4 times
My Contributions



uh oh your gonna kick yourself for this :s

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.

happy coding smile.gif


User is online!Profile CardPM

Go to the top of the page

F!st!cuffs
post 18 Jul, 2008 - 08:00 PM
Post #3


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 28



Thanked 1 times
My Contributions


cpp

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;

int playerTurn(int&);
int computerTurn(int&);
int pTotal = 0;
int cTotal = 0;

int main()
{


int pTotal = 0;
int cTotal = 0;

printf("\n");
printf("***************************\n");
printf("* *\n");
printf("* Project *\n");
printf("* *\n");
printf("***************************\n\n");

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 smile.gif wont work but should get you closer smile.gif ...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);
User is offlineProfile CardPM

Go to the top of the page

smartwater
post 19 Jul, 2008 - 12:02 PM
Post #4


New D.I.C Head

*
Joined: 18 Jul, 2008
Posts: 2

Thank you for the help - much appreciated.

QUOTE(stayscrisp @ 18 Jul, 2008 - 07:44 PM) *

uh oh your gonna kick yourself for this :s

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.

happy coding smile.gif



Thanks you for your help - much appreciated.

QUOTE(F!st!cuffs @ 18 Jul, 2008 - 08:00 PM) *

cpp

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;

int playerTurn(int&);
int computerTurn(int&);
int pTotal = 0;
int cTotal = 0;

int main()
{


int pTotal = 0;
int cTotal = 0;

printf("\n");
printf("***************************\n");
printf("* *\n");
printf("* Project *\n");
printf("* *\n");
printf("***************************\n\n");

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 smile.gif wont work but should get you closer smile.gif ...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);

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/21/08 03:34PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month