Write a C++ program that simulates the casino game of craps. These are the rules of the game:
• If a player throws a 7 or 11 (sum of two dice) on the first roll, the player wins the game.
• If a player throws a 2, 3 or 12 (sum of two dice) on the first roll, the player loses the game.
• If a player throws a 4, 5, 6, 8, 9 or 10 (sum of two dice) on the first roll, s(he) neither wins nor loses but creates a “point.” If this is the case, the player keeps rolling the dice until the point (4, 5, 6, 8, 9 or 10) is thrown again, and the player wins the game. However, if the player throws a 7 (sum of two dice) before the “point” is thrown, the player loses the game.
You will create a function called rollDice that will, when called, roll two dice and return a random number between 2 and 12. Each time rollDice is called, the program should output the result of the roll
The program will ask the player ” Another game? Y(es) or N(o)?” and terminate whenever any key other than Y or y is pressed.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int dice1, dice2 = 0;
int rollDice;
char repeat = 'y';
cout << "**********************************************************"<< endl;
cout << "******** Welcome to the Kyung Bae Choi Casino ********"<< endl;
cout << "********* Step up to the table and place your bets! ******"<< endl;
cout << "**********************************************************"<< endl;
while (repeat == 'y' || repeat == 'Y')
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
rollDice = dice1 + dice2;
cout << "Your rolled " << rollDice;
if (rollDice == 7 || rollDice == 11)
{
cout << ". Winner !" << endl ;
}
else if (rollDice == 2 || rollDice == 3 || rollDice == 12)
{
cout << ". You lose!" << endl;
}
else if (rollDice == 4 || rollDice == 5 ||rollDice == 6 ||rollDice == 8 || rollDice == 9 || rollDice == 10)
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
int sum2 = dice1 + dice2;
if( sum2 == rollDice )
{
cout << ". Winner !" << endl;
break;
}
else if( sum2 == 7 )
{
cout << ". You Lose!" << endl;
break;
}
}
cout <<"Another game? Y(es) or N(o)" << endl;
cin >> repeat;
while (repeat == 'n' || repeat == 'N')
{
cout << "Thank you for playing!"<< endl;
}
return 0;
}
}
This does output only 7. so, it keep saying Your rolled 7. Winner !
Another game? Y(es) or N(o). And, if i put y, the program ends. or if i put n, the program output the "thank you for playing!" endlessly.
what is problem??
How can I make it continually re-rolling the dice until the player rolls the winning "point" or rolls a seven.
You're missing the "...." in the coutstatement. So, the compiler is fooled to belive that Thank, you,... are instructions [keywords/variables] which is actually wrong.
change
cout << Thank you for playing!<< endl;
to
cout <<"Thank you for playing!"<< endl;
Thumb Rule:
Always try to look into the information provided by the compiler during error/warning meessage. Here, the line number 58. It is really helpful to pinpoint the erroneous instructuion.
EDIT:
Please do not edit the current qusetion and add a completely new query. Reatain the previous version and metion the EDIT. Otherwise, it makes the previous answers irrelevant.
You're missing quotes in cout << Thank you for playing!<< endl;.
cout << "Thank you for playing!" << endl;
Also, your logic for testing repeat == 'n' or 'N' is wrong:
You should use an if statement, not a while loop.
You should quit the program only if the user enters 'N' or 'n'.
To notice the second error, it is helpful to use a standard indentation style.
Use Double qoutes (") while printing a string..
cout <<"Thank you for playing!"<< endl;
And One more thing as suggested by #irrelephant in his answer
use
if(repeat == 'n' || repeat == 'N') //Use if condition
{
cout << Thank you for playing!<< endl;
return 0; // exit only when user enter `n` or `N`
}
If you use return 0; outside then no matter what user has entered Y or N, program will terminate.
Related
I'm working on a minigame in C++ lately. The goal is to write a little game where you have to guesse a number. If you do so you'll get a point (I call it hit there) and if you don't you'll get a "miss". Logically speaking I don't want the game to go for ever. So I was trying to use a while loop to define at which scores you can still play. How you will be able to see in the code there are two conditions. Here is why I asked you: As long as there are two conditions it just ignores these so the game turns into an endless game. I don't recive any error-messages from VS2019. When I only try it w/ one condition it works just fine.
Here's the code:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
cout << "Welcom to the Hit-or-Miss-minigame. Here are the rules:" << endl;
cout << "You have to guess the same number as the computer. The numbers are within 0 and 10(both are still included)." << endl;
cout << "If you do so, you'll get a 'HIT' but if you don't you'll get a 'MISS'. When you reach 10 'HIT's you win" << endl;
cout << "but if you get a 'MISS' 15 times you'll lose." << endl;
char rep = 'y';
while (rep == 'y')
{
int hits = 0;
int miss = 0;
while ((hits < 3||miss < 15)) //Somehow doesn't work. So why?
{
int input_number;
srand(time(NULL));
int random_number = rand() % 11;
cout << "Your number: ";
cin >> input_number;
if (input_number == random_number)
{
cout << "HIT" << endl;
hits += 1;
}
else if (input_number != random_number)
{
cout << "MISS" << endl;
miss += 1;
}
else if ((input_number > 10) || (input_number < 0))
{
cout << "That was not an option";
}
else
{
cout << "That's not supposed to happen." << endl;
}
}
if (hits == 10)
{
cout << "You've won! Do you want to play another round?(y/n)" << endl;
cin >> rep;
}
else if (miss == 15)
{
cout << "You lose! Do you want to play another round?(y/n)" << endl;
cin >> rep;
}
}
}
I really would appreciate any help. Thanks!
EDIT: Problem solved. THANK YOU GUYS!
if you want the game will end after 3 hits or 15 misses you should use the && operator and not the || operator
it is because the || operator will return true when at least one of the conditions true, the && operator will return true when both of the true
Like the other comment said you should use && in your while loop, because you can have 16 misses and 3 hits before the loop breaks(for example 2 < 3 || 25 < 15 returns true and is only false when you get 3 < 3 || 25 < 15), which won't enter any if below the while, and it will just reset the variables back to 0 (this makes the while infinite). Furthermore if you put && in the while you need to change the if statement for hits to hits == 3 or it will never happen.
Also as a side note your if statement for numbers below zero and bigger than 10 needs to be above the one where you check if the guessed number is a miss (because every number bigger than 10 and smaller than 0 is a miss).
Hope this helps
I am compleatly new to coding and my first project is to take something already made and change it in several ways to teach myself the basics. I have chosen to use FizzBuzz, where on multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
cout << "FizzBuzz" << endl;
else
if (i % 3 == 0)
cout << "Fizz" << endl;
else
if (i % 5 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
system("sleep");
return 0;
}
What I am trying to do is: instead of the program printing "Fizz" on multiples of three it does it on multiple of 4 and it prints "Buzz" for multiples of 6 instead of 5 when I press a key
I am struggling on figuring out how to change the code on a key press. Thank you.
I'm new to stackoverflow, and also somewhat new to programming, so please don't mind my poor formatting of the code. I have two problems with my code.
My continue statement, which I'm using to continue the loop if the player types 'y' or 'Y', doesn't work. It terminates the program after only getting the guess correctly, which leads me to:
2.My continue counter goes past 0 without stopping, and I just can't see my error in the logic of the program.
I can't see the problems with my logic.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <random>
using namespace std;
int getNumber(); //random number prototype
double getScore(); //gets score
int chances = 7; //chances to guess with
int main()
{
int guess = 0,
random;
char retry = 'y'; //initialize retry to 'y'
cout << "This is a random number guessing game. " << "You will be guessing between 1-100."
<< "You have 7 chances. Good luck! \n \n" << endl;
random = getNumber(); //give the function a variable
do
{
cout << random << "\n" << "\n";
chances--;
cout << "Enter your guess: ";
cin >> guess;
if (guess == random)
{
cout << "You have won the game! " << "Your score was: " << getScore();
cout << "Would you like to retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue; //player can retry the game
}
else if (chances == 0)
{
cout << "You have no chances left. Retry? (Y or N): ";
cin >> retry;
if (retry == 'y' || retry == 'Y')
{
chances = 7;
guess = 0;
getNumber();
continue;
}
}
return 0;
}
else if (guess != random)
cout << "You got it wrong. \n" << "You have: " << chances << " chances left" << endl << endl;
else
cout << "Incorrect Input. Please type a number." << endl << endl;
} while (guess != random);
return 0;
}
int getNumber()
{
unsigned seed = time(0); //seed the random number
srand(seed);
int randNum = rand() % 10 + 1; //random number in the range of 1-10
return randNum;
}
if (retry == 'y' || 'Y')
This is incorrect logic, which is why your code does not work the way you want it to. You want it to be:
if (retry == 'y' || retry == 'Y')
Fix this logic error in your other if-else statements as well.
You'll wanna take a look at this
Your continue statement is jumping to the end and checking the condition, guess != random, which evaluates to false and exits the do while. What you need to do is reset guess to a value such as 0 so that the condition does evaluate to true.
This program will play a game with the user, called Odds and Evens. The computer will play Evens, and the human user will play Odds. For a round of the game, each player picks an integer in the range [1,10]. The players pick their numbers independently: neither player knows the other player's number before choosing its own number. If the sum of the numbers is even, then Evens (the computer) wins that round; if the sum of the numbers is odd, then Odds (the human) wins that round. The game continues for as many rounds as the user want to play; the user ends the game by typing a non-# or a number outside [1,10] for the input. At the end of the game, the program summarizes the score.
I am having trouble properly looping this question. Randomizing the number pc chooses is not working as every round in the game the pc chooses the same number. Also i do not know how I would have the program summarize the score. Help would be much appreciated as I have another problem for homework that is similar to this!
Here is my code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
bool die(const string & msg);
int main(){
srand(static_cast<unsigned>(time(0)));
unsigned num1 = 0, num = 0, sum = 0;
bool userTurn = true;
cout << "Welcome to the Odds and Evens game!";
num = rand() % 10 + 1;
while (num){
if (userTurn){
cout << " Your # in [1,10] is ";
cin >> num1;
}
else {
cout << "My number is " << num;
sum = num1 + num;
if (sum % 2 == 0){
cout << " I win!";
}
else {
cout << " You win!";
}
}
userTurn = !userTurn;
}
}
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
Randomizing the number pc chooses is not working as every round in the game the pc chooses the same number.
You don't have code to re-set the value of num when it's the computer's turn.
After the line
userTurn = !userTurn;
add
if ( !userTurn )
{
num = rand() % 10 + 1;
}
Also i do not know how I would have the program summarize the score.
Keep two counters that indicate how many times the human won and how many times the computer won.
int computerWinCount = 0;
int humanWinCount = 0;
and then, update the loop to use:
if (sum % 2 == 0){
cout << " I win!";
++computerWinCount;
}
else {
cout << " You win!";
++humanWinCount;
}
The conditional of the while loop is such that your program will never terminate. Update it to something like below.
while (true) {
if (userTurn){
cout << " Your # in [1,10] is ";
cin >> num1;
// If the user entered a number that is not
// within range or the user did not input a number,
// then break out of the loop.
if ( !cin || num1 < 1 || num1 > 10 )
{
break;
}
}
else {
cout << "My number is " << num;
sum = num1 + num;
if (sum % 2 == 0){
cout << " I win!" << endl;
++computerWinCount;
}
else {
cout << " You win!" << endl;
++humanWinCount;
}
}
userTurn = !userTurn;
if ( !userTurn )
{
num = rand() % 10 + 1;
}
}
To report the summary, add the following lines before the end of the main.
cout << "Number of times I won: " << computerWinCount << endl;
cout << "Number of times you won: " << humanWinCount << endl;
Here:
num = rand() % 10 + 1;
while (num){
... // never change num
}
Do you see the problem? The computer player chooses num randomly, but only once. Just put another num = rand() % 10 + 1; inside the main loop.
(Also, you don't seem to have a way for the user to terminate the game.)
So you want a simple loop that will do the following things.
get the user input.
get the computer input
check to see who win's the current round
update scores.
this happens until the user chooses an option not from 1 to 10
after this you want to display the score.
Here is a complete example.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int mynum, compNum, myScore(0), compScore(0);
srand(time(NULL));
cout << "Welcome to the Odds and Evens game!" << endl;
cout << "Your # in [1,10] is ";
while ((cin >> mynum) && mynum > 0 && mynum <= 10){
compNum = rand()%10 + 1;
if ((mynum + compNum)%2){
cout << "You win" << endl;
++myScore;
} else {
cout << "Computer Wins" << endl;
++compScore;
}
cout << "Your # in [1,10] is ";
}
cout << "You won " << myScore << " games" << endl;
cout << "The computer won " << compScore << " games" << endl;
return 0;
}
Your problem with the computer's number not changing is due to the fact you do not update its value within the loop.
If you want to keep track of the score, you can simply keep two integers that keep track of how many times the user has won and how many times the computer has won. Then at the end (after the while loop) cout each of their scores.
Overall your code is pretty close.
You just need to make sure you update the computer's guess inside the while loop and when you decide who's won the round increment that person's score.
The whole loop condition in your original code will always evaluate to true. As num will always be to a number 1 to 10. You'll want to use the user's input in the while loop condition.
The while condition in my code will do the following:
get the user's input. cin >> mynum will evaluate to false if cin fails to read a number. If it did read a number the condition will check to see if the number is between 1 and 10 inclusive.
In this case, how would I solve this? I've looked at other posts with the same problem and I can't seem to apply it to this. I've solved this problem before but for some reason I can't remember how I did it.
#include <iostream>
#include <cstdlib>
#include <Windows.h>
#include<ctime>
using namespace std;
//Tristan Currie 11/20/14
//Dice Game
int main()
{
//variables
int die1, die2, dice_total, specialsum;
char rerun, reroll;
//intro
cout << "Welcome to the dice game! I will now explain the rules. \nIf you land on a 7 or 11 on the first role you win. \nIf your sum is 2, 3, or 12 you lose. \nAny other role becomes your special sum. If you roll your special sum before you roll a 7 then you win. \nIf when you roll you get a 7 before your special sum then you lose. ";
cout << "\n\nIf you would like to start the game, press enter to do your first roll. ";
cin.get();
cout << "\n\nRolling Dice...";
//Suspend program for 2 seconds
Sleep(2000);
//seed random number generator using the system clock
srand(static_cast<unsigned int>(time(0)));
//generate a random number between 1 and 6
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice_total = die1 + die2;
cout << "Done!" << endl << "Dice #1 = " << die1 << "\nDice #2 = " << die2 << "\nDice Total = " << dice_total;
cin.get();
if ((dice_total == 7) || (dice_total == 11)) {
cout << "Congratulations! You have won the game, press enter to end the program. ";
cin.get();
reroll = 'n';
}
else if ((dice_total == 2) || (dice_total == 3) || (dice_total == 12)) {
cout << "You lost. Press enter to exit. ";
cin.get();
reroll = 'n';
}
else if ((dice_total != 2) || (dice_total != 3) || (dice_total != 12) || (dice_total != 7) || (dice_total != 11)) {
cout << "This is your special sum: " << dice_total << endl;
dice_total = specialsum;
reroll = 'y';
}
while (reroll == 'y') {
cout << "\n\nRolling Dice...";
//Suspend program for 2 seconds
Sleep(2000);
//seed random number generator using the system clock
srand(static_cast<unsigned int>(time(0)));
//generate a random number between 1 and 6
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice_total = die1 + die2;
cout << "Done!" << endl << "Dice #1 = " << die1 << "\nDice #2 = " << die2 << "\nDice Total = " << dice_total;
cin.get();
if (dice_total == specialsum) {
cout << "Congratulations! You have won the game, press enter to end the program. ";
cin >> rerun;
cin.get();
reroll = 'n';
}
else if (dice_total == 7) {
cout << "What a shame, you have lost the game, press enter to exit the gane. ";
cin >> rerun;
cin.get();
reroll = 'n';
}
else {
reroll = 'y';
}
}
}
Well, I got an answer in the comments. I mixed up the order of this line: dice_total = specialsum;
int specialsum = 0;
Initializing integer variables to zero, or to a known value. Defining your variable like this, "int specialsum;" ,it gets lost.
Also try specialsum= dice_total; instead of dice_total=specialsum; because "dice_total" would be equal to zero all the time. Hope this helped.