I have created a dice betting game. I cannot get the loop to run correctly could someone help me with finding the issue? - infinite-loop

Okay so I need help with getting the bank to NOT reset to 100 after each time the loop runs. I have tried many ways but can't seem to get it to work. Could you please help me with a few explanations and examples?
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int displaystats(int gamesplayed, int wins, int losses, int bank);
int main()
{
int bank = 100;//intital bank value
int bet = 0;//desired wager
int wins = 0;//games won
int losses = 0;//games lost
int gamesplayed = 0;//how many rounds you played
int compdice1 = 0;//first rolled dice for computer
int compdice2 = 0;//second rolled dice for computer
int playdice1 = 0;//first rolled dice for player
int playdice2 = 0;//seconds rolled dice for player
int newdice = 0;//the dice to risk your wager
int comproll = 0;//the sum of the computers roll
int playroll = 0;//the sum of the players roll
do
{
if (bank < 0)
{
cout << "You have " << bank << " coins in your bank." << endl;
cout << "I am sorry you are out of money." << endl;
displaystats(gamesplayed, wins, losses, bank);
break;
}
else if (bank > 0)
{
cout << "You have " << bank << " coins in your bank." << endl;
cout << "How many coins would you like to bet? ";
cin >> bet;
compdice1 = (rand() + time(0)) % 6 + 1;//computer dice
compdice2 = (rand() + time(0)) % 6 + 1;//computer second dice
playdice1 = (rand() + time(0)) % 6 + 1;//player dice
playdice2 = (rand() + time(0)) % 6 + 1;//player second dice
comproll = compdice1 + compdice2;//computer sum
playroll = playdice1 + playdice2;//player sume
cout << "Your roll was " << playdice1 << " and " << playdice2 << " with a sume of " << playroll << endl;
if (playroll < comproll)
{
char option;//option to roll another dice
cout << "You win!" << endl;
cout << "Would you like to roll a third dice to earn 1.5 times your bet, yes or no? ";
cin >> option;
if (option == 'yes')
{
int newroll;//the new sum of the three dice
int newdice;//the extra roll
newdice = (rand() + time(0)) % 6 + 1;
newroll = playroll + newdice;//the value of players roll
if (newroll > comproll)
{
cout << "The computer rolled " << comproll << endl;
cout << "You now rolled higher than the computer therefore, I am sorry you lose this round." << endl;
cout << "Your bank now equals " << bank - bet << endl;
losses++;
gamesplayed++;
}
else if (newroll < comproll)
{
cout << "You win!" << endl;
cout << "Your bank now equals " << bank + (1.5 * bet) << endl;
wins++;
gamesplayed++;
}
}
else if (option == 'no')
{
cout << "Your bank now equals " << bank + bet << endl;
wins++;
gamesplayed++;
}
}
else if (playroll > comproll)
{
cout << "The computer rolled " << comproll << endl;
cout << "You rolled higher than the computer therefore, I am sorry you lose this round." << endl;
cout << "Your bank now equals " << bank - bet << endl;
losses++;
gamesplayed++;
}
else if (playroll = comproll)
{
cout << "The computer also rolled " << comproll << endl;
cout << "I am sorry you now lose double your bet!" << endl;
cout << "Your bank now equals " << bank - (2 * bet) << endl;
losses++;
gamesplayed++;
}
}
} while (bank > 0);
int stats = displaystats(gamesplayed, wins, losses, bank);
cout << "Your stats are " << stats << endl;
return 0;
}
int displaystats(int gamesplayed, int wins, int losses, int bank)
{
cout << "Games Played: " << gamesplayed << endl;
cout << "Wins: " << wins << endl;
cout << "Losses: " << losses << endl;
cout << "Bank Total: " << bank << endl;
return (gamesplayed, wins, losses, bank);
}

Your problem with the bank "resetting" constantly was you never actually subtracted the bet from the bank. See the following code, I hope this helps.
cout << "You have " << bank << " coins in your bank." << endl;
cout << "How many coins would you like to bet? ";
cin >> bet;
//This is the line that you forgot.
bank = bank - bet
If you then win, then you may want to later on add some money back into the bank. (But this is up to you.) I hope this helps.
EDIT:
Here is a paste-bin for the full code as requested: http://pastebin.com/HzvRxjXL
Also, if this solves your problem, I would appreciate it if you would mark it as the answer so others don't spend time answering a problem that has been solved.
EDIT 2:
This has a (As far as I can tell) fixed and commentated version of the code. I hope this helps: http://pastebin.com/miQjy4B5

Related

Rcpp: Calculation in loop stops with error "Not a matrix"

