Source Code Simple C++ Practice Code

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
I thought I would share some of my simple mini-games and code pieces in the hope that it might help some people who are beginning their adventure into C++.

C++:
//Rock-Paper-Scissors Game

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

enum hand {ROCK = 1, PAPER, SCISSOR};
char PlayAgain = 'y';

int main()
{
	srand (static_cast<unsigned int>(time(0)));

	while (PlayAgain == 'y')
	{
		cout << "Rock-Paper-Scissors!\n\n";
		cout << "1)Rock\n2)Paper\n3)Scissors\n";
		int randomNumber = rand();

		int CompChoice = (randomNumber % 3) + 1;
		int PlayerChoice;
		cout << "Your choice?\n";
		cin >> PlayerChoice;

		//Checks if the choices are the same
		if (PlayerChoice == 1 && CompChoice == 1 || PlayerChoice == 2 && CompChoice == 2 || PlayerChoice == 3 && CompChoice == 3)
		{
			cout << "Tie!";
			cout << "\nWant to play again?";
			cin >> PlayAgain;
			system("CLS");
		}

		//Checks if the player has the winning choice
		else if (PlayerChoice == 1 && CompChoice == 3 || PlayerChoice == 2 && CompChoice == 1 || PlayerChoice == 3 && CompChoice == 2)
		{
			cout << "You Win!\n";
			cout << "Comp Chose " << CompChoice;
			cout << "\nWant to play again?";
			cin >> PlayAgain;
			system("CLS");
		}

		//If it isn't a tie or a win, it has to be a loss
		else
		{
			cout << "You Lost!\n";
			cout << "Comp Chose " << CompChoice;
			cout << "\nWant to play again?";
			cin >> PlayAgain;
			system("CLS");
		}
	}

	return 0;
}

C++:
#include <iostream>
#include <ctime>

using namespace std;

void compGuess();
int playerNumber;
int high = 100;
int low = 0;
int& rHigh = high;
int& rLow = low;
const int& crHigh = high;
const int& crLow = low;
int guess;
int& rGuess = guess;
int tries;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	cout << "Please give a number 1-99:\n";
	cin >> playerNumber;

	// the playerNumber is used to compare to the computer's guesses
	do
	{
		compGuess();
		if (guess > playerNumber) // check if guess is too high
		{
		cout << "The comp guessed " << guess << ", which is too high.\n";
		++tries;
		high = rGuess;
		}
		else if (guess < playerNumber) // check if guess is too low
		{
		cout << "The comp guessed " << guess << ", which is too low.\n";
		++tries;
		low = rGuess;
		}
	} while (guess != playerNumber); // Goldilocks found the number that was
	++tries;						 // just right!
	cout << "The computer guessed " << guess;
	cout << " in " << tries << " tries.\n";

	return 0;
}

// this makes sure that the guess is within the bounds of low and high
void compGuess()
{
	do
	{
		guess = rand()%(crHigh - crLow + 1) + crLow;
	} while (guess <= low || guess >= high);
}

C++:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
void answerZero();
void answerOne();
void answerTwo();
void answerThree();
int answer;
string addItem;
string removeItem;
vector<string> bag;
vector<string>::iterator myIterator;

int main()
{
	//Adding strings to the vector
	bag.push_back("Sword");
	bag.push_back("Armor");
	bag.push_back("Shield");

	//Sorts the items, then displays them
	cout << "Items in your bag:\n";
	sort(bag.begin(), bag.end());
	
	for (unsigned int i = 0; i < bag.size(); ++i)
	{
		cout << bag[i] << endl;
	}

	do
	{
		cout << "\nWhat would you like to do?\n";
		cout << "0 - Add an item\n";
		cout << "1 - Remove an item\n";
		cout << "2 - View items\n";
		cout << "3 - Exit\n";
		cin >> answer;

		if (answer == 0)
		{
			//Add Item
			answerZero();
		}

		else if (answer == 1)
		{
			//Remove Item
			answerOne();
		}

		else if (answer == 2)
		{
			//View Items
			answerTwo();
		}
	
		else if (answer == 3)
		{
			//Exit
			answerThree();
		}
	} while (answer != 3);

	return 0;
}


void answerZero()
{
		cout << "\nWhat would you like to add?\n";
		cin >> addItem;
		bag.push_back(addItem);
}


void answerOne()
{
		cout << "\nWhat would you like to remove?\n";
		cin >> removeItem;
		myIterator = find(bag.begin(), bag.end(), removeItem);

		if (myIterator != bag.end())
		{
			bag.erase(myIterator);
		}
		else
		{
			cout << "Item not found.\n";
		}
}


void answerTwo()
{
		cout << "\nYour Items:\n";
		sort(bag.begin(), bag.end());

		for (unsigned int i = 0; i < bag.size(); ++i)
		{
			cout << bag[i] << endl;
		}
}

void answerThree()
{
	cout << "\nGoodbye.\n";
}


