I\'m having troubles running this program, why can\'t I get this c++ code to run
ID: 3859859 • Letter: I
Question
I'm having troubles running this program, why can't I get this c++ code to run? I'm getting 60 error messages.
#include "stdafx.h"
#include
#include
#include
using namespace std;
// functions prototypes
void displayGameDetails();
void playGame();
string getComputerChoice();
string getUserChoice();
string getChoiceAsString(int);
void displayChoices(string, string);
string findWinner(string, string);
// start main function
int main()
{
Call the displayGameDetails() function to display the rules of the game.
// get the computer choice for the game
displayGameDetails();
Call the playGame() function to find and display the winner of Rock, Paper, Scissors game.
// find and display the winner of the game
playGame();
// pause the system for a while
system("pause");
return 0;
} // end of main function
// displayGameDetails function defintion
void displayGameDetails()
{
// display the game details
cout << " Rock, Paper, Scissors Game " << endl;
cout << "------------------------------" << endl;
cout << "Rules to decide a winner:" << endl;
cout << "Rock can smash Scissor. Here, the player who chooses Rock is the winner." << endl;
cout << "Paper can wrap Rock. Here, the player who chooses Paper is the winner." << endl;
cout << "Scissor can cut Paper. Here, the player who chooses Scissor is the winner." << endl;
cout << "Both players choose the same choice. Here, both players must play the game again." << endl;
} // end of displayGameDetails function
// playGame function definition
void playGame()
{
// local variables
string computerChoice;
string userChoice;
string gameWinner;
Call the computerChoice() function to get the computer choice for the game.
// get the computer choice for the game
computerChoice = getComputerChoice();
Call the getUserChoice() function to get the user choice for the game.
// get the user choice for the game
userChoice = getUserChoice();
Call the displayChoices() function to display the computer choice and the user choice for the game.
/* display the computer choice and the user choice for the game */
displayChoices(computerChoice, userChoice);
Call the findWinner() function to get the winner of the game.
/* get the winner of the game */
gameWinner = findWinner(computerChoice, userChoice);
Display the winner of the Rock, Paper, and Scissors game.
// display the winner of the game
if (gameWinner != "")
cout << "The winner of this game is " << gameWinner << endl;
} // end of playGame function
// getComputerChoice function definition
string getComputerChoice()
{
// local variables
int comp;
string cmpChoice;
// get the current time in the system
unsigned seedTime = time(0);
/* call the predefined srand function to generate a different random number */
srand(seedTime);
// generate a random number as the computer choice
comp = 1 + rand() % 3;
// get the computer choice as string
cmpChoice = getChoiceAsString(comp);
// return the computer choice
return cmpChoice;
} // end of getComputerChoice function
// getUserChoice function definition
string getUserChoice()
{
// local variables
int usr;
string usrChoice;
// display the menu for the user
cout << " The computer has chosen one choice." << endl;
cout << " Now the user has to choose one of the following." << endl;
cout << "1. Rock" << endl;
cout << "2. Paper" << endl;
cout << "3. Scissor" << endl;
// prompt the user to enter his or her choice
cout << "Enter the user choice: ";
cin >> usr;
// verify whether the choice is 1 or 2 or 3
while (usr != 1 && usr != 2 && usr != 3)
{
// prompt the user to enter his or her choice
cout << "Enter 1 or 2 or 3 only: ";
cin >> usr;
} // end while
// get the user choice as string
usrChoice = getChoiceAsString(usr);
// return the user choice
return usrChoice;
} // end of getUserChoice function
// getChoiceAsString function definition
string getChoiceAsString(int choice)
{
// return the corresponding name of the choice
if (choice == 1)
return "Rock";
else if (choice == 2)
return "Paper";
else // choice == 3
return "Scissor";
} // end of getChoiceAsString function
// displayChoices function definition
void displayChoices(string comp, string usr)
{
// display the computer choice
cout << " The computer choice: " << comp << endl;
// display the user choice
cout << "The user choice: " << usr << endl;
} // end of displayChoices function
// findWinner function definition
string findWinner(string comp, string usr)
{
// local variables
string computer = "Computer";
string user = "User";
string winner;
if (comp == "Rock" && usr == "Scissor")
winner = computer;
else if (comp == "Rock" && usr == "Paper")
winner = user;
else if (comp == "Paper" && usr == "Rock")
winner = computer;
else if (comp == "Paper" && usr == "Scissor")
winner = user;
else if (comp == "Scissor" && usr == "Paper")
winner = computer;
else if (comp == "Scissor" && usr == "Rock")
winner = user;
else
{
// display the message as game drawn
cout << " Game is drawn." << endl;
cout << "Play again" << endl;
cout << "-----------" << endl;
// call the playGame function to play again
playGame();
}
// return the winner of the game
return winner;
} // end of findWinner function
Explanation / Answer
Hi
I have fixed the issues. Issues here are with comments. We did not comment them properly. Becuase of that we are getting compilation errors that i have fixed.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void displayGameDetails();
void playGame();
string getComputerChoice();
string getUserChoice();
string getChoiceAsString(int);
void displayChoices(string, string);
string findWinner(string, string);
// start main function
int main()
{
// Call the displayGameDetails() function to display the rules of the game.
// get the computer choice for the game
displayGameDetails();
// Call the playGame() function to find and display the winner of Rock, Paper, Scissors game.
// find and display the winner of the game
playGame();
// pause the system for a while
system("pause");
return 0;
} // end of main function
// displayGameDetails function defintion
void displayGameDetails()
{
// display the game details
cout << " Rock, Paper, Scissors Game " << endl;
cout << "------------------------------" << endl;
cout << "Rules to decide a winner:" << endl;
cout << "Rock can smash Scissor. Here, the player who chooses Rock is the winner." << endl;
cout << "Paper can wrap Rock. Here, the player who chooses Paper is the winner." << endl;
cout << "Scissor can cut Paper. Here, the player who chooses Scissor is the winner." << endl;
cout << "Both players choose the same choice. Here, both players must play the game again." << endl;
} // end of displayGameDetails function
// playGame function definition
void playGame()
{
// local variables
string computerChoice;
string userChoice;
string gameWinner;
// Call the computerChoice() function to get the computer choice for the game.
// get the computer choice for the game
computerChoice = getComputerChoice();
// Call the getUserChoice() function to get the user choice for the game.
// get the user choice for the game
userChoice = getUserChoice();
//Call the displayChoices() function to display the computer choice and the user choice for the game.
/* display the computer choice and the user choice for the game */
displayChoices(computerChoice, userChoice);
//Call the findWinner() function to get the winner of the game.
/* get the winner of the game */
gameWinner = findWinner(computerChoice, userChoice);
//Display the winner of the Rock, Paper, and Scissors game.
// display the winner of the game
if (gameWinner != "")
cout << "The winner of this game is " << gameWinner << endl;
} // end of playGame function
// getComputerChoice function definition
string getComputerChoice()
{
// local variables
int comp;
string cmpChoice;
// get the current time in the system
unsigned seedTime = time(0);
/* call the predefined srand function to generate a different random number */
srand(seedTime);
// generate a random number as the computer choice
comp = 1 + rand() % 3;
// get the computer choice as string
cmpChoice = getChoiceAsString(comp);
// return the computer choice
return cmpChoice;
} // end of getComputerChoice function
// getUserChoice function definition
string getUserChoice()
{
// local variables
int usr;
string usrChoice;
// display the menu for the user
cout << " The computer has chosen one choice." << endl;
cout << " Now the user has to choose one of the following." << endl;
cout << "1. Rock" << endl;
cout << "2. Paper" << endl;
cout << "3. Scissor" << endl;
// prompt the user to enter his or her choice
cout << "Enter the user choice: ";
cin >> usr;
// verify whether the choice is 1 or 2 or 3
while (usr != 1 && usr != 2 && usr != 3)
{
// prompt the user to enter his or her choice
cout << "Enter 1 or 2 or 3 only: ";
cin >> usr;
} // end while
// get the user choice as string
usrChoice = getChoiceAsString(usr);
// return the user choice
return usrChoice;
} // end of getUserChoice function
// getChoiceAsString function definition
string getChoiceAsString(int choice)
{
// return the corresponding name of the choice
if (choice == 1)
return "Rock";
else if (choice == 2)
return "Paper";
else // choice == 3
return "Scissor";
} // end of getChoiceAsString function
// displayChoices function definition
void displayChoices(string comp, string usr)
{
// display the computer choice
cout << " The computer choice: " << comp << endl;
// display the user choice
cout << "The user choice: " << usr << endl;
} // end of displayChoices function
// findWinner function definition
string findWinner(string comp, string usr)
{
// local variables
string computer = "Computer";
string user = "User";
string winner;
if (comp == "Rock" && usr == "Scissor")
winner = computer;
else if (comp == "Rock" && usr == "Paper")
winner = user;
else if (comp == "Paper" && usr == "Rock")
winner = computer;
else if (comp == "Paper" && usr == "Scissor")
winner = user;
else if (comp == "Scissor" && usr == "Paper")
winner = computer;
else if (comp == "Scissor" && usr == "Rock")
winner = user;
else
{
// display the message as game drawn
cout << " Game is drawn." << endl;
cout << "Play again" << endl;
cout << "-----------" << endl;
// call the playGame function to play again
playGame();
}
// return the winner of the game
return winner;
} // end of findWinner function
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Rock, Paper, Scissors Game
------------------------------
Rules to decide a winner:
Rock can smash Scissor. Here, the player who chooses Rock is the winner.
Paper can wrap Rock. Here, the player who chooses Paper is the winner.
Scissor can cut Paper. Here, the player who chooses Scissor is the winner.
Both players choose the same choice. Here, both players must play the game again.
The computer has chosen one choice.
Now the user has to choose one of the following.
1. Rock
2. Paper
3. Scissor
Enter the user choice: 1
The computer choice: Rock
The user choice: Rock
Game is drawn.
Play again
-----------
The computer has chosen one choice.
Now the user has to choose one of the following.
1. Rock
2. Paper
3. Scissor
Enter the user choice: 2
The computer choice: Rock
The user choice: Paper
The winner of this game is User
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.