in an R script I source a cpp file to make some calculations. In that R script, a function defined in the cpp file is called and a matrix and an integer is provided. After a few rounds through the loop it gives the error "Not a matrix" (in line of code resid = (x(_,j) - x(_,i))*(x(_,j) - x(_,i));), even though for the rounds before it worked.
R script:
## all together
# rm(list=ls())
library(RcppArmadillo)
library(Rcpp)
sourceCpp("~/test.cpp",verbose = FALSE)
cat("start loop")
for(n in c(45:46)){
cat("\n", n, "\n")
p_m <- matrix(data=rnorm(n^2,1,1),nrow = n, ncol=n)
print(class(p_m))
print(some_function(p_m,nosamples=10))
}
cat("finished")
I start this R script via the command line. R version R-4.1.0. In R-Studio it crashes with a fatal error.
The cpp file:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector some_function(NumericMatrix x,int nosamples) {
int ncol = x.ncol();
NumericVector out2(nosamples);
int loops;
int loops2;
double result=0;
NumericVector::iterator it;
double acc = 0;
NumericVector resid(ncol);
NumericVector out(ncol*(ncol-1)/2);
loops2=0;
std::cout << nosamples << std::endl;
std::cout << (ncol-1) << std::endl;
std::cout << ncol*(ncol-1)/2 << std::endl;
while(loops2 < (nosamples)){
std::cout << "loops2:" << std::endl;
std::cout << loops2 << std::endl;
loops=0;
int i;
int j;
for(j=0;j<(ncol-1);++j){
std::cout << " j: " << j << std::endl;
for (i = (j+1); i < (ncol); ++i) {
std::cout << " i: " << i << std::endl;
resid = (x(_,j) - x(_,i))*(x(_,j) - x(_,i)); //here it stops
std::cout << " i: " << i << std::endl;
for(int ii=0; ii<ncol;++ii){
acc += resid[i];
}
result=sqrt(acc);
loops += 1;
out[loops] = result;
std::cout << " i: " << i << std::endl;
}
}
std::cout << "loops:" << std::endl;
std::cout << loops << std::endl;
out = out[out > 0];
it = std::min_element(out.begin(), out.end());
out= *it;
std::cout << out << std::endl;
loops2 += 1;
out2[loops2]=out[0];
}
std::cout << "cpp finished" << std::endl;
return(out2);
}
Can someone explain what the problem is about?
Thanks and kind regards
Edit
I adapted some things in the cpp file (shown below) and the error disappeared. First I thought, everything is fine. But when I increase the number of loops, another problem occurs: the function breaks, but no error is shown. It breaks after loop number 543 ("loop2: 543"). At least it does the same in each round of the while loop with the same data.
I adapted the R-script and the ccp file to make this problem (at least on my machine) reproducible.
I know this code seems to be somehow meaningless, but it is part of a bigger program and I wanted to give here a minimum example.
The R script:
## all together
# rm(list=ls())
library(RcppArmadillo)
library(Rcpp)
sourceCpp("~/test.cpp",verbose = FALSE)
cat("start loop")
for(n in c(100:101)){
cat("\n", n, "\n")
p_m <- matrix(data=rnorm(n^2,1,1),nrow = n, ncol=n)
print(class(p_m))
print(some_function(p_m,nosamples=800))
}
cat("finished")
The cpp file:
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(RcppEigen)]]
#include <RcppArmadillo.h>
#include <RcppEigen.h>
using namespace Rcpp;
using Eigen::Map;
using Eigen::VectorXd;
typedef Map<VectorXd> MapVecd;
// [[Rcpp::export]]
NumericVector some_function(NumericMatrix x,int nosamples) {
int ncol = x.ncol();
NumericVector out(ncol*(ncol-1)/2);
NumericVector out2(nosamples);
NumericVector out3(ncol*(ncol-1)/2);
NumericVector resid(ncol);
int loops;
int loops2;
double result=0;
double acc = 0;
int show_cout=0;
loops2=0;
std::cout << nosamples << std::endl;
std::cout << (ncol-1) << std::endl;
std::cout << ncol*(ncol-1)/2 << std::endl;
while(loops2 < (nosamples)){
std::cout << "loops2:" << loops2 << std::endl;
loops=0;
int i;
int j;
for(j=0;j<(ncol-1);++j){
// std::cout << " j: " << j << std::endl;
for (i = (j+1); i < (ncol); ++i) {
if(show_cout==1){
std::cout << " i: " << i << std::endl;
}
resid = (x(_,j) - x(_,i))*(x(_,j) - x(_,i));
if(show_cout==1){
std::cout << " i: " << i << std::endl;
}
for(int ii=0; ii<ncol;++ii){
acc += resid[ii];
}
result=sqrt(acc);
loops += 1;
out[loops] = result;
if(show_cout==1){
std::cout << " i: " << i << std::endl;
}
}
}
// std::cout << "loops:" << loops << std::endl;
//
out = out[out > 0];
const MapVecd xy(as<MapVecd>(out));
out3=xy.minCoeff();
out2[loops2]=out3[0];
loops2 += 1;
}
std::cout << "cpp finished" << std::endl;
return(out2);
}
Two things here:
Use out[loops++] = result; instead of loops += 1; out[loops] = result; because you were starting at 1, and probably accessing the last element outside of the range of this vector.
Use
for(int ii=0; ii<ncol;++ii){ double eps = x(ii, j) - x(ii, i); acc += eps * eps; }
instead of relying on this resid vector.

Passing map by reference in C++ and mutating its value

I am passing map by reference in the canSum function where i am mutating its value and adding pairs but at the end when I iterate over the map I find the value of map has not been updated.
canSum function is a recursive function which takes a number (targetSum) and an array and finds if it is possible to form targetSum by any combinations of number in the array (numbers can be repeated).
#include<iostream>
#include<vector>
#include<map>
using namespace std;
bool canSum(int targetSum,vector<int> a,map<int, bool> &m){
if(!(m.find(targetSum) == m.end()))
return m[targetSum];
if (targetSum == 0)
return true;
if(targetSum<0)
return false;
for (int num : a)
{
if (canSum(targetSum - num, a,m)==true)
{
// m[targetSum] = true;
m.insert(pair<int, bool>(targetSum, true));
return m[targetSum];
}
}
m[targetSum] = false;
return m[targetSum];
}
int main(){
int targetSum, t;
vector<int> a;
map<int, bool> m;
m[0] = true;
cout << "enter target" << endl;
cin >> targetSum;
cout << "enter array, press esc to stop entering"<<endl;
while(cin>>t){
a.push_back(t);
}
for (int j = 0; j < a.size(); j++)
{
cout << a[j]<<" ";
}
cout << endl;
for (auto itr = m.begin(); itr != m.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
if(canSum(targetSum, a,m)){
cout << endl << "true" << endl;
}
else cout << endl << "false" << endl;
return 0;
}
Please help me. Thank you.
The for loop to print the map should be after the function call like.
if(canSum(targetSum, a,m)){
cout << endl << "true" << endl;
}
else cout << endl << "false" << endl;
for (auto itr = m.begin(); itr != m.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
Instead of
for (auto itr = m.begin(); itr != m.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
if(canSum(targetSum, a,m)){
cout << endl << "true" << endl;
}
else cout << endl << "false" << endl;
To see mutations in the map due to the function

Is there any explanation for this behavior?

Memory address shows two different values.
We have const variable (a) and we put the address of the variable into two pointers (b and c). After changing the value at the address in one of the pointers (c), we had a situation where the same memory address has two different values.
Is there any explanation for this behavior?
#include <iostream>
int main(void)
{
const int a = 99;
const int *b = &a;
int *c = (int *)b;
std::cout << &a << " - " << a << '\n';
std::cout << b << " - " << *b << '\n';
std::cout << c << " - " << *c << "\n\n";
*c = 61;
std::cout << &a << " - " << a << '\n';
std::cout << b << " - " << *b << '\n';
std::cout << c << " - " << *c << '\n';
return 0;
}
//here are the result(output)
003CFAA4 - 99
003CFAA4 - 99
003CFAA4 - 99
003CFAA4 - 99
003CFAA4 - 61
003CFAA4 - 61

Runtime Check Failure #3 - Variable is not initiliazed

I really need some help on this problem. My project is to create a math tutor program with a menu that look like this:
cout << "Select one of the following options" << endl;
cout << "-----------------------------------------" << endl;
cout << "1. [A]dd [+]" << endl;
cout << "2. [S]ubtract [-]" << endl;
cout << "3. [M]ultiply [x]" << endl;
cout << "4. [D]ivide [/]" << endl;
cout << "5. [Q]uit " << endl;
when the user chose add or subtract, the program will generate 2 random numbers and come up with the result. When the user chose multiply or divide, the program will generate the second number again and do the math.
Here is my code
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{ //Introduction to user
cout << "Welcome to Math Tutor. This program is designed to help student" << endl;
cout << "to learn how to do basic math such as addition, subtraction,..." << endl;
cout << endl;
//Declare variables
char choice;
int num1, num2, answer, useranswer, remainder, userremainder;
//Seed 2 random numbers
srand(static_cast<unsigned int>(time(0)));
num1 = rand() % 200 + 800;
num2 = 1 + rand() % 200;
do
{
//Display the menu to user and get user input
cout << "Select one of the following options" << endl;
cout << "-----------------------------------------" << endl;
cout << "1. [A]dd [+]" << endl;
cout << "2. [S]ubtract [-]" << endl;
cout << "3. [M]ultiply [x]" << endl;
cout << "4. [D]ivide [/]" << endl;
cout << "5. [Q]uit " << endl;
cin >> choice;
cout << "Your choice is: " << choice << endl;
//Respond to user's selection
switch (choice)
{
case 'A':
case 'a':
case '1':
case '+':
answer = num1 + num2;
cout << "" << num1 << endl;
cout << "+" << num2 << endl;
cout << "-----" << endl;
cout << "Enter your answer: " << useranswer;
if (answer == useranswer)
{
cout << "Congratulation, Your answer is correct" << endl;
cout << num1;
cout << "+" << num2;
cout << "------" << endl;
cout << answer;
}
else
{
cout << "You answer is not correct" << endl;
cout << "The answer is: " << answer;
}
break;
case 'S':
case 's':
case '2':
case '-':
answer = num1 - num2;
cout << num1;
cout << "-" << num2;
cout << "------" << endl;
cout << "Enter your answer: " << useranswer;
if (answer == useranswer)
{
cout << "Congratulation, Your answer is correct" << endl;
cout << num1;
cout << "-" << num2;
cout << "------" << endl;
cout << answer;
}
else
{
cout << "You answer is not correct" << endl;
cout << "The answer is: " << answer;
}
break;
//Seed the second number again
num2 = 1 + rand() % 10;
case 'M':
case 'm':
case '3':
case 'x':
answer = num1 * num2;
cout << num1;
cout << "x" << num2;
cout << "------" << endl;
cout << "Enter your answer: " << useranswer;
if (answer == useranswer)
{
cout << "Congratulation, Your answer is correct" << endl;
cout << num1;
cout << "x" << num2;
cout << "------" << endl;
cout << answer;
}
else
{
cout << "You answer is not correct" << endl;
cout << "The answer is: " << answer;
}
break;
case 'D':
case 'd':
case '4':
case '/':
answer = (num1 / num2);
cout << " __________" << endl;
cout << num2 << ")" << num1 << endl;
cout << "Enter your answer, both the quotient";
cout << "and the remainder separated by a space: " << endl;
cin >> useranswer >> remainder;
if ((useranswer == answer) && (userremainder = remainder))
{
cout << "Congratulation, Your answer is correct" << endl;
cout << " __________" << endl;
cout << num2 << ")" << num1 << endl;
cout << "The answer is: " << answer << "and the remainder is: " << remainder << endl;
}
else
{
cout << "Sorry. The correct answer is: " << answer << " and the remainder is: " << remainder << endl;
}
case 'Q':
case 'q':
case '5':
cout << "Exiting the program....." << endl;
break;
//Validate user's chocie
default:
{
cout << "Please enter a valid choice" << endl;
cout << "You can enter a number from 1 to 5 or" << endl;
cout << "A letter corresponding to the calculation that you want to do" << endl;
cin >> choice;
cout << "Your choice is: " << choice;
}
}
//Pause the screen
if (choice != 5)
{
cout << endl;
cin.ignore(80, '\n');
cout << "Hit the enter key to continue....\n";
cin.get();
cout << endl;
}
} while (choice != 5);
return 0;
}
your problem is the lines
cout << "Enter your answer: " << useranswer
useranswer is what you wanted the user to input, so change these lines to
cout << "Enter your answer: ";
if(cin >> useranswer) { ... } else { ... } // handle non-integer input too

Why is my program looping infinitely?

My program skips past the option for the user to enter the squareSide and keeps looping.
Code is as follows:
do
{
//Displays menu
cout << "Please select a geometric shape." << endl << endl;
cout << "s:" << setw(10) << "Square" << endl;
cout << "c:" << setw(10) << "Circle" << endl;
cout << "d:" << setw(10) << "Diamond" << endl;
cout << "t:" << setw(10) << "Triangle" << endl;
cout << "e:" << setw(10) << "Exit" << endl << endl;
cin >> letter;
cout << "You selected " << letter << endl;
if (letter != EXIT || letter == EXIT1)
{
if (letter == SQUARE || letter == SQUARE1)
{
int squareSide;
int character;
cout <<"\nPlease enter the ASCII character you would like to use to print your square" << endl;
cin >> character;
cout << "\nPlease enter the length of one side of your square"
<< endl;
cin >> squareSide;
for (int x=0; x < squareSide; x++)
{
for (int y = 0; y < squareSide; y++)
{
cout << character;
}
cout << endl;
}
}
}
} while (letter != EXIT && letter != EXIT1);
return 0;
}
All menu selections have to be char.

Resources