Hopefully someone will find this useful, or have fun with the games :p. A simple "challenge" to a newer coder would maybe be to add a score system to Rock-Paper-Scissors, and a way of displaying that score every round, and storing those scores in a vector. Another step could be to use iterators and references to improve upon RPS as well as the inventory code, and try using pointers in place of iterators or references. Just some ideas to work the brain a little bit.
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,093
78,998
2,372
It's fun to see how different each person writes a program. There's so many different ways to accomplish the same thing
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
Very true! I prefer to keep things a little bit spaced, while others might like to scrunch things together. I've seen someone use mass hyphens to separate their code segments. I guess it all depends on where someone learned to code, and their own preferences. It's all kinda like a coder's accent :p
 

GAFO666

Hacker
Meme Tier VIP
Aug 19, 2012
520
3,188
23
pretty basic but such we had in the 1st semester of univeristy xDD
just wondering why the first one is not 'rock paper scissors lizard spock' Oo
 

Solaire

Respected Hacker
Dank Tier VIP
Dec 15, 2013
1,051
16,353
62
pretty basic but such we had in the 1st semester of univeristy xDD
just wondering why the first one is not 'rock paper scissors lizard spock' Oo
Hahaha I'll have to expand that now, I'll add it to this post once I'm finished! And yeah, about as basic as it gets (other than "Hello World!").

Edit:
I finished Rock-Paper-Scissors-Lizard-Spock!

C++:
//Rock-Paper-Scissors Game

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

enum hand {ROCK = 1, PAPER, SCISSOR, LIZARD, SPOCK};
char PlayAgain = 'y';

int main()
{
	srand (static_cast<unsigned int>(time(0)));

	while (PlayAgain == 'y')
	{
		cout << "Rock-Paper-Scissors!\n\n";
		cout << "1)Rock\n2)Paper\n3)Scissors\n4)Lizard\n5)Spock\n";
		int randomNumber = rand();

		int CompChoice = (randomNumber % 5) + 1;
		int PlayerChoice;
		cout << "Your choice?\n";
		cin >> PlayerChoice;

		while (PlayerChoice < 1 || PlayerChoice > 5)
		{
			system("CLS");
			cout << "Rock-Paper-Scissors!\n\n";
			cout << "1)Rock\n2)Paper\n3)Scissors\n4)Lizard\n5)Spock\n";
			cout << "\nInvalid Choice\n";
			cout << "Your choice?\n";
			cin >> PlayerChoice;
		}

		//Checks if the choices are the same
		if (PlayerChoice == 1 && CompChoice == 1)
		{
			cout << "Tie! Two Rocks Don't Make A Right!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		if (PlayerChoice == 2 && CompChoice == 2)
		{
			cout << "Tie! Paper Can't Cut Paper!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		if (PlayerChoice == 3 && CompChoice == 3)
		{
			cout << "Tie! Scissoring!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		if (PlayerChoice == 4 && CompChoice == 4)
		{
			cout << "Tie! The Lizards Walk Away!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		if (PlayerChoice == 5 && CompChoice == 5)
		{
			cout << "Tie! What The Spock Is Going On?!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		//Checks if the player has the winning choice
		else if (PlayerChoice == 3 && CompChoice == 2)
		{
			cout << "You Win! Scissors Cut Paper!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 2 && CompChoice == 1)
		{
			cout << "You Win! Paper Covers Rock!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 1 && CompChoice == 4)
		{
			cout << "You Win! Rock Crushes Lizard!";
			cout << "\nWant to play again?";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 4 && CompChoice == 5)
		{
			cout << "You Win! Lizard Poisons Spock!";
			cout << "\nWant to play again?";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 5 && CompChoice == 3)
		{
			cout << "You Win! Spock Smashes Scissors!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 3 && CompChoice == 4)
		{
			cout << "You Win! Scissors Decapitate Lizard!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 4 && CompChoice == 2)
		{
			cout << "You Win! Lizard Eats Paper!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 2 && CompChoice == 5)
		{
			cout << "You Win! Paper Disproves Spock!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 5 && CompChoice == 1)
		{
			cout << "You Win! Spock Vaporizes Rock!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 1 && CompChoice == 3)
		{
			cout << "You Win! Rock Crushes Scissors!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		//Checks if player has the losing choice
		else if (PlayerChoice == 2 && CompChoice == 3)
		{
			cout << "You Lose! Scissors Cut Paper!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 1 && CompChoice == 2)
		{
			cout << "You Lose! Paper Covers Rock!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 4 && CompChoice == 1)
		{
			cout << "You Lose! Rock Crushes Lizard!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 5 && CompChoice == 4)
		{
			cout << "You Lose! Lizard Poisons Spock!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 3 && CompChoice == 5)
		{
			cout << "You Lose! Spock Smashes Scissors!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 4 && CompChoice == 3)
		{
			cout << "You Lose! Scissors Decapitate Lizard!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 2 && CompChoice == 4)
		{
			cout << "You Lose! Lizard Eats Paper!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 5 && CompChoice == 2)
		{
			cout << "You Lose! Paper Disproves Spock!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 1 && CompChoice == 5)
		{
			cout << "You Lose! Spock Vaporizes Rock!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}

		else if (PlayerChoice == 3 && CompChoice == 1)
		{
			cout << "You Lose! Rock Crushes Scissors!";
			cout << "\nWant to play again?\n";
			cin >> PlayAgain;
			system("CLS");
		}
	}

	return 0;
}
 
Last edited:
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods