In the below code, I've got some bugs and I don't know why it doesn't work.
I used a fmod to do the decimals but I got some bugs in the part where it gives you the 0.10 and 0.05 $ and most of the time I never get the good amount that I'm supposed to give back if there is.
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
int iAmount_due = { 0 };
int iGiven_money = { 0 };
int iMoney_back;
iMoney_back = iGiven_money - iAmount_due;
cout << "Enter the amount due please: " << endl;
cin >> iAmount_due;
cout << "Enter the amount given please: " << endl;
cin >> iGiven_money;
if (iGiven_money >= iAmount_due) {
iMoney_back = iGiven_money - iAmount_due;
cout << "We will give you : " << iMoney_back << " $ back" << endl;
}
else {
cout << "No money back" << endl;
}
if (iMoney_back >= 100) {
cout << "You will receive: " << iMoney_back % 100 << " x 100$" << endl;
}
else {
cout << "No 100$ bill" << endl;
}
if (iMoney_back >= 50) {
cout << "You will receive: " << iMoney_back % 50 << " x 50$" << endl;
}
else {
cout << "No 50$ bill" << endl;
}
if (iMoney_back >= 20) {
cout << "You will receive: " << iMoney_back % 20 << " x 20$" << endl;
}
else {
cout << "No 20$ bill" << endl;
}
if (iMoney_back >= 10) {
cout << "You will receive: " << iMoney_back % 10 << " x 10$" << endl;
}
else {
cout << "No 10$ bill" << endl;
}
if (iMoney_back >= 5) {
cout << "You will receive: " << iMoney_back % 5 << " x 5$" << endl;
}
else {
cout << "No 5$ bill" << endl;
}
if (iMoney_back >= 0.25) {
cout << "You will receive: " << fmod (iMoney_back, 0.25) << " x 0.25$ " << endl;
}
else {
cout << "No 0.25$ " << endl;
}
if (iMoney_back >= 0.10) {
cout << "You will receive: " << fmod (iMoney_back, 0.10) << " x 0.10$ " << endl;
}
else {
cout << "No 0.10$ " << endl;
}
if (iMoney_back >= 0.05) {
cout << "You will receive: " << fmod (iMoney_back, 0.05) << " x 0.05$ " << endl;
}
else {
cout << "No 0.05$ " << endl;
}
return 0;
}
int holds only integer values. 0.05 is not an integer value. You might be tempted to use double values instead that probably work for your simple problem; however, they generally have rounding errors which is something you really do not want to have with currency values.
The better solution will be to still use integers. You would need to define 1 as the smallest possible unit (e.g. 1 cent), meaning that all your values will be in cent. You would need to write extra code to be able to enter and display values as dollars instead of cents.
Related
So i have to code the dice game LCR for a final prject but am having some trouble. First off, I know the code is sloppy and really redundant, that's why im looking for help. I couldn't figure out how to connect the 'chips' int vector and 'name' array into the player.h file. I basically need help writing methods for the chip passing to make the code less redundant. But another problem of mine is having the game loop until just one person has chips. Thanks for any help or advice.
LeftCenterRight.cpp
#include <iostream>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
#include <vector>
#include <algorithm>
using namespace std;
void Player::gameRules()
{
cout << ("\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
cout << ("Left Center Right is a multiplayer dice game");
cout << ("With a minimum of three players.");
cout << ("Dice containing letters L, C, R along\n"
"with dots on the remaining side are rolled each turn");
cout << ("\n-----Each Player starts with three chips-----\n");
cout << (">> For each L rolled, the player must pass one chip to the player to their left\n"
">> For each R rolled, the player must pass one chip to the player to their right\n"
">> For each C rolled, the player must pass a chip into the center pot (out of play)\n"
">> Dots are neutral and require no action");
cout << ("If a player has three or more chips, he/she rolls all three dice\n"
"If a player only has two chips, he/she rolles onlt two dice\n"
"If a player only has one chip, he/she rolls only one die\n"
"If a player is out of chips, he/she is still in the game,\n"
"\tbut does not roll any dice and passes their turn"
"\n\n >>> The last player with chips is the winner <<<");
cout << ("\n\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n");
};
int main()
{
int result = 0;
int currPlayer = 1;
srand((unsigned)time(NULL));
int numPlayers;
cout << "How many players are playing? (Please enter 3 or more): " << endl;
cin >> numPlayers;
if (numPlayers <= 2)
{
while (numPlayers <= 2)
{
cout << "More players needed.";
cout << "How many players are player?: ";
cin >> numPlayers;
}
}
std::string* names = new string[numPlayers]; // getting names and index is seat number
for (int i = 0; i < numPlayers; i++)
{
cout << "Please enter your name player " << i+1 << endl;
cin >> names[i];
std::string playerName = names[i];
}
vector<int>chips[1]; // intial 3 chips
for (int i = 0; i < numPlayers; i++)
{
int InitialChips = 3;
chips->push_back(InitialChips);
}
Player::gameRules();
int sum = 0;
for (int i = 0; i < chips->size(); i++)
{
int num = chips->at(i);
sum+=num;
}
int thePot = 0;
int i = 0;
while (sum > 0)
{
if ( i >=4 )
{
i = 0;
}
string currPlayer = names[i];
int currSeat = i;
cout << "It's " << currPlayer << "'s Turn!" << endl;
cout << "[1] Roll Dice [2] Quit :";
int choice;
cin >> choice;
if (choice == 1)
{
if (chips->at(currSeat) == 0)
{
break;
}
if (chips->at(currSeat) >= 3)
{
for (int k = 0; k <= 3; k++)
{
int outcome = Dice::rollDice();
if (outcome == 1)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == 0)
{
int j = (numPlayers - 1);
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i - 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 2)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == chips->size())
{
int j = chips->at(0);
int currChips2 = chips->at(0);
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i + 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 3)
{
thePot++;
cout << ">> +1 chip to the Center Pot" << endl;
cout << "There are now " << thePot << " chip(s) in the Center Pot " << endl;
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
break;
}
else if ((outcome == 4) || (outcome == 5) || (outcome == 6))
{
break;
}
}
}
// ^^basically copied and pasted most of the while loop for the other two numbers of dice to roll^^
// had redundant code for if the player had 2 chips, to roll two dice only ^^
// also redundant code for only one chip, to roll one die. ^^
}
}
else if (choice == 2)
{
break;
}
else
{
cout << ">> Input Error";
cout << "[1] Roll Dice [2] Quit";
cin >> choice;
}
i++;
}
return 0;
}
Dice.h
#pragma once
using namespace std;
class Dice
{
public:
static int rollDice();
static int diceOutcome;
};
int Dice::rollDice()
{
int diceOutcome;
diceOutcome = (rand() % 6) + 1;
switch (diceOutcome)
{
default:
cout << "Error, retry";
case 1:
if (diceOutcome == 1)
{
cout << " --- " << endl;
cout << "You rolled a | L | Move 1 chip left." << endl;
cout << " --- " << endl;
return 1;
}
break;
case 2:
if (diceOutcome == 2)
{
cout << " --- " << endl;
cout << "You rolled a | R | Move 1 chip right." << endl;
cout << " --- " << endl;
return 2;
}
break;
case 3:
if (diceOutcome == 3)
{
cout << " --- " << endl;
cout << "You rolled a | C | Move 1 chip to the center." << endl;
cout << " --- " << endl;
return 3;
}
break;
case 4:
if (diceOutcome == 4)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 5:
if (diceOutcome == 5)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 6:
if (diceOutcome == 6)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
}
}
To be fair, I'm quite new to programming, but I have been working on this project for days and kept running into conatiner problems and storing the name and chip values independently. So i've tried a lot of different things, probably not correctly though so I'm open to anything.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
So I'm new to coding and I'm doing this for a class project. How do I make it where the while loop stops if it's <= to 0. If I put <= to 0 it doesn't do the loop at all and if I put it !=
it works, but it sometimes goes negative resulting in the loop never stopping. Is there something I'm missing? I have no idea why <= 0 wouldn't work
int main()
{
while (health != 0 && enemy_health != 0) {
std::cout << "Player " " Health: " << health << " Level: " << level << "\n";
std::cout << "Inventory " << "Gold: " << gold << " Keys: " << keys << " Health Potions: " << health_potions << "\n\n";
std::cout << "Fist: " << fist_damage << " damage " << " Torch: " << torch_damage << " damage \n" << " Status Effects:\n\n" <<" Burn: " << burn_damage << " damage\n\n";
std::cout << "Use: ";
std::cin >> input;
if (input == Fist) {
std::cout << "\nYou did " << fist_damage << " damage\n\n";
std::cout << "Monsters Health: " << enemy_health << "\n";
playerAttack = true;
}
else if (input == Torch) {
std::cout << "\nYou did " << torch_damage << " damage\n" << "You did " << burn_damage << " burn damage\n\n";
int total = enemy_health - (torch_damage + burn_damage);
enemy_health = total;
std::cout << "Monsters Health: " << enemy_health;
torch_durability--;
playerAttack = true;
isBurning = true;
}
while (playerAttack != true) {
std::cout << "\n\nPlease enter a valid answer\n\n";
std::cout << "Use: ";
std::cin >> input;
if (input == Fist) {
std::cout << "\nYou did " << fist_damage << " damage\n\n";
std::cout << "Monsters Health: " << enemy_health << "\n";
playerAttack = true;
}
else if (input == Torch) {
std::cout << "\nYou did " << torch_damage << " damage\n" << "You did " << burn_damage << " burn damage\n\n";
int total = enemy_health - (torch_damage + burn_damage);
enemy_health = total;
std::cout << "Monsters Health: " << enemy_health;
torch_durability--;
playerAttack = true;
isBurning = true;
}
}
if (playerAttack == true) {
std::cout << "\n\nThe enemy attacks\n\n";
}
if (dodge_chance == 5) {
std::cout << "\n\nYou dodged the enemies attack\n\n";
}
else if (dodge_chance != 5) {
std::cout << "The enemy landing a crushing blow\n\n";
playerHit = enemy1_damage;
int health_real = health - playerHit;
health = health_real;
std::cout << "The enemy does " << playerHit << " damage\n\n";
}
}
}
When asking questions or just for debugging, it is a good idea to isolate the part that is giving you trouble. Most of the code you have posted is irrelevant and makes it harder to read and test.
From what I understand, you have tried putting
while (health <= 0 && enemy_health <= 0)
instead of your current loop ?
If so, then it is simply a logic error. You are asking the code to loop while player health is negative or zero. Simply changing to
while (health > 0 && enemy_health > 0)
would work.
I have a basic menu program that gives the user different programs to choose from and run, when I run the simpleCalculator option and iput values and the operation of arithmetic the output is always the wrong answer but when I first programmed the calculator program without the menu and it being in its own function it would run just fine and give the the right answers, what exactly is going wrong here?
Ignore the third option in the menu since i haven't added code for the prime number checker program
double first_arg;
double second_arg;
string arithmeticOperation;
int numBottles = 99;
double simpleCalculator()
{
cout << "Enter first value: ";
cin >> first_arg;
cout << "Enter choice of arithmetic operation:\n* = multiplication\n/ = division\n+ = addition\n- = subtraction\n\n";
cin >> arithmeticOperation;
cout << "\nEnter second value: ";
cin >> second_arg;
if(arithmeticOperation == "*")
{
cout << "\nYour answer is " << first_arg * second_arg;
}
else if(arithmeticOperation == "/")
{
cout << "\nYour answer is " << first_arg / second_arg;
}
else if(arithmeticOperation == "+")
{
cout << "\nYour answer is " << first_arg + second_arg;
}
else if(arithmeticOperation == "-")
{
cout << "\nYour answer is " << first_arg - second_arg;
}
else
{
cout << "\nPlease enter a valid choice of an arithmetic operation\n";
}
}
string bottlesProgram()
{
while(true)
{
if (numBottles > 2)
{
cout << numBottles << " bottles of beer on the wall\n" << numBottles << " bottles of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottles of beer on the wall.";
cout << "\n\n";
}
else if(numBottles == 2)
{
cout << numBottles << " bottles of beer on the wall\n" << numBottles << " bottles of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottle of beer on the wall.";
cout << "\n\n";
}
else if(numBottles == 1)
{
cout << numBottles << " bottle of beer on the wall\n" << numBottles << " bottle of beer!\n" << "Take one down\nPass it around\n" << numBottles - 1 << " bottles of beer on the wall.";
cout << "\n\n";
}
else if(numBottles == 0)
{
cout << "No more bottles of beer on the wall,\nNo more bottles of beer.\nGo to the store and buy some more,\n" << "99 bottles of beer on the wall...";
cout << "\n\n";
}
else
{
break;
}
numBottles--;
}
}
int main()
{
int menuInput;
cout << " ***Programs Menu***\n" << "------------------------------------------\n" << "1. Run a simple Calculator\n" << "2. Run 99 bottles of beer on the wall song\n" << "3. Run prime number checker program\n" << "------------------------------------------\n";
cin >> menuInput;
if(menuInput == 1)
{
cout << simpleCalculator();
}
else if (menuInput == 2)
{
cout << bottlesProgram();
}
}
You seem to be confused about returning values from a function, and printing values in a function. These are not the same thing.
If you print the values in the function then the function should be void (i.e. nothing is returned) and the printing happens inside the function (obviously)
void simpleCalculator()
{
...
cout << "\nYour answer is " << first_arg * second_arg;
...
}
simpleCalculator(); // no cout
On the other hand if you return a value from the function then the function is not void and the printing happens outside the function
double simpleCalculator()
{
...
return first_arg * second_arg; // return not cout
...
}
cout << "\nYour answer is " << simpleCalculator();
Your code is doing half one version and half the other.
I can't figure out how to make my code go to the next else if statement if my user input satisfies the previous if state.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
in your code if input > 60 it will satisfy condition and will not execute else part so first check wether input > 86400 if not then check if input > 36000 if not then check for input > 60
try below code in which if conditions are reversed
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
return 0;
}
Make it other way round.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " minutes." << endl;
}
else
{
cout << "The time is " << input << " seconds." << endl;
}
return 0;
}
Hope this is what you want!
There are multiple ways to do this, you can implement a check between 2 values using conditions like >= val1 and < val2 or ensure that the order in which the checks happen is different.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
or another way would be to change the order, once the particular if statement is valid, it doesn't check with the rest of the if statements.
#include <iostream>
using namespace std;
int main()
{
double input;
cout << "Time Calculator\n Enter the number of Seconds: " << endl;
cin >> input;
if (input >= 86400)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 3600)
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 60)
{
cout << "The time is " << input << " hours." << endl;
}
else if (input < 60)
{
cout << "The time is " << input << " days." << endl;
}
return 0;
}
Your if else conditions are not completely defined... you have to set those in a range... otherwise is not going to work...
if (input < 60)
{
cout << "The time is " << input << " seconds." << endl;
}
else if (input >= 60 && input < 3600))
{
cout << "The time is " << input << " minutes." << endl;
}
else if (input >= 3600 && input < 86400))
{
cout << "The time is " << input << " hours." << endl;
}
else if (input >= 86400)
{
cout << "The time is " << input << " days." << endl;
}
My question is this, how do I save an int value in a while loop, my code is all about gambling, you start with 1,000 and you want to make the most amount of cash, but when I roll again my cash restores back to its original value that I set.
My code is this (Note I am new so do not laugh at how bad it is)
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
char again = 'Y';
int test;
int yes;
int CashW;
CashW = 1000;
int CashL;
CashL = 1000;
int yLose;
yLose = 500;
int xCash;
xCash = 1000;
int xRan;
srand(time(0));
xRan = rand() % 100 + 1;
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you lose!" << endl;
while (again == 'y' || again == 'Y')
{
cout << " The Number I Choose Is: " << xRan << endl;
CashL = xCash - xCash - xCash;
CashW = xCash + xCash;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashW << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashL << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (yes == 1) {
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
}
}
In your code you currently display either CashW or CashL depending on the gampbling result.
Unfortunately, you only print out the result and never store it into xCash. So at next iteration you start again with the same xCash value !
You can easily solve this by adding xCash = CashW; or xCash = CashL; just under the line in which you display the result.
You are never updating xCash with the amount of each win/loss. You are not generating a new random number on each loop iteration. And you are stuck in an endless loop because you never update the loop variable again.
Try something more like this instead:
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
const int CashW = 1000;
const int CashL = 1000;
int xCash = 1000;
int xRan;
char answer;
srand(time(0));
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you win!" << endl;
do
{
xRan = rand() % 100 + 1;
cout << " The Number I Choose Is: " << xRan << endl;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash += CashW;
cout << " The cash you have now is: " << xCash << endl;
}
else if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash -= CashL;
cout << " The cash you have now is: " << xCash << endl;
}
else {
cout << " dang, a draw!" << endl;
}
cout << " play again? " << endl;
cin >> answer;
if ((answer != 'y') && (answer != 'Y')) {
cout << " All done? Come back again another time!" << endl;
break;
}
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
while (true);
return 0;
}