I'm currently in a class that wants me to make a craps game.
The problem is in int main on the second while statement comparing the point with the roll. It ignores the if statement and does the loop again, even though it hits the point or 7. Sometimes it works like it should and other times it repeats the loop a few times.
#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int diceRoll() {
int x = 0, y = 0;
x = rand() % 6 + 1;
y = rand() % 6 + 1;
cout << "You rolled a " << x << " and a " << y << " which comes out to ------> " << x + y << " <-------" << endl;
return x + y;
}
bool playAgain() {
char ans;
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
while (ans != 'Y' || ans != 'y' || ans != 'n' || ans != 'N') {
if (ans == 'Y' || ans == 'y')
{
return true;
}
if (ans == 'N' || ans == 'n')
{
return false;
}
cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
cin >> ans;
}
}
int main()
{
srand(time(NULL));
int dices, bid, point = 0;
int money = 50;
bool gameRunning = true;
bool didTheyWin;
while (gameRunning == true) {
if (money == 0) {
cout << "You have no money, ending game." << endl;
break;
}
cout << "Please enter a bid. You currently have $" << money << endl;
cout << "$";
cin >> bid;
while (bid > money) {
cout << "Please bet below your current balance: $" << money << endl;
cout << "$";
cin >> bid;
}
dices = diceRoll();
didTheyWin = false;
if ((dices == 7) || (dices == 11)) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
}
else if ((dices == 2) || (dices == 3) || (dices == 12)) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
}
else {
point = dices;
cout << "The target number is > " << point << " <" << endl;
cout << "If you hit a 7 you lose, if you hit the target you win. \nYou automatically roll until one of these two things happen.\n";
while (didTheyWin == false) {
diceRoll();
dices = diceRoll();
if (dices == point) {
cout << "You won $" << bid << " !" << endl;
money = money + bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
else if (dices == 7) {
cout << "You LOSE! You lost $" << bid << " !" << endl;
money = money - bid;
cout << "You now have $" << money << endl;
didTheyWin = true;
}
}
}
gameRunning = playAgain();
}
cout << "Thanks for playing. _END_" << endl;
return 0;
}
You call diceRoll twice, and ignore what you get back from the first call. You'll see the results of that first roll displayed, but they'll be ignored and you'll roll again.
Related
I've been working on my introduction to C++ project which is to make a game of NIM. I've written program and I thought it should run smoothly but I've countered the following errors.
I know too much code but I couldn't figure out what is the issue that is stopping me from running the program.
I'm new to the programming word so be understanding if the issue is very simple.
thank you in advance.
Error Message
#include <iostream>
#include <iomanip>
using namespace std;
int stonesNum;
char goFirst;
int turn = 0; // 0 = player, 1 = computer
int stonesRemove;
int main() {
do
{
cout << "Enter number of starting stones:" << endl;
cin >> stonesNum;
cout << "Would you like to go first? (y/n)" << endl;
cin >> goFirst;
if (goFirst == 'y' || goFirst == 'Y')
{
turn = 0;
}
else
{
turn = 1;
}
while (stonesNum > 0)
{
if (turn == 0)
{
cout << "How many would you like to remove: 1 or 2?" << endl;
cin >> stonesRemove;
stonesNum = stonesNum - 1;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
if (stonesNum % 3 == 0)
{
stonesNum = stonesNum - 2;
cout << "The computer removes 2 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
stonesNum = stonesNum - 1;
cout << "The computer removes 1 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
}
if (stonesNum == 0)
{
if (turn == 0)
{
cout << "The computer wins!" << endl;
}
else
{
cout << "you won!" << endl;
}
}
}
}
return 0;
}
You are missing an ending while condition after your do loop. The syntax is :
do{
//do stuff here
}while(condition);
In your case the condition would be (stonesNum!=0)
OR
Do
if (stonesNum == 0)
{
if (turn == 0)
{
cout << "The computer wins!" << endl;
return;
}
else
{
cout << "you won!" << endl;
return;
}
}
and put condition (true)
You actually don't require the do loop at all, are you trying to loop the game endlessly? If so then you should put an appropriate prompt at the beginning and put condition (true) in the do loop.
The complete code:
do
{
cout << "Enter number of starting stones:" << endl;
cin >> stonesNum;
cout << "Would you like to go first? (y/n)" << endl;
cin >> goFirst;
if (goFirst == 'y' || goFirst == 'Y')
{
turn = 0;
}
else
{
turn = 1;
}
while (stonesNum > 0)
{
if (turn == 0)
{
cout << "How many would you like to remove: 1 or 2?" << endl;
cin >> stonesRemove;
stonesNum = stonesNum - 1;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
if (stonesNum % 3 == 0)
{
stonesNum = stonesNum - 2;
cout << "The computer removes 2 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
else
{
stonesNum = stonesNum - 1;
cout << "The computer removes 1 stones." << endl;
cout << "The number of stones left is " << stonesNum << endl;
}
}
}
if (turn == 0)
{
cout << "The computer wins!" << endl;
}
else
{
cout << "you won!" << endl;
}
}while(true);
So I have an issue with my code where I cannot get either of my functions (int human_turn, or int computer_turn) to return the correct value of their turn totals. What I am trying to do is have the two int functions return their respective turn totals after they are done with their turn, then have a separate function that will cout these values and will act as the scoreboard and also show if the win conditions are met. Currently at the end of the while loop for each ones turn, I have turnTotal_1 = roll_1 + turnTotal_1; which updates the total and then the loop checks to see if the while is still true. I then put a return turnTotal_1; statement after the loop so it would return this value, and it will always return 1. No matter what the value of the turn total is, the return statement will always return 1. any help or advice would be appreciated.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
using namespace std;
/**
* rollDie
* returns a random integer between 1 and 6, works as rolling a dice.
* return value, int number (1-6)
*/
int rollDie()
{
return random() % 6 + 1;
}
int human_turn(int turnTotal_1)
{
cout << "It is now human's turn" << endl;
cout << "Do you want to roll a dice(Y/N)?:" << endl;
char user_choice;
cin >> user_choice;
while ((user_choice == 'y') || (user_choice == 'Y'))
{
int roll_1 = rollDie();
if ((roll_1 == 2) || (roll_1 == 5))
{
cout << "You rolled a " << roll_1 << endl;
roll_1 = 0;
}
else if ((roll_1 == 1) || (roll_1== 3) ||(roll_1 == 6))
{
cout << "You rolled a " << roll_1 << endl;
}
else if (roll_1 == 4)
{
cout << "You rolled a " << roll_1 << endl;
roll_1 = 15;
}
turnTotal_1 = roll_1 + turnTotal_1;
cout << "Your turn total is " << turnTotal_1 << endl;
cout << "Do you want to roll a dice(Y/N)?:" << endl;
cin >> user_choice;
}
if ((user_choice == 'n') || (user_choice == 'N'))
{
return turnTotal_1;
}
}
int computer_turn()
{
int turnTotal_2 = 0;
cout << "It is now computer's turn" << endl;
int roll_1 = rollDie();
while (turnTotal_2 <= 10)
{
int roll_1 = rollDie();
if ((roll_1 == 2) || (roll_1 == 5))
{
cout << "Computer rolled a " << roll_1 << endl;
roll_1 = 0;
}
else if ((roll_1 == 1) || (roll_1== 3) ||(roll_1 == 6))
{
cout << "Computer rolled a " << roll_1 << endl;
}
else if (roll_1 == 4)
{
cout << "Computer rolled a " << roll_1 << endl;
roll_1 = 15;
}
turnTotal_2 = roll_1 + turnTotal_2;
cout << "Computer turn total is " << turnTotal_2 << endl;
}
}
void scoreboard()
{
cout << human_turn << endl;
cout << computer_turn << endl;
}
void game()
{
cout << "Welcome to Jeopardy Dice!" << endl;
human_turn(0);
cout << human_turn << endl;;
computer_turn();
scoreboard();
}
int main()
{
// start the game!
game();
return 0;
}
Here is an example of how it runs.
Welcome to Jeopardy Dice!
It is now human's turn
Do you want to roll a dice(Y/N)?:
n
1
It is now computer's turn
Computer rolled a 5
Computer turn total is 0
Computer rolled a 4
Computer turn total is 15
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 am stuck.
I have been given this problem: http://i.imgur.com/1U8PjY4.png?1
The code I've written so far is this:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
char cIn;
char full;
int capacity = 750;
int numVans = 1;
float heaviestVan = 0;
float payload = 0;
float parcelWeight;
bool emptyBelt = false;
int main()
{
bool end = false;
while(!end)
{
start:
cout << string(25, '\n');
cout << endl << "Pauls Premier Parcels (PPP)" << endl << endl;
cout << "Van being loaded is number: " << numVans << endl << endl;
cout << "The payload of van " << numVans << " is currently " << payload << " / 750kg" << endl << endl;
cout << "Is the belt full? ('Y' or 'N'): ";
cin >> full;
if (full == 'Y' || 'y')
{
while (!emptyBelt)
{
cout << endl << endl << "Please enter the weight of the next parcel: ";
cin >> parcelWeight;
if (parcelWeight > 120)
{
cout << "The maximum parcel weight is 120kg, please weigh a different parcel: ";
cin >> parcelWeight;
}
if (payload + parcelWeight <= capacity)
{
payload = payload + parcelWeight;
cout << endl << "The parcel has been loaded onto the van" << endl << endl;
goto start;
}
else
{
cout << endl << "The current van has reached capacity and is being dispatched" << endl;
//numVans = numVans + 1;
if(payload > heaviestVan)
{
heaviestVan = payload;
}
payload = 0;
cout << endl << endl << endl << "Vans dispatched: " << numVans;
cout << endl << endl << "Weight of heaviest van: " << heaviestVan;
}
}
}
}
return 0;
}
I need to implement a statement asking the user to place parcels on the belt if the belt is empty, right now It just continues running the program.
Also the user could enter anything besides Y or y and the program would still run.
Try rewriting
if (full == 'Y' || 'y')
to
if ((full == 'Y') || (full == 'y'))
Some explanation:
if (full == 'Y' || 'y')
is the same as
if ((full == 'Y') || ('y'))
which is the same as
if ((full == 'Y') || true)
which is the same as
if (true)
regardsless of the value of the variable full.
for some reason my subtotal variable doesn't store the prices of the items inputted by the user any suggestions? I don't know if I set up my loop wrong or if the subtotal has to be put outside the if else statements but then I don't know how I would know what to store from users input
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const double tax_rate = 0.05;
struct menuItemType
{
string ItemName;
double ItemPrice;
};
void getData(ifstream &in, menuItemType menuList[]);
void showMenu(menuItemType menuList[]);
void printCheck(menuItemType menuList[]);
int main()
{
menuItemType menuList[10];
menuList[10].ItemName;
menuList[10].ItemPrice;
ifstream in;
in.open("Menu.txt");
cout << "Welcome to Johnny's Breakfast Diner!" << endl;
getData(in,menuList);
showMenu(menuList);
return 0;
}
void getData(ifstream &in, menuItemType menuList[])
{
int i = 0;
while (!in.eof())
{
in >> menuList[i].ItemName >> menuList[i].ItemPrice;
i++;
}
}
void showMenu(menuItemType menuList[])
{
int j = 0;
char answ;
cout << fixed << setprecision(2);
cout << "Would you like to take a look at our menu? (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << left << setw(10) << "Item#" << left << setw(15) << "Item" << left << setw(18) << "Price" << endl;
do {
{
cout << left << setw(8) << j << " " << left << setw(15) << menuList[j].ItemName << " " << "$" << menuList[j].ItemPrice << endl;
j++;
}
} while (j < 8);
printCheck(menuList);
}
else if (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Have a good day!" << endl;
}
}
void printCheck(menuItemType menuList[])
{
char answ;
int choice;
bool menu = true;
double subtotal = 0;
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
cout << "Would like to place your order (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
cout << "Please enter the number of the item, 8 to finish order:";
do {
cin >> choice;
if (choice == 0)
{
cout << menuList[0].ItemName << " " << "$" << menuList[0].ItemPrice << endl;
subtotal = subtotal + menuList[0].ItemPrice; \\ for some reason here it doesn't store the prices have no idea why
}
else if (choice == 1)
{
cout << menuList[1].ItemName << " " << "$" << menuList[1].ItemPrice << endl;
subtotal = subtotal + menuList[1].ItemPrice;
}
else if (choice == 2)
{
cout << menuList[2].ItemName << " " << "$" << menuList[2].ItemPrice << endl;
subtotal = subtotal + menuList[2].ItemPrice;
}
else if (choice == 3)
{
cout << menuList[3].ItemName << " " << "$" << menuList[3].ItemPrice << endl;
subtotal = subtotal + menuList[3].ItemPrice;
}
else if (choice == 4)
{
cout << menuList[4].ItemName << " " << "$" << menuList[4].ItemPrice << endl;
subtotal = subtotal + menuList[4].ItemPrice;
}
else if (choice == 5)
{
cout << menuList[5].ItemName << " " << "$" << menuList[5].ItemPrice << endl;
subtotal = subtotal + menuList[5].ItemPrice;
}
else if (choice == 6)
{
cout << menuList[6].ItemName << " " << "$" << menuList[6].ItemPrice << endl;
subtotal = subtotal + menuList[6].ItemPrice;
}
else if (choice == 7)
{
cout << menuList[7].ItemName << " " << "$" << menuList[7].ItemPrice << endl;
subtotal = subtotal + menuList[7].ItemPrice;
}
else if (choice == 8)
{
break;
}
}while(menu = true);
cout << "Taxes" << "$" << tax << endl;
cout << "Amount Due" << "$" << total << endl;
}
else if (answ == 'N' || answ == 'n')
{
system("cls");
cout << "Ok, maybe I can help you at a later time." << endl;
}
}
It looks like you're trying to use subtotal before you've actually put data into it.
The problem is these lines:
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
At that point in the program, subtotal still contains the initial value, which is 0, so the result of those calculations is also 0. You need to put those lines after the loop so that they work with the final value of subtotal.
What's the result. the subtotal will have
menuList[choice].ItemPrice value
if you want to change
subtotal += menuList[choice].ItemPrice;