How to save int values in a while loop? - c++

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;
}

Related

C++ Left Center Right Dice Game (Game Loop and connecting vectors/arrays to classes/header files)

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.

Timer source code c++

Hi guys i found a timer source code for c++
Found it here:
https://www.daniweb.com/programming/software-development/code/216933/a-countdown-timer-in-c
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
using namespace std;
int main()
{
int m, s, h;
cout << "A COUNTDOWN TIMER " << endl;
cout << "enter time in hours here" << endl;
cin >> h;
cout << "enter time in minutes here " << endl;
cin >> m;
cout << "enter im in seconds here" << endl;
cin >> s;
cout << "Press any key to start" << endl;
cout << " A COUNTDOWN TIMER" << endl;
cout << "time remaining" << endl;
cout << "hours : " << h << "mins : " << m << " secs : " << s << endl;
for (int hour = h; hour >= 0; hour--)
{
for (int min = m; min >= 0; min--)
{
if (min == 0 && h > 0)
m = 59;
for (int sec = s; sec >= 0; sec--)
{
if (sec == 0)
s = 59;
Sleep(1000);
system("cls");
cout << hour << " :hours " << min << " :mins " << sec << " :secs" << endl;
}
}
}
Sleep(1000);
cout << "THE END" << endl;
return 0;
The problem i keep getting is:
Severity Code Description Project File Line Suppression State
Error C1075 the left brace '{' was unmatched at the end of the file
I'm not sure where I'm supposed to add a } ?
Thanks in advance!
Looks like you're missing your end brace at the end of your main function after the return 0;

How to skip over >= in C++

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;
}

Why won't my code switch player names correctly after 3 rounds of play?

So in my class I had to make a Numberwang simulation game. Everything works fine except for the fact that after 2 rounds the names don't correlate correctly. It supposed to say "Round 3, Player1 to play first." which it does however player2 comes up as the one to play first.
# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;
bool numberwang(int n)
{
if(n < 100 ){
return 1;
} else {
return 0;
}
}
int main()
{
string Firstplayer, Otherplayer;
int rounds;
int counter = 1;
int number;
int win = 18;
int lose= 1;
cout << "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about!" << endl;
cout << "What is player 1's name? ";
cin >> Firstplayer;
cout << "What is player 2's name? ";
cin >> Otherplayer;
cout << "How many rounds? ";
cin >> rounds;
cout << "Well, if you're ready, lets play Numberwang!" << endl;
while(counter <= rounds){
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
while(true){
cout << Firstplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
}
}
return 0;
}
After your if-statement (line 61) you say 'Firstplayer' and then you output the 'Otherplayer'. The names do not match.
Blockquote
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;

(C++) Code seems to be operating off the wrong loop...?

I've been playing around with some code in my down-time from my degree and I've nested a do{}while() loop inside another one but the problem I'm having is that the code keeps going until the last van is full, even after the number of parcels has been fulfilled...
The code's below. If someone could take a look at it and tell me what I've done wrong that'd be awesome. Bare in mind I've only really been coding in C++ for about a month so I've still got hella lot to learn..
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while ((fCurrentPayload + fParcelWeight)) < fVanCapacity)
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCout;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}
Change this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity);
to this
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity
&& iParcelLoaded < iParcelCount);
That way you will load as many items the user inputs. You code contains many syntax errors.
I corrected them for you, but please be more careful next time you post.
#include <iostream>
using namespace std;
char cBeltFull;
int iVanCount, iParcelCount, iParcelLoaded;
float fHeaviestVanWeight, fParcelWeight, fCurrentPayload, fVanCapacity;
char cExit = 'N';
int main() {
iVanCount = 1;
iParcelLoaded = 1;
fHeaviestVanWeight = 0;
fVanCapacity = 410;
do {
//Get the number of parcels to dispatch
cout << "How many parcels need sending?" << endl;
cin >> iParcelCount;
do {
fCurrentPayload = 0;
do {
//Get parcel weight
cout << endl << endl << endl << "What is the weight the parcel " << iParcelLoaded << "?";
cin >> fParcelWeight;
//'Load' the parcel
cout << endl << endl << "Parcel loaded";
iParcelLoaded ++;
//Update the payload
fCurrentPayload = fCurrentPayload + fParcelWeight;
} while (((fCurrentPayload + fParcelWeight)) < fVanCapacity && iParcelLoaded < iParcelCount);
//Dispatch the van
cout << endl << endl << "Van dispatched.";
//Update the van count
iVanCount ++;
if (fCurrentPayload > fHeaviestVanWeight) {
//Update the heaviest weight
fHeaviestVanWeight = fCurrentPayload;
}
} while (iParcelLoaded <= iParcelCount);
cout << endl << endl << endl << "Vans dispatched: " << iVanCount;
cout << endl << endl << "Weight of heaviest van: " << fHeaviestVanWeight;
cout << endl << endl << endl << "Exit? Y for YES or N for NO." << endl;
cin >> cExit;
} while (cExit == 'N');
}