Can someone help me it wont compile

December 6, 2010 at 16:31:30
Specs: Windows 7
/***********************************************************************
Program: Functions.cpp
Date: 12/08/2010
Purpose: This program will simulate the game of paper-rock-scissors. The computer will
play against a human user of the program.
***********************************************************************/
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <fstream>
using namespace std;

void readAndValidateUserMove (char);
int computerChoice(int ,char);
void gameStatus(int, int, int );
/**********************************************************************/
int main()
{
int numberOfAttempts;
char userChoice;
int i =0;
int results=0;

cout<<"\n\t\tEnter the Number of Times You Would Like To Play: "<<endl;
cout<<"\n\t\tGame Choices: Best Of 1"<<
cout<<"\n\t\t\tBest Of 3"<<
cout<<"\n\t\tBest Of 5"<<
cout<<"\n\t\tEnter the Number Of One of the choices above: 1-3-5";
cin>>numberOfAttempts;

// ofstream outFile;
// outFile.open (results"prsresults.txt");
// outFile.close(results"prsresults.txt")


cout<<"\n\t\tGood-Bye. Thank You For Playing"<<endl;

return 0;
}
/**************************************************************************/
void readAndValidateUserMove(char &userChoice)
{ do
{
cout<<"\t\t\tEnter P-Paper, R-Rock, and S-Scissors: ";
cin>>userChoice;

userChoice =toupper(userChoice);

}while((userChoice!='P'||userChoice!='p')&&(userChoice!='R'||userChoice!='r')&&(userChoice!='S'||userChoice!='s'));
return ;
}
/*************************************************************************/
int computerChoice(int randomComputerChoice, char randomChoice)
{
srand(time( 0));
randomComputerChoice= (rand()%3)+1;
if(randomComputerChoice==0)
{
randomChoice='P';
}
else if (randomComputerChoice==1)
{
randomChoice='R';
}
else if (randomComputerChoice==2)
{
randomChoice='S';
}

return ;
}
/*************************************************************************/
char outcome(char userChoice, char randomChoice)
{
if (userChoice==randomChoice)
{
cout<<" \n\t\tIts A Tie"<<endl;
}
switch (userChoice)
{
case 'P':
result=(randomChoice=='R');
cout<<"\n\t\tPaper beats Rock"<<endl;
break;
case 'R':
result=(randomChoice=='S');
cout<<"\n\t\tRock beats Scissors"<<endl;
break;
case 'S'
result=(randomChoice=='P');
cout<<"\n\t\tScissors beats Paper"<<endl;
break;
default: cout<<"\n\t\tYou've Entered A NON-Valid Move"<<endl;
}

return ;
}


See More: Can someone help me it wont compile

Report •

#1
December 6, 2010 at 23:18:26
You've created functions, but you aren't using them that I can tell. Where are the calls in main?

with toupper / tolower, there's no need in testing both cases.

outFile.open (results"prsresults.txt");
not sure what you're doing here, but lose results in the call.
outFile.close();

numberOfAttempts should be validated and then used

readAndValidateUserMove (char); the prototype is pass by value while the definition is pass by reference - make sure they match; it's declared as a void function, but tries to return a value.

int computerChoice(int randomComputerChoice, char randomChoice) wants to return an int, but you haven't decided what to return; srand should be in main. randomChoice is a local copy.

char outcome(char userChoice, char randomChoice)
result isn't declared within the function, and it doesn't do anything with its assigment; the function wants to return a char.

outcome has a definition, but no prototype; you may want an if/else path
gameStatus has a prototype, but no definition.


Report •

#2
December 8, 2010 at 14:35:42
**It will do the first function but thats it it wont do the other two that i need can someone tell me what is wrong with it


#include <iostream>
#include <cstdlib>//allows the user to use functions
#include <cctype>//allows the user to use the toupper tool
#include <ctime>//print random numbers
#include <fstream>//print to the outFile
using namespace std;

