找到我写的基于文本的石头剪刀应用程序后,我决定改进代码。现在,我想写入控制台以在返回主菜单之前以及在执行请求的应用程序操作之前清除屏幕。这是代码:
#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;
}
我考虑使用cin.clear()
,但如果我想打开不同的功能,我认为这对我没有帮助。我该怎么办?
答案 0 :(得分:0)
对于发现此问题的任何人,我在加载其他操作之前使用system("CLS");
清除控制台。