So I'm writing a small Rock, Paper, Scissors game structure in C++ and I've run into some errors I don't understand.
Solution
The function string numberToWord (int x) can't be in the function main. It has to be a separate method due to the way the compiler works. I simply moved it out then it worked fine.
Previous Question
So I'm writing a small Rock, Paper, Scissors game structure in C++ and I've run into some errors I don't understand.
The first is the code expects a ';' at the NumberToWord function but it shouldn't since it's a function.
Another error is randomly one of the else statements it doesn't seem to like.
Maybe I'm missing something, I don't know but it should be a simple fix.
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
int seed = static_cast <int> (time(0)); //Sets the random seed
srand(seed);
int winCount = 0;
string numberToWord (int x) {
string outputChoice;
if (x == 0) { outputChoice = "Rock"; }
else if (x == 1) { outputChoice = "Paper"; }
else if (x == 2) { outputChoice = "Scissors"; }
return outputChoice;
}
while (winCount < 3) {
int computerChoice = rand() % 4;
int userChoice;
cout << userChoice << endl;
cout << "Please Enter 0 for Rock, 1 for Paper, or 2 for Scissors: "; //Asks for user input
cin >> userChoice; //Inputs user input to variable
if (userChoice == computerChoice) {
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Draw!" << endl;
}
else if ((userChoice == 1) && (computerChoice == 2)) { //Rock v Paper
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 1) && (computerChoice == 3)) { //Rock v Scissors
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
else if ((userChoice == 2) && (computerChoice == 1)) { //Paper v Rock
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
else if ((userChoice == 2) && (computerChoice == 3)) { //Paper v Scissors
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 3) && (computerChoice == 1)) { //Scissors v Rock
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "Compuer wins!" << endl;
}
else if ((userChoice == 3) && (computerChoice == 2)) { //Scissors v Paper
cout << "Compuer Choose: " << numberToWord(computerChoice) << endl;
cout << "You Choose: " << numberToWord(userChoice) << endl;
cout << "You win!" << endl;
winCount += 1;
}
}
return 0;
}
Thanks for any and all help!
Part 2
Simply put the program doesn't like the '<<'. I use this just fine in many other programs for variables but this time when I used a string variable it throws an error. I looked up C++ string variables and it looks like I'm doing it correctly so I don't know the reason for the errors.
References:
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/variables/
void displayOutput(int comp, int user, string winner) {
string compOutputChoice = "";
string userOutputChoice = "";
/*
if (comp == 0) { compOutputChoice = "Rock"; }
else if (comp == 1) { compOutputChoice = "Paper"; }
else if (comp == 2) { compOutputChoice = "Scissors"; }
if (user == 0) { userOutputChoice = "Rock"; }
else if (user == 1) { userOutputChoice = "Paper"; }
else if (user == 2) { userOutputChoice = "Scissors"; }
*/
cout << "Compuer Choose: " << compOutputChoice << endl;
cout << "You Choose: " << userOutputChoice << endl;
//cout << winner << endl;
return;
}
Errors:
Error (active) no operator "<<" 32
Error (active) no operator "<<" 33
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 32
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 33
The function string numberToWord (int x) is nested inside the main function. That is not valid C++.
The GCC compiler does support nested functions as an extension, but it's not part of the standard and other compilers (that I know of) don't accept it. Just don't do that. Move the function out of main (or, if it makes sense, make it a lambda).
The problem is simple. numberToWord cannot be an internal function of main. Move it outside main or change it to a lambda if you are using a newer C++.
auto numberToWord = [](int x) -> string {
string outputChoice;
if (x == 0) { outputChoice = "Rock"; }
else if (x == 1) { outputChoice = "Paper"; }
else if (x == 2) { outputChoice = "Scissors"; }
return outputChoice;
};
Related
I'm making a Dice Game in C++. I was wondering why it doesn't restart the loop. The game is Best of 3. It's supposed to restart the loop as long as the player wants to keep playing. However it only restarts the loop once. The second time I press 'Y' or yes in this case it just exits the program.
I've tried putting the restart in a nested while loop but it doesn't seem to work either.
restart:
while ((pWin != 2) && (cWin != 2))
{
pDice1 = rand() % 6 + 1;
cDice1 = rand() % 6 + 1;
cout << "Player score is: " << pDice1 << endl;
cout << "Computer score is: " << cDice1 << endl;
if (cDice1 > pDice1) {
cout << "Computer wins!" << endl << endl;
cWin++;
} if (pDice1 > cDice1) {
cout << "Player wins!" << endl << endl;
pWin++;
} if (pDice1 == cDice1) {
cout << "It's a draw!" << endl << endl;
} if (pWin > cWin) {
cout << "Player wins this round! Do you wish to keep playing?" << endl;
cin >> Y;
if (Y == 'y') {
goto restart;
}
else {
exit(0);
}
}if (cWin > pWin) {
cout << "Computer wins this round! Do you wish to keep playing?" << endl;
cin >> Y;
if (Y == 'y') {
goto restart;
}
else {
exit(0);
}
}
}
First, is this all your code? I noticed most of your variables seem to be declared outside of the provided code block. If so, is your "Y" being declared as a char and not a string type to match your condition type?
It looks like you are failing to set your pWin and cWin back to zero when it returns to the top. You can fix with:
restart:
cWin = 0;
pWin = 0;
while ((pWin != 2) && (cWin != 2))
{
pDice1 = rand() % 6 + 1;
cDice1 = rand() % 6 + 1;
cout << "Player score is: " << pDice1 << endl;
cout << "Computer score is: " << cDice1 << endl;
if (cDice1 > pDice1) {
cout << "Computer wins!" << endl << endl;
cWin++;
} if (pDice1 > cDice1) {
cout << "Player wins!" << endl << endl;
pWin++;
} if (pDice1 == cDice1) {
cout << "It's a draw!" << endl << endl;
} if (pWin > cWin) {
cout << "Player wins this round! Do you wish to keep playing?" << endl;
cin >> Y;
if (Y == 'y') {
goto restart;
}
else {
exit(0);
}
}if (cWin > pWin) {
cout << "Computer wins this round! Do you wish to keep playing?" << endl;
cin >> Y;
if (Y == 'y') {
goto restart;
}
else {
exit(0);
}
}
}
Because you don't reset pWin and cWin to zero after a game.
You should fix that but also turn the goto into another while loop and make the guts of that while loop into a function maybe.
I've been working on my introduction to C++ project which is to make a game of NIM. I've written program and I thought it should run smoothly but I've countered the following errors.
I know too much code but I couldn't figure out what is the issue that is stopping me from running the program.
I'm new to the programming word so be understanding if the issue is very simple.
thank you in advance.
Error Message
#include <iostream>
#include <iomanip>
using namespace std;
int stonesNum;
char goFirst;
int turn = 0; // 0 = player, 1 = computer
int stonesRemove;
int main() {
do
{
cout << "Enter number of starting stones:" << endl;
cin >> stonesNum;
cout << "Would you like to go first? (y/n)" << endl;
cin >> goFirst;
if (goFirst == 'y' || goFirst == 'Y')
{
turn = 0;
}
else
{
turn = 1;
}
while (stonesNum > 0)
{
if (turn == 0)
{
cout << "How many would you like to remove: 1 or 2?" << endl;
cin >> stonesRemove;
stonesNum = stonesNum - 1;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
if (stonesNum % 3 == 0)
{
stonesNum = stonesNum - 2;
cout << "The computer removes 2 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
stonesNum = stonesNum - 1;
cout << "The computer removes 1 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
}
if (stonesNum == 0)
{
if (turn == 0)
{
cout << "The computer wins!" << endl;
}
else
{
cout << "you won!" << endl;
}
}
}
}
return 0;
}
You are missing an ending while condition after your do loop. The syntax is :
do{
//do stuff here
}while(condition);
In your case the condition would be (stonesNum!=0)
OR
Do
if (stonesNum == 0)
{
if (turn == 0)
{
cout << "The computer wins!" << endl;
return;
}
else
{
cout << "you won!" << endl;
return;
}
}
and put condition (true)
You actually don't require the do loop at all, are you trying to loop the game endlessly? If so then you should put an appropriate prompt at the beginning and put condition (true) in the do loop.
The complete code:
do
{
cout << "Enter number of starting stones:" << endl;
cin >> stonesNum;
cout << "Would you like to go first? (y/n)" << endl;
cin >> goFirst;
if (goFirst == 'y' || goFirst == 'Y')
{
turn = 0;
}
else
{
turn = 1;
}
while (stonesNum > 0)
{
if (turn == 0)
{
cout << "How many would you like to remove: 1 or 2?" << endl;
cin >> stonesRemove;
stonesNum = stonesNum - 1;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
if (stonesNum % 3 == 0)
{
stonesNum = stonesNum - 2;
cout << "The computer removes 2 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
stonesNum = stonesNum - 1;
cout << "The computer removes 1 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
}
}
if (turn == 0)
{
cout << "The computer wins!" << endl;
}
else
{
cout << "you won!" << endl;
}
}while(true);
I have written a code that displays a rock-paper-scissors game against the computer. I would like to add a feature where I can create a text file in order to store the person's score and the computer score and keep track of the score but I don't know how to do it. Thank you in advance!
Here is my code.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void rock_paper_scissors()
{
static int userscore = 0;
static int computerscore = 0;
string playername;
int userchoice;
int computerchoice;
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
cout << "Please enter your choice\n";
cin >> userchoice;
cout << endl;
while (!(userchoice > 0 && userchoice <= 3))
{
cout << "invalid choice. please enter a number between 1 and 3\n";
cin >> userchoice;
}
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
}
else
{
cout << "computer wins!" << endl;
}
cout << "thank you for playing!\n";
string restart;
cout << "Would you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
if (restart == "y")
{
rock_paper_scissors();
}
}
int main()
{
cout << "MAIN\n";
rock_paper_scissors();
return 0;
}
Why do you need to write the data into in file? Updating a simple txt file after each round could be cumbersome, as you always result in a single score at the end of the program. I would suggest changing the type of the rock_paper_scissors into int indicating the score achieved by the player in a single lot. The intermediate results are irrelevant. Just place the game loop into your main function and do not use a recursive function call and static function variables here. Otherwise, the player is forced to enter his name for every single lot.
Moreover, I tested your code and you have to change your error handling. I typed in "rock" and the program stuck in an infinity loop "invalid choice. please enter a number between 1 and 3\n" Whereby it is not possible to make another entry. As I entered a string instead of an integer, you have to reset the console. Beware of the dumbest possible user.
Moreover, you should seed your program to avoid identical computer choices in each game. This could be done with srand(time(NULL)).
Eventually, I write the tracked score into a score file at the end of the main function using the fstream standard library.
#include <iostream>
#include <string>
#include <algorithm> // min max
#include <fstream> // read/write from/to files
#include <time.h> // time
using namespace std;
int rock_paper_scissors(const std::string& playername);
int main()
{
srand(time(NULL));
cout << "MAIN\n";
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
int userscore = 0;
int computerscore = 0;
std::string playername;
std::string restart;
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
do
{
int result = rock_paper_scissors(playername);
cout << "thank you for playing!\n";
userscore += result;
computerscore += std::max(0, 3 - 2 * result);
cout << playername << "'s score: " << userscore;
cout << "\ncomputer's score: " << computerscore;
cout << "\nWould you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
} while (restart == "y");
std::ofstream ofile;
ofile.open("scorefile.txt");
ofile << "Scores:\n" << playername << ": " << userscore;
ofile << "\nComputer: " << computerscore;
ofile.close();
return 0;
}
int rock_paper_scissors(const std::string& playername)
{
int userchoice;
int computerchoice;
cout << endl << endl;
do {
cout << "Please enter your choice\n";
if (std::cin >> userchoice)
{
if (userchoice > 0 && userchoice <= 3)
{
break;
}
else
{
cout << "invalid choice. please enter a number between 1 and 3\n";
continue;
}
}
else if (!cin.bad() && !cin.eof())
{
// a non integer value entered
cerr << "invalid choice. please enter a number between 1 and 3\n";
// Reset error state
cin.clear();
// remove error input
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
} while (true);
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
return 1;
}
else
{
cout << "computer wins!" << endl;
return 0;
}
}
So I have an issue with my code where I cannot get either of my functions (int human_turn, or int computer_turn) to return the correct value of their turn totals. What I am trying to do is have the two int functions return their respective turn totals after they are done with their turn, then have a separate function that will cout these values and will act as the scoreboard and also show if the win conditions are met. Currently at the end of the while loop for each ones turn, I have turnTotal_1 = roll_1 + turnTotal_1; which updates the total and then the loop checks to see if the while is still true. I then put a return turnTotal_1; statement after the loop so it would return this value, and it will always return 1. No matter what the value of the turn total is, the return statement will always return 1. any help or advice would be appreciated.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
using namespace std;
/**
* rollDie
* returns a random integer between 1 and 6, works as rolling a dice.
* return value, int number (1-6)
*/
int rollDie()
{
return random() % 6 + 1;
}
int human_turn(int turnTotal_1)
{
cout << "It is now human's turn" << endl;
cout << "Do you want to roll a dice(Y/N)?:" << endl;
char user_choice;
cin >> user_choice;
while ((user_choice == 'y') || (user_choice == 'Y'))
{
int roll_1 = rollDie();
if ((roll_1 == 2) || (roll_1 == 5))
{
cout << "You rolled a " << roll_1 << endl;
roll_1 = 0;
}
else if ((roll_1 == 1) || (roll_1== 3) ||(roll_1 == 6))
{
cout << "You rolled a " << roll_1 << endl;
}
else if (roll_1 == 4)
{
cout << "You rolled a " << roll_1 << endl;
roll_1 = 15;
}
turnTotal_1 = roll_1 + turnTotal_1;
cout << "Your turn total is " << turnTotal_1 << endl;
cout << "Do you want to roll a dice(Y/N)?:" << endl;
cin >> user_choice;
}
if ((user_choice == 'n') || (user_choice == 'N'))
{
return turnTotal_1;
}
}
int computer_turn()
{
int turnTotal_2 = 0;
cout << "It is now computer's turn" << endl;
int roll_1 = rollDie();
while (turnTotal_2 <= 10)
{
int roll_1 = rollDie();
if ((roll_1 == 2) || (roll_1 == 5))
{
cout << "Computer rolled a " << roll_1 << endl;
roll_1 = 0;
}
else if ((roll_1 == 1) || (roll_1== 3) ||(roll_1 == 6))
{
cout << "Computer rolled a " << roll_1 << endl;
}
else if (roll_1 == 4)
{
cout << "Computer rolled a " << roll_1 << endl;
roll_1 = 15;
}
turnTotal_2 = roll_1 + turnTotal_2;
cout << "Computer turn total is " << turnTotal_2 << endl;
}
}
void scoreboard()
{
cout << human_turn << endl;
cout << computer_turn << endl;
}
void game()
{
cout << "Welcome to Jeopardy Dice!" << endl;
human_turn(0);
cout << human_turn << endl;;
computer_turn();
scoreboard();
}
int main()
{
// start the game!
game();
return 0;
}
Here is an example of how it runs.
Welcome to Jeopardy Dice!
It is now human's turn
Do you want to roll a dice(Y/N)?:
n
1
It is now computer's turn
Computer rolled a 5
Computer turn total is 0
Computer rolled a 4
Computer turn total is 15
I am stuck on something I know should be really simple to fix but I just can't figure it out I've been Google-ing and gone through my text (Gaddis C++ Intro, Ch. 6) and tried a few things - mostly moving the loop around as I thought I placed it wrong; I've searched through several C++ forums as well and I have found examples where you ask the userj "do you want to continue/play again" but that is not what I am to do, I am to have it automatically restart if there is a tie. It keeps coming back to the variables not being defined/declared for use within the loop -- except I don't see how they aren't. I'm using VS 2010.
Here's the code. I appreciate any help. I feel kinda stupid for not being able to fix this myself. This is my first programming class and I'm also taking visual basic at the same time. It's been interesting.
The debug errors I'm getting are both C2065 "undeclared identifier" for both cpuChoice and userChoice.
[code]
#include <iostream>
#include <ctime>
using namespace std;
void outputChoice(int c)
{
switch(c)
{
case 1:
cout << "Rock";
break;
case 2:
cout << "Paper";
break;
case 3:
cout << "Scissors";
break;
case 4:
cout << "Lizard";
break;
case 5:
cout << "Spock";
break;
}
}
//bool for determining if win or if draw -- loss will be elseif
bool isWin(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == 1 && cpuChoice == 3) ||
(userChoice == 1 && cpuChoice == 4) ||
(userChoice == 2 && cpuChoice == 1) ||
(userChoice == 2 && cpuChoice == 5) ||
(userChoice == 3 && cpuChoice == 2) ||
(userChoice == 3 && cpuChoice == 4));
return result;
}
bool isDraw(int userChoice, int cpuChoice)
{
bool result =
( (userChoice == cpuChoice));
return result;
}
int main()
{
do{
srand(time(NULL));
cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard";
cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
cout << endl;
{
int userChoice;
cout << "Please choose your move. Select 1-5: \n\n";
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "4) Lizard" << endl;
cout << "5) Spock\n\n" << endl;
cin >> userChoice;
if (!(userChoice >= 1 && userChoice <= 5))
{
cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
}
else
{
int cpuChoice = rand() % 5 + 1;
cout << "You chose... ";
outputChoice(userChoice);
cout << endl;
cout << "The computer chose... ";
outputChoice(cpuChoice);
cout << endl;
cout << endl;
cout << "The result is..." << endl;
}
if (isWin(userChoice, cpuChoice))
{
cout << "You chose wisely! WINNER!!!!!" << endl;
}
else if (isDraw(userChoice, cpuChoice))
{
cout << "You chose well, but so did I - TIE!" << endl;
}
else
{
cout << "You chose poorly! You loose!" << endl;
}
}
while (userChoice == cpuChoice);
return 0;
}
[/code]
You problem is variable scope. change the first lines inside main():
int main()
{
int userChoice, cpuChoice;
do {
Then inside, instead of declaring these variables, just assign a value:
int cpuChoice = rand() % 5 + 1;
should be
cpuChoice = rand() % 5 + 1;
And get rid of the other declaration of userChoice altogether.
That should do it.
You declared these variables int the wrong scope. Move both declarations before the loop
int main()
{
int userChoice;
int cpuChoice;
do{
srand(time(NULL));
cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard";
cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
cout << endl;
{
cout << "Please choose your move. Select 1-5: \n\n";
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "4) Lizard" << endl;
cout << "5) Spock\n\n" << endl;
cin >> userChoice;
if (!(userChoice >= 1 && userChoice <= 5))
{
cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
}
else
{
cpuChoice = rand() % 5 + 1;
cout << "You chose... ";
outputChoice(userChoice);
cout << endl;
cout << "The computer chose... ";
outputChoice(cpuChoice);
cout << endl;
cout << endl;
cout << "The result is..." << endl;
}
if (isWin(userChoice, cpuChoice))
{
cout << "You chose wisely! WINNER!!!!!" << endl;
}
else if (isDraw(userChoice, cpuChoice))
{
cout << "You chose well, but so did I - TIE!" << endl;
}
else
{
cout << "You chose poorly! You loose!" << endl;
}
}
while (userChoice == cpuChoice);
return 0;
}