at the moment I'm trying to add up the numbers for my hand and the hit cards, the issue is I created a function for my random number generator so that I can keep calling it into my dice game and the blackjack game, I would normally add the number generator to a variable and call it a day but I made it into a function instead. I am still new to c++.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void RandomNumber() { cout << (rand() % 10) + 1; }
void blackjack(int total) {
int startstakes = 15;
int stakes;
int hand;
cout << "Welcome to the Blackjack(21) table\n"
<< "How much are you adding to your initial 15 chip stake - ";
cin >> stakes;
cout << "New stake - " << stakes + 15 << " remaining chips - "
<< total - (stakes + 15) << endl;
cout << "Here is your hand - ";
RandomNumber();
cout << " and ";
RandomNumber();
cout << "Hit me cards: 0 - 0 - 0\n"
<< "Total = ";
system("pause>0");
}
int main() {
int total = 0;
int choice;
srand((unsigned)time(NULL));
cout << "Welcome to Royal Casino!!!, How much money do you wish to "
"convert? ";
cin >> total;
cout << "Excelent you currently have " << total << " Chips.\n"
<< "Let's Begin!\n\n"
<< "We have to tables available today\n";
cout << "1) Blackjack (21)\n"
<< "2) Dice\n"
<< "Both have an entry fee of 15 Chips\n"
<< "Select a table - ";
cin >> choice;
if (choice == 1) {
blackjack(total);
}
if (choice == 2) {
dice();
}
system("pause");
}
So the issue is that you should return the value instead of printing it. Like this (note the return type has changed from void to int)
int RandomNumber() {
return (rand() % 10) + 1;
}
A function which returns a value is much more flexible than a function which prints a value.
Now you can use a function call RandomNumber() pretty much the same way as a variable. E.g.
cout << RandomNumber() << " and " << RandomNumber();
or
int var = RandomNumber();
Related
I'm currently making a 2 player dice game and i need to create a function that checks the value of the dice combination you rolled. Ex: I rolled 3-4-2, I need the function to check if there is a payout for 3-4-2, example rolls and their payouts + code below
//Rolling 1-1-1 would give you 5x your wager
//Rolling 3 of the same number (except 1-1-1) would give you 2x wager
//Rolling 3 different numbers (ex 1-4-6) would give you 1x your wager
//Rolling 1-2-3 makes the player automatically lose a round and pay opposing Player 2x wager
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void roll_3_dice(int &dice1, int &dice2, int &dice3)
{
srand(time(NULL));
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
dice3 = rand() % 6 + 1;
return;
}
int main()
{
int cash = 90000;
int wager;
int r;
//dealer's die
int dealer1;
int dealer2;
int dealer3;
// your die
int mdice1;
int mdice2;
int mdice3;
while ( cash > 100 || round < 10 )
{
cout << "Set your wager: "<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}
cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;
cout << endl;
cout << "Dealer will now roll the dice" << endl;
roll_3_dice(dealer1, dealer2, dealer3);
cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;
cout << "It's your turn to roll the dice." << endl;
cout << endl;
cout << "Press any key to roll the dice" << endl;
cin >> r;
roll_3_dice(mdice1, mdice2, mdice3);
cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;
system ("pause`enter code here`");
}
}
I would suggest to write your algorithm on a paper first, like you did in the comments on top of the code.
Then, ask yourself what you need to process the final wager ? As in parameters. For instance, in your case, you may need a function which takes the initial wager, and the values of the three dices as entry parameters.
This function would return the final wager.
Finally, in the function itself, organize the algorithm by priority rules.
If 1-1-1 gives you 5x your wager, then it's a particular case which can be isolated at the top of the function maybe, and you can return 5 x initial wager directly, without any need to process further operations.
You'll just have to organize the different cases with if statements, regarding the priorities of each statement.
The 1-1-1 case has to come before the "same number for each dice" for example.
Hope this helps.
So i'm making a turn based dice game that's modeled after this game called "underground chinchiro" which was taken from an anime called "Kaiju". I need to set a limit to my program so that it only runs for a set number of rounds,
I'm only a beginner in coding so sorry for anything unusual you see in my code.
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void roll_3_dice(int &dice1, int &dice2, int &dice3)
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
dice3 = rand() % 6 + 1;
return;
}
int main()
{
int cash = 90000;
int wager;
int r;
//dealer's die
int dealer1;
int dealer2;
int dealer3;
// your die
int mdice1;
int mdice2;
int mdice3;
for (int i = 0; i < 10; i++)
{
cout << "Wager up boy!"<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}
cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;
cout << endl;
cout << "Dealer will now roll the dice" << endl;
roll_3_dice(dealer1, dealer2, dealer3);
cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;
cout << "It's your turn to roll the dice." << endl;
cout << endl;
cout << "Press any key to roll the dice" << endl;
cin >> r;
roll_3_dice(mdice1, mdice2, mdice3);
cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;
system ("pause");
}
}
Take a look at for loops. For loops will allow you to run your code for a set number iteration.
e.g. iterate over some code 7 times.
int number_of_iterations = 7;
for(int i = 0; i < number_of_iterations; i++) {
// Your code that you would like to iterate over goes here.
}
EDIT:
As has been specified by the OP (in comments below), the issue is appears to be with the program not stopping to receive input from the user through each iteration of the for loop.
This could be for a number of reasons. My best guess would be that the stdin buffer is not clear and that std::cin continues to read in from the buffer. This could be solver by calling cin.clear() before reading in your input.
is time to learn how to use constants...
define one doing
const int max_round = 5;
and the do a while so long round is <= than max_round
Your problem is pretty unclear. Edit your code, find the section where problems occurring and paste that part only.
like:
while(cin>>wager)
{
if(condition fails)
{
break;
}
}
I am creating a guessing game. I need to display the users' previous guesses every time the user makes another guess. I am using an array and pointers. The code below only displays the most recent thing entered by the user. I need it to display a list of previous inputs. The part of the code that includes the pointer seems to be where my issue is located, but I don't know exactly where?
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
void arrayTable(int[]);
int reviewGuess(int Answer, int userGuess);
int _tmain(int argc, _TCHAR* argv[])
{
int guessNum = 3; // Declares how many guesses the user can take.
int userGuess = 0;
int Answer;
char Choice;
int *pointer = NULL;
srand(time(NULL));
Answer = rand() % 20 + 1; //Gives a random number ranging from 1 to 20
do{
int x;
cout << " ____________________________________________________\n";
cout << " | |\n";
cout << " | I want to play a game |\n"; // Displays a welcome message
cout << " | I am thinking of a number between 1 through 20. |\n";
cout << " | Can you guess the number in less than " << guessNum << " tries ? |\n";
cout << " | Press '1' to play. |\n";
cout << " | Press '2' if you want to EXIT!!! |\n";
cout << " |__________________________________________________|\n";
cin >> x;
switch (x)
{
case 1:
for (int i = 0; i < guessNum; i++) //Counter, for loop, stops when condition is met.
{
cout << " This is guess# " << i + 1 << " : "; //Takes the user input
cin >> userGuess;
int guessNum = 1;
pointer = new int[guessNum];
for (int i = 0; i < guessNum; i++) //Store the user guesses
{
*(pointer + i) = userGuess;
}
cout << "Here is a list of your guesses" << endl;
for (int i = 0; i < guessNum; i++) // Display the user guesses.
{
cout << " Guess# " << i + 1 << " is " << *(pointer + i) << endl;
}
reviewGuess(Answer, userGuess); //Calls the function
}
cout << " ------------------> <----------------------\n";
cout << " ------------------> You Lost!!! <----------------------\n";
cout << " ------------------> <----------------------\n";
cout << " You have exceeded the amount of guesses you were given.\n";
cout << " The correct Answer is : " << Answer << endl;
break;
case 2:
cout << "-------------->Thank You.\n";
cout << "-------------->Have a good day.\n";
break;
system("pause");
return 0;
}
cout << "would you like to play again (y/n)?";
cin >> Choice;
} while (Choice == 'y');
system("pause");
return 0;
}
int reviewGuess(int Answer, int userGuess)
{
if (userGuess != Answer)
{
if (userGuess > Answer)
cout << " -----> 1 \n";
else
cout << " -----> -1 \n";
}
else
{
cout << " -----> 0 \n";
cout << " Good JOb, you have guessed the right number. \n";
system("pause");
return 0;
}
}
review the changes, and don't just copy and paste.
to access pointer array members, you can use the index [].
you are already in a for loop, so you dont want to create another for loop getting input, that would be 3 x 3 requests for data.
you were storing the data into an int, not into the array you created with pointer.
pointer isn't a very good name, when its an array of guesses. try calling it something more readable.
once it ran, if the user won, it still triggered losing code. you need to break out before accessing that code the way you have it set up. i created a bool, and changed your reviewguess method to return a bool whether they won or not. break out on exiting. note the double use of breaks; its in a for loop in a switch. you have to break out of the foor loop, and then break out of the switch. you cannot just put it after the for loop, as it will then keep asking for input if they win, and then report they won at the end.
note the use of the global constant to keep track of how many guesses. you were declaring and initializing the same variables all over the place. initialize with the type keyword once, at the start, and if you need the variable in the loop, initalize it before the loop so you dont try to reinitialize every iteration.
logic was reversed as far as higher or lower with reviewGuess
Not a bad start, keep your head up, and i hope i could help you out!
this is my first time answering a question.
Code:
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
void arrayTable(int[]);
bool reviewGuess(int, int);
int _tmain(int argc, _TCHAR* argv[])
{
const int MAX_NUMBER_GUESSES = 3;// Declares how many guesses the user can take.
int currentGuessNum = 0;
int userGuess = 1;
int Answer;
char Choice;
int x;
int *arrGuesses = NULL;
bool gameWon = false;
srand(time(NULL));
//Gives a random number ranging from 1 to 20
do {
Answer = rand() % 20 + 1; //Gives a random number ranging from 1 to 20
//you want this in the loop otherwise you always have same answer
cout << " ____________________________________________________\n";
cout << " | |\n";
cout << " | I want to play a game |\n"; // Displays a welcome message
cout << " | I am thinking of a number between 1 through 20. |\n";
cout << " | Can you guess the number in less than " << MAX_NUMBER_GUESSES << " tries ? |\n";
cout << " | Press '1' to play. |\n";
cout << " | Press '2' if you want to EXIT!!! |\n";
cout << " |__________________________________________________|\n";
cin >> x;
switch (x)
{
case 1:
userGuess = 1;
arrGuesses = new int[MAX_NUMBER_GUESSES];
for (int i = 0; i < MAX_NUMBER_GUESSES; i++) //Counter, for loop, stops when condition is met.
{
cout << " This is guess# " << userGuess << " : " << endl; //Takes the user input
cin >> arrGuesses[i];
cout << "Here is a list of your guesses" << endl;
for (int i = 0; i < userGuess; i++) // Display the user guesses.
{
cout << " Guess# " << i+1 << " is " << arrGuesses[i] << endl;
}
gameWon = reviewGuess(Answer, arrGuesses[i]); //Calls the function
if (gameWon)
{
break;
}
userGuess++;
}
if (gameWon)
{
break;
}
cout << " ------------------> <----------------------\n";
cout << " ------------------> You Lost!!! <----------------------\n";
cout << " ------------------> <----------------------\n";
cout << " You have exceeded the amount of guesses you were given.\n";
cout << " The correct Answer is : " << Answer << endl;
break;
case 2:
cout << "-------------->Thank You.\n";
cout << "-------------->Have a good day.\n";
break;
system("pause");
return 0;
}
cout << "would you like to play again (y/n)?";
cin >> Choice;
} while (Choice == 'y');
system("pause");
return 0;
}
bool reviewGuess(int Answer, int userGuess)
{
if (userGuess != Answer)
{
if (userGuess < Answer)
cout << " -----> 1 \n";
else
cout << " -1 <----- \n";
return false;
}
else
{
cout << " -----> 0 \n";
cout << " Good JOb, you have guessed the right number. \n";
system("pause");
return true;
}
}
I'm making a small game in C++ where a user will type "roll" and the program should generate 5 numbers between 1-6. The objective of the game is to get all the numbers the same.
My problem I am having is fully understanding how to format the different algorithms need for a project like this and what type of different syntax's that allowed through the language.
I am a student learning this language and will appreciate any help anyone is willing to offer!
The code below is not finished in anyway and just wanted some extra advice!
Code:
// Random Number Game
// ***
// ***
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
// Intro Instruction
cout << "Welcome to Random Number game!\n";
cout << "The objective of this game to get all 5 dice the same number.\n";
// Variables
char roll;
int dice1, dice2, dice3, dice4, dice5;
cout << "To begin the game please type roll. ";
cin >> roll;
srand((unsigned)time(0));
// Stating Low and High Numbers (Dice numbers)
int lowerrange = 1;
int upperrange = 7;
upperrange = 6;
dice1 = rand() % 6;
dice2 = rand() % 6;
dice3 = rand() % 6;
dice4 = rand() % 6;
dice5 = rand() % 6;
if ((roll == 'roll') || (roll == 'Roll'))
{
cout << dice1;
cout << dice2;
cout << dice3;
cout << dice4;
cout << dice5;
}
system("pause");
return 0;
}
Here is a possible solution.
The idea is to use a loop to repeatedly asking from user to "roll" the dices, for testing purposes only I put 3 dices here, but you can extend to 5 dices.
I maintain a boolean variable that will be true if and only if all the dices has the same value, if that the case, the program will break out from loop, and will print a successful message.
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Intro Instruction
cout << "Welcome to Random Number game!\n";
cout << "The objective of this game to get all 5 dice the same number.\n";
string command;
int magic = 0;
do
{
cout << "To begin the game please type roll: ";
cin >> command;
if(command == "roll")
{
int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
int dice3 = rand() % 6 + 1;
cout << "First Dice: " << dice1 << endl;
cout << "Second Dice: " << dice2 << endl;
cout << "Third Dice: " << dice3 << endl;
cout << "Which Dice do you want to keep? : (Enter 1, 2 or 3)" << endl;
int keep_dice;
cin >> keep_dice;
if(keep_dice == 1)
{
magic = dice1;
break;
}
else if(keep_dice == 2)
{
magic = dice2;
break;
}
else if(keep_dice == 3)
{
magic = dice3;
break;
}
}
}
while(true);
cout << "You choose the number: " << magic << ". Roll the other dices until you get for both this number." << endl;
do
{
cout << "Type roll: ";
cin >> command;
if(command == "roll")
{
int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
cout << "First Dice: " << dice1 << endl;
cout << "Second Dice: " << dice2 << endl;
if(dice1 == magic && dice2 == magic)
{
break;
}
}
}
while(true);
cout << "You won !" << endl;
}
Alternative Solution:
My idea is to create a function that will accept two parameters: num_dice the remaining dices to be rolled if you have 5 dices that will be 4, if you have n dices this will be n-1 and so on.
The other parameter will be the lucky number that the user will choose (easy with an if or with switch statement)
You will then repeat that with the same logic within your function until you get your n-1 dices equal to lucky number.
Moreover, think that my code may need a bit alteration, so that the first roll of all dices will happen once and not repeatedly, and then with the function you will do the repeat "job".
To check if all the dices are the same, use the code below:
int magicValue = dice1;
cout << "You got:" << endl;
cout << "Dice 1: " << dice1 << endl;
cout << "Dice 2: " << dice2 << endl;
cout << "Dice 3: " << dice3 << endl;
cout << "Dice 4: " << dice4 << endl;
cout << "Dice 5: " << dice5 << endl;
if(dice2 == magicValue && dice3 == magicValue
&& dice4 == magicValue && dice5 == magicValue){
cout << "Congratulations! You Win!" << endl;
} else {
cout << "Sorry, try again next time!" << endl;
}
What this does is it sets magicValue to dice1 and checks if all other dice variables are equal. Not the fanciest way to do it, but I hope this helps you!
Since you have at the top ‘Using namespace std`
You don’t have to specify using std::cout or anything like that.
Also you can just assign upperrange to 6 right away.
Besides this jut put it in a loop if you want, add some more output so they know they can keep playing.
Not a great title I know but I'm not sure how to word it. Anyway, in brief I am trying to calculate the number of scores, the total of scores and the average and grade of the scores. I am using for... loops to complete this task. So...
my function prototypes:
int validateNumber(int);
void summary(int,int,float,char);
char getGrade(float);
float getAvg(int,int);
probably only the validateNumber(int) is relevant here but just in case.
The main()
int num, total, scores;
cout << over4 << "How many scores do you want to average? " << endl;
cout << over4 << "Enter a value from 1 to 4: ";
cin >> num;
And the calls(?):
total = validateNumber(num);
summary(num,total,average,grade);
And then the definitions:
int validateNumber(int num)
{
int total = 0, score;
while (num < 1 || num > 4)
{
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++)
{
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << over3 << score << " is not between 0 and 100! Renter the score: "
<< i << ": ";
cin >> score;
}
total += score;
}
return total;
}
and:
void summary(int num,int total,float average,char grade)
{
cout << over4 << "Number of scores : " << num << endl;
cout << over4 << "Scores total : " << total << endl;
cout << over4 << "Average : " << average << "%" << endl;
cout << over4 << "Grade : " << grade << endl;
cout << down11;
}
When the user enters a num value between 1 and 4, there is no problem, the program works as it should. However when the user enters a value for num not in that range, the function works as it should BUT the summary will tell me that the number of scores was that first erroneous value and as a result mess up my average/grade.
in your function you are passing by value not by reference, so the change which is done in your function int validateNumber(int); is only in that function's scope, outside num's value is same as it was first. you should send value by reference. in this way :
int validateNumber(int &);
What happens is that you pass num to the validateNumber function by value. The local num in the validateNumber function copies the global num's value and get's processed accordingly. However, the global num variable stays as it were. In order to change the num itself you will have to pass it by reference. Change the parameters on your function definition to: int validateNumber(int &num) { ... }