After finding a text-based rock paper scissors app I wrote, I decided to improve the code. Now, I want to write in for the console to clear the screen before returning to the main menu, and before executing a requested app operation. Here is the code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include <algorithm>
using namespace std;
void scoreCounter(int playerWins, int playerTies, int playerLosses, int compWins, int compTies, int compLosses)
{
cout << "PLAYER SCORE:\n\n";
cout << "Wins: " << playerWins << "\n";
cout << "Losses: " << playerLosses << "\n";
cout << "Ties: " << playerTies << "\n\n\n";
cout << "COMPUTER SCORE:\n\n";
cout << "Wins: " << compWins << "\n";
cout << "Losses: " << compLosses << "\n";
cout << "Ties: " << compTies << "\n\n\n";
}
void mainMenu()
{
cout << "Main Menu\n\n";
cout << "1) Play Rock Paper Scissors\n";
cout << "2) Display scoreboard\n";
cout << "3) Exit app\n";
cout << "\n";
}
void rockPaperScissorsShoot(int playerWins, int playerTies, int playerLosses, int compWins, int compTies, int compLosses) {
cout << "Rock Paper Scissors Shoot!\n";
string playerChoice;
cin >> playerChoice;
transform(playerChoice.begin(), playerChoice.end(), playerChoice.begin(), ::tolower);
int n1;
int n2;
int n3;
srand(time(NULL));
if (playerChoice == "Rock" || playerChoice == "rock") {
n1 = rand() %3;
if (n1 == 0) {
cout << "You win!\n";
playerWins++;
compLosses++;
}
else if (n1 == 1) {
cout << "You lose!\n";
playerLosses++;
compWins++;
}
else if (n1 == 2) {
cout << "You tied!\n";
playerTies++;
compTies++;
}
}
else if (playerChoice == "Paper" || playerChoice == "paper") {
n2 = rand() %3;
if (n2 == 0) {
cout << "You win!\n";
playerWins++;
compLosses++;
}
else if (n2 == 1) {
cout << "You lose!\n";
playerLosses++;
compWins++;
}
else if (n2 == 2) {
cout << "You tied!\n";
playerTies++;
compTies++;
}
}
else if (playerChoice == "Scissors" || playerChoice == "scissors") {
n3 = rand() %3;
if (n3 == 0) {
cout << "You win!\n";
playerWins++;
compLosses++;
}
else if (n3 == 1) {
cout << "You lose!\n";
playerLosses++;
compWins++;
}
else if (n3 == 2) {
cout << "You tied!\n";
playerTies++;
compTies++;
}
}
else {
cout << "You made an invalid choice. Ending game...\n";
}
}
int main()
{
bool gameOver = 0;
bool exitProg = 0;
int playerWins = 0;
int playerTies = 0;
int playerLosses = 0;
int compWins = 0;
int compTies = 0;
int compLosses = 0;
int menuChoice;
while (exitProg != 1)
{
mainMenu();
cin >> menuChoice;
switch (menuChoice)
{
case 1:
{
while (gameOver == 0) {
rockPaperScissorsShoot(playerWins, playerTies, playerLosses, compWins, compTies, compLosses);
cout << "Would you like to play again? Press Y or N:\n";
char yOrN;
cin >> yOrN;
switch (yOrN) {
case 'Y': {
cout << "Ok then!\n";
break;
}
case 'y': {
cout << "Ok then!\n";
break;
}
case 'N': {
cout << "Game over.\n";
gameOver = 1;
break;
}
case 'n': {
cout << "Game over.\n";
gameOver = 1;
break;
}
}
}
cout << "\n";
break;
}
case 2:
{
scoreCounter(playerWins, playerTies, playerLosses, compWins, compTies, compLosses);
break;
}
case 3:
{
exitProg = 1;
system("PAUSE");
break;
}
}
}
return 0;
}
I considered using cin.clear(), but I don't think that will help me if I want to open a different function. What can I do?
For anyone who finds this question, I use system("CLS"); to clear the console before loading a different operation.
Related
The issue I am having is that I am storing values into my vectors and concatenating them into one bundle of vectors. However, when I go back into the class where I stored the values, they are gone. VS then throws up a generic error. Please find my code below.
#include <vector>
#include <conio.h>
#include <cctype>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
void Train_Time_Entries();
struct
{
int iOption, iCount = 0;
int myContNo = -1;
int fDecide = -1;
string AdminUser, AdminPass;
vector <string> AdminUsers{ "John1966", "Christine55", "BlakeBennett74", "SarahHarding93", "Connor1987" };
vector <string> AdminPasswords{ "KalEL", "HenryDog6", "WesternRailway21", "Railwayssince89", "Budweisersaregood" };
vector <string> sAdmins;
string sLocation, sArrive, sDepart, sDay;
bool bAdmin = true, bAddEnt = true, bMenu = true, bMenu1 = true;
}iop, admus, admpa, AdminUsers, AdminPas, bAdm, sLoc, sArr, sDep, bAdd, myCo,fde, bMe, bMen, sAds,sDa,iCou;
//****************Admin Only********************
class Train_Time_Ent
{
public:
vector<string> sLocations;
vector<string> sDay;
vector<string> sArrive;
vector<string> sDepart;
vector<string> sBundle;
};
//*****************Admin Only*******************
void Train_Time_Entries()
{
system("cls");
Train_Time_Ent myobjs;
do
{
cout << " Train Entries(Admin View Only)\n";
for (int i = 0; i < 5; i++)
{
cin.ignore();
cout << "Please enter a train location: " << flush;
getline(cin, sLoc.sLocation);
myobjs.sLocations.push_back(sLoc.sLocation + "\n");
cout << "Enter a day of the week for times: " << flush;
cin >> sDa.sDay;
myobjs.sDay.push_back(sDa.sDay + "\n");
do
{
cout << "Now enter a Train Arrive time for this location: " << flush;
cin >> sArr.sArrive;
myobjs.sArrive.push_back(sArr.sArrive + "\n");
cout << "Would you like to add any more arrival times? Y=1 N=2: " << flush;
cin >> iop.iOption;
if (iop.iOption == 2)
{
cout << "Okay Thank you\n";
break;
}
} while (iop.iOption == 1);
do
{
cout << "Now enter a Train Departure time for this location: " << flush;
cin >> sDep.sDepart;
myobjs.sDepart.push_back(sDep.sDepart + "\n");
cout << "Would you like to enter more departure times? Y=1 N=2: " << flush;
cin >> iop.iOption;
if (iop.iOption == 2)
{
cout << "Okay Thank you\n";
break;
}
} while (iop.iOption == 1);
cout << "Would you like to make any other entries? Y = 1 N = 2: " << flush;
cin.ignore();
cin >> iop.iOption;
if (iop.iOption == 1)
{
bAdd.bAddEnt = true;
fde.fDecide++;
}
else if (iop.iOption == 2)
{
bAdd.bAddEnt = false;
fde.fDecide++;
break;
}
}
} while (bAdd.bAddEnt == true);
// For future entries this needs to be checked so the values are set accordingly
for (int i = myCo.myContNo; myCo.myContNo <= fde.fDecide;)
{
myCo.myContNo++;
if (myCo.myContNo <= fde.fDecide)
{
myobjs.sBundle.push_back(myobjs.sDay[myCo.myContNo] + myobjs.sLocations[myCo.myContNo] + myobjs.sArrive[myCo.myContNo] + myobjs.sDepart[myCo.myContNo]);
}
}
cout << myobjs.sBundle[0];
myCo.myContNo--;
bMe.bMenu = true;
cout << "Exiting to Admin Menu...\n";
_getch();
}
void Concatenate_Admins()
{
for (int i = 0; i < AdminUsers.AdminUsers.size(); i++)
{
sAds.sAdmins.push_back(AdminUsers.AdminUsers[i] + AdminPas.AdminPasswords[i]);
}
}
int main()
{
do
{
system("cls");
cout << " Western Railway\n";
cout << "Please choose from one of the following\n";
cout << "1:: Train Times\n";
cout << "2:: Make a Reservation\n";
cout << "3:: Make a Cancellation\n";
cout << "4:: Reserved Trains\n";
cout << "5:: Admin Login\n";
cout << "6:: Quit\n";
cin >> iop.iOption;
switch (iop.iOption)
{
case 1:
iop.iOption = 1;
//Train_Times();
case 2:
iop.iOption = 2;
//Make_A_Reservation();
case 3:
iop.iOption = 3;
//Make_A_Cancellation();
case 4:
iop.iOption = 4;
//Reserved_Trains();
case 5:
iop.iOption = 5;
Concatenate_Admins();
// Look into an option to add more admin logins after authorization of login (New Employees)
// also look into updating times for various locations
do
{
system("cls");
cout << " *******Admin Login*******\n";
cout << "Please enter Authorised credentials below;\n";
cout << "Username: " << flush;
cin >> admus.AdminUser;
cout << "Password: " << flush;
cin >> admpa.AdminPass;
iCou.iCount++;
if (find(sAds.sAdmins.begin(), sAds.sAdmins.end(), admus.AdminUser + admpa.AdminPass) != sAds.sAdmins.end())
{
cout << "Successfully Logged in...\n";
bAdm.bAdmin = false;
}
else if (find(sAds.sAdmins.begin(), sAds.sAdmins.end(), admus.AdminUser + admpa.AdminPass) == sAds.sAdmins.end())
{
cout << "Entry not recognized...\n";
cout << "No " << iCou.iCount << " of 3 Attempts\n";
bAdm.bAdmin = true;
if (iCou.iCount == 3)
{
cout << "Too many attempts made\n";
cout << "Exiting to main menu...\n";
bAdm.bAdmin = false;
}
}
_getch();
} while (bAdm.bAdmin == true);
do
{
if (iCou.iCount <= 3)
{
system("cls");
cout << " *******Admin Mode*******\n";
cout << "Please choose from one of the following; \n";
cout << "Option 1: Train Time Entries\n";
cout << "Option 2: View Reserved Lists\n";
cout << "Option 3: View Passenger Info\n";
cout << "Option 4: Return to main menu\n";
cin >> iop.iOption;
switch (iop.iOption)
{
case 1:
iop.iOption = 1;
Train_Time_Entries();
bMe.bMenu = true;
case 2:
iop.iOption = 2;
//Reserved_Lists();
bMe.bMenu = true;
case 3:
iop.iOption = 3;
//Passenger_Info();
bMe.bMenu = true;
case 4:
iop.iOption = 4;
cout << "Returning to Main Menu...\n";
bMe.bMenu = false;
break;
_getch();
}
}
else if (iCou.iCount > 3)
{
bMen.bMenu1 = true;
main();
}
} while (bMe.bMenu == true);
bMen.bMenu1 = true;
break;
case 6:
iop.iOption = 6;
cout << "Exiting Application...\n";
bMen.bMenu1 = false;
break;
}
} while (bMen.bMenu1 == true);
return 0;
}
I'm coding a program for rock paper scissors where there is a user player and a computer player. I believe everything is fine up to the bool value returning function. It needs to take two arguments (computer's choice, player's choice) and see if they are equal to print out "Tie". However, I'm getting an error that says undeclared identifiers for my two arguments.
I tried changing it to an int function instead of bool. and have my bool statements in main, but it did not work
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int getComputerChoice();
int getPlayerChoice();
bool isTie (int, int);
int main()
{
char choice;
int compChoice;
int plaChoice;
do
{
cout << "ROCK PAPER SCISSORS MENU" << endl;
cout << "--------------------------" << endl;
cout << "p)Play Game" << endl;
cout << "q)Quit" << endl;
cout << "Please enter your choice : " << endl;
cin >> choice;
while (choice != 'p' && choice != 'q')//or if//why &&
{
cout << "Invalid selection. Try again." << endl << endl << endl;
cin >> choice;
}
switch (choice)
{
case 'p':
compChoice = getComputerChoice();
plaChoice = getPlayerChoice();
if (plaChoice == 1)
{
cout << "You chose: Rock" << endl;
}
else if (plaChoice == 2)
{
cout << "You chose: Paper" << endl;
}
else
{
cout << "You chose: Scissors" << endl;
}
if (compChoice == 1)
{
cout << "The computer chose: Rock" << endl;
}
else if (compChoice == 2)
{
cout << "The computer chose: Paper" << endl;
}
else
{
cout << "The computer chose: Scissors" << endl;
}
if (isTie(compChoice, plaChoice))
{
cout << "It is a Tie!";
}
break;
case 'q':
cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
break;
}
} while (choice != 'q');
return 0;
}
int getComputerChoice()
{
int comp = 0;
int rando = 0;
srand((unsigned int)time(NULL));
rando = rand() % 3 + 1;
switch (rando)
{
case 1:
comp = 1;
break;
case 2:
comp = 2;
break;
case 3:
comp= 3;
break;
return comp;
}
}
int getPlayerChoice()
{
int player;
cout << "Rock, Paper or Scissors?" << endl;
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "Please enter your choice: " << endl;
cin >> player;
while (player != 1 && player != 2 && player != 3)
{
cout << "Invalid" << endl;
cin >> player;
}
return player;
}
bool isTie(compu, playa)
{
if (compu == playa)
return true;
else
return false;
}
These are the error messages I'm getting
compu': undeclared identifier
playa': undeclared identifier
'isTie': redefinition; previous definition was 'function'
see declaration of 'isTie'
'isTie': function-style initializer appears to be a function definition
isTie is a function that has 2 parameters.
From your code, I can see that it's expecting 2 integers.
So you need to update the function signature to:
bool isTie(int compu, int playa)
I have a pig dice game where there are two modes (1 or 2 dice are rolled). It's played with 2 human players. When I run my program with 1 die selected it runs fine, but when I roll 2 dice it gets thrown into an infinite loop. I'm looking for a hint on where the problem lies and why it was thrown into a loop as in my mind both programs should be almost identical. Sorry in advance if the code looks strange.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int PLAYER1 = 0;
const int PLAYER2 = 1;
const int winningScore = 100;
int turn = PLAYER1;
void printIntro()
{
cout << " Welcome to the dice game: Pig! "<< endl;
cout << "The goal is to be the first player to reach 100. If playing with one die the rules are that each player can rol as many times as they choose. just dont roll a 1 or else you'll lose your turn AND all points accumulated in that round. If you're playing with 2 dies the same rules applies, but if you roll snake eyes (double 1's) you'll not only lose your turn but you'll also loose all your points. good luck and may the best player win!"<< endl;
}
int game1(string playerName, int playerScore)
{
int roll = rand() % 6 + 1;
cout << playerName << " You rolled: " << roll <<endl;
if(roll == 1)
{
cout << " OH NO! You rolled a 1. "<< endl;
cout << " Your turn is over. " << endl;
playerScore = 0;
}
else
{
playerScore +=roll;
cout << playerName << " Your score: " << playerScore <<endl;
}
if(roll == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else
{
char choice;
cout << " Would you like to roll again? (y/n): ";
cin >> choice;
if(choice != 'y')
{
if (turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
return playerScore;
}
int game2(string playerName, int playerScore)
{
int roll1 = rand() % 6 + 1;
int roll2 = rand() % 6 + 1;
cout << playerName << " You rolled: " << roll1 << " and " << roll2 <<endl;
if(roll1 || roll2 == 1)
{
cout << " OH NO! You rolled a 1. " << endl;
cout << " Your turn is over. " << endl;
playerScore = 0;
}
else if (roll1 && roll2 == 1)
{
cout << "OH CRAP! You rolled snake eyes!" << endl;
cout << " Your turn is over. " << endl;
playerScore == 0;
}
else
{
playerScore += roll1 + roll2 ;
cout << playerName << " Your score: " << playerScore <<endl;
}
if(roll1 || roll2 == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else if (roll1 && roll2 == 1)
{
if(turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
else
{
char choice;
cout << "Would you like to roll again? (y/n): ";
cin >> choice;
if(choice != 'y')
{
if (turn == PLAYER1)
turn = PLAYER2;
else
turn = PLAYER1;
}
}
return playerScore;
}
int main()
{
srand(time(0));
int player1score = 0;
int player2score = 0;
string player1name;
string player2name;
int dieRoll;
printIntro();
cout << " Player 1, Enter your name: ";
cin >> player1name;
cout << " Player 2, Enter your name: ";
cin >> player2name;
cout << "Wouild you like to roll with 1 or 2 dice?" << endl;
cin >> dieRoll;
if (dieRoll == 1)
{
while (player1score < winningScore && player2score < winningScore)
{
if (turn == PLAYER1)
{
player1score = game1(player1name, player1score);
}
else
{
player2score = game1(player2name, player2score);
}
}
if(player1score >= winningScore)
{
cout << player1name <<endl;
cout << " Your score is : " << player1score<<endl;
cout << player1name << " WINS! " << endl;
}
else
{
cout << player2name << endl;
cout <<" Your score: "<< player2score << endl;
cout << player2name << " WINS!" << endl;
}
}
else
{
while (player1score < winningScore && player2score < winningScore)
{
if (turn == PLAYER1)
{
player1score = game2(player1name, player1score);
}
else
{
player2score = game2(player2name, player2score);
}
}
if(player1score >= winningScore)
{
cout << player1name <<endl;
cout << " Your score is : " << player1score<<endl;
cout << player1name << " WINS! " << endl;
}
else
{
cout << player2name << endl;
cout <<" Your score: "<< player2score << endl;
cout << player2name << " WINS!" << endl;
}
}
return 0;
}
There are a couple issues in the following block which may be causing problems:
else if (roll1 && roll2 == 1)
{
cout << "OH CRAP! You rolled snake eyes!" << endl;
cout << " Your turn is over. " << endl;
playerScore == 0;
}
The way you have the conditional written, it just checks to see if roll1 is anything besides zero (that's the part before &&) and then it checks if roll2 is equal to 1. The conditional should probably read: else if (roll1 == 1 && roll2 == 1).
And I think you want to assign (=) playerScore at the bottom rather than evaluate for equality (==). You already know this, but this is what it should look like: playerScore = 0.
I need help on how to allowing player to input a number in this code:
using namespace std;
int main()
{
char name[50];
menu:
cout << "Select your choice..." << endl;
cout << "1) Start Game" << endl;
cout << "2) Help" << endl;
cout << "3) Exit Game" << endl;
cin.getline(number, 1);
if(number = 1) {
start_game();
}
else if(number = 2){
help();
}
else if(number = 3){
exit();
}
else {
goto menu;
}
return 0;
}
It says that there is a problem in the cin.getline(number,1) in my program. If anyone knows how to fix it, please tell me. Thanks
Do it like this:
using namespace std;
int main()
{
char name[50];
while (true) // Use a loop instead of goto since it is more readable for other developers
{
cout << "Select your choice..." << endl;
cout << "1) Start Game" << endl;
cout << "2) Help" << endl;
cout << "3) Exit Game" << endl;
int number; // You didn't declare `number`
cin >> number; // You used getline wrong
if (number == 1) { // Use `==` for comparing
start_game();
break;
}
if (number == 2) {
help();
break;
}
if (number == 3) {
exit();
break;
}
}
return 0;
}
I explained some fixes in the comments.
#include <iostream>
using namespace std;
int main()
{
char name[50];
int number ;
menu:
cout << "Select your choice..." << endl;
cout << "1) Start Game" << endl;
cout << "2) Help" << endl;
cout << "3) Exit Game" << endl;
cin >> number;
if(number == 1) {
start_game();
}
else if(number == 2){
help();
}
else if(number == 3){
exit();
}
else {
goto menu;
}
return 0;
}
I don't understand why the following code always prints 'draw!'. Can you help me figure that out?
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
void main()
{
clrscr();
int choice1;
char choice2[50];
int compare(const char*, const char*); //prototype
char s[100];
cout << "\n\n WELCOME TO STONE PAPER SCISSORS " << endl
<< endl;
cout << " enter your choice" << endl;
cout << "\n 1:SCISSORS \n";
cout << "\n 2:ROCK \n";
cout << "\n 3:PAPER \n";
cout << "enter choice number";
cin >> choice1;
if (choice1 == 2) {
cout << "you have entered Stone!";
strcpy(s, "ROCK");
}
if (choice1 == 1) {
cout << "you have entered scissors";
strcpy(s, "SCISSORS");
}
if (choice1 == 3) {
strcpy(s, "PAPER");
cout << "you have entered paper";
}
randomize();
float point = 2;
float compchoice;
compchoice = random(point);
if (compchoice < 0.37) {
strcpy(choice2, "ROCK");
}
else if (compchoice < 0.64) {
strcpy(choice2, "PAPER");
}
else {
strcpy(choice2, "SCISSORS");
}
cout << endl;
cout << "User Choice=" << s << endl;
cout << "Computer Choice=" << choice2 << endl;
cout << s << "\t"
<< "VS"
<< "\t" << choice2 << "="
<< " ";
int p = compare(s, choice2);
if (p == 1) {
cout << "computer wins";
if (p == 0)
cout << "user wins";
if (p == -1)
cout << "draw!";
getch();
}
int compare(const char* s, const char* choice2)
{
if (s == "SCISSORS") {
if (choice2 == "ROCK")
return 1;
else
return 0;
}
else if (s == "ROCK") {
if (choice2 == "SCISSORS")
return 0;
else
return 1;
}
else if (s == "PAPER") {
if (choice2 == "SCISSORS")
return 1;
else
return 0;
}
else
return -1;
}
The problem in this code lies within code like if (choice2 == "SCISSORS")
as this is a pointer comparison. For details, see this stack overflow post.
I would suggest modernizing this code and use std::string instead of char [50] as you have to worry less about memory for these strings, nor the fact that strings are stored as arrays.