void readAndValidateUserMove (char);//fuction fo the user first choice
int computerChoice(int );//function for the computers choice
int outcome(char , char );//function to compare the results
/**********************************************************************/
int main()
{
int numberOfAttempts; //allows the user to input the number of times they want to attempt the game
char userChoice;//the users choice
int randomComputerChoice=0;//the computers random number
char randomChoice;//the random number into a character
int i =0;//for the loop for number of attempts
int human=0;//the wins for the human
int computer=0;//the wins for the computer
char playAgain;

ofstream outFile;//to print the results to an out file
outFile.open("C:\\prsresults.txt");//outputs data to the file that is in the ()
playAgain='Y';
while(playAgain=='Y')
{
cout<<"\n\t\tEnter the Number of Times You Would Like To Play: "<<endl;
cout<<"\n\t\t\tGame Choices: \n\t\t\t**Best Of 1";
cout<<"\n\t\t\t**Best Of 3";
cout<<"\n\t\t\t**Best Of 5";
cout<<"\n\n\t\tEnter the Number Of One of the choices above: 1-3-5:\t";
cin>>numberOfAttempts;

readAndValidateUserMove(userChoice);

randomChoice= computerChoice(randomComputerChoice);

human= outcome(userChoice,randomChoice);

cout<<"\n\t\tHuman: "<<human<<" wins"<<"\tComputer: "<<computer<<" wins"<<endl;
cout<<"\n\t\tDo you want to play again: Y or N"<<" ";
cin>>playAgain;
}

outFile<<"Human Wins: "<<human<<" Computer Wins: "<<computer<<endl;//prints to the outfile
outFile.close();
cout<<"\n\t\tGood-Bye. Thank You For Playing"<<endl;//Ends the game overall

return 0;
}
/**************************************************************************/
void readAndValidateUserMove(char userChoice)
{ do
{
cout<<"\n\t\t\tEnter P-Paper, R-Rock, and S-Scissors: ";
cin>>userChoice;
userChoice=toupper(userChoice);

}while((userChoice!='P'&&userChoice!='p')&&(userChoice!='R'&&userChoice!='r')&&(userChoice!='S'&&userChoice!='s'));
return ;
}
/*************************************************************************/
int computerChoice(int randomComputerChoice)
{
char randomChoice;
srand(time( 0));

randomComputerChoice= (rand()%3);

if(randomComputerChoice==0)
{
randomChoice='P';
}
else if (randomComputerChoice==1)
{
randomChoice='R';
}
else if (randomComputerChoice==2)
{
randomChoice='S';
}

return randomChoice;
}
/*************************************************************************/
int outcome(char userChoice, char randomChoice)
{
int human;
int computer;

human=0;
computer=0;

if (userChoice==randomChoice)
{
cout<<" \n\t\tIts A Tie"<<endl;
}
if(userChoice=='P')
{
human++;
cout<<"\n\t\tPaper beats Rock"<<endl;
}
else if (userChoice=='R')
{
human++;
cout<<"\n\t\tRock beats Scissors"<<endl;
}
else if (userChoice=='S')
{
human++;
cout<<"\n\t\tScissors beats Paper"<<endl;
}
else
{
computer++;
}

return human ;
}


Report •

#3
December 8, 2010 at 22:20:47
You seem to be missing an inner loop that controls the number of games attempted. I don't really know what you're needing to do, so I'll try to give you an idea of what you could do.

char readAndValidate(void); // prototype
void computeGame(int [], const char, const char); // prototype

... // other bits of code in main
srand((unsigned)time(NULL)); // needs to be in main

while(toupper(playAgain) == 'Y') {

   // initialize whatever per game variables here
   int ngames = 0, outcome[3] = { 0 }; // ties, wins array

   // prompt user for number of games to play
   // read in and validate numberOfAttempts

   while(ngames < numberOfAttempts) {

      char pmove = readAndValidateMove(); // get players's move
      char cmove = "RPS"[rand() % 3];     // get computer's move
      computeGame(outcome, cmove, pmove); // who won or tie?
      ++ngames;
   }

   // show results of attempted games
   // prompt user to play again
}

void computeGame(int res[], const char comp, const char play) {

   // parameters: res[0] - ties, res[1] - comp wins, res[2] - player wins
   //             comp - R, P, or S   play - R, P, or S
   // compare moves; for example

   switch(play) {

      case 'R' : if(comp == 'R') { ++res[0]; } else
                 if(comp == 'P') { ++res[1]; } else
                 if(comp == 'S') { ++res[2]; } break;

      ... // other cases
   }
}

If you want or need to write a function for the computer's move, then you could use something similar to the assignment...

char computerMove(void); // prototype
char computerMove(void) { return "RPS"[rand() % 3]; }


For your readAndValidate function, it may be eaiser to follow if you just return a value.

char readAndValidate(void); // prototype
char readAndValidate(void) {
   char tmp;
   do {
      // prompt for move
      // read in tmp
   } while(tmp is not a valid move);

   return tmp;
}

HTH


Report •
Related Solutions


Ask Question