Hello all. I am new to the forum. I joined specifically to ask this question, as I stumbled upon here while searching for answers.
I am working on a project for my beginning programming class (this is my first semester) and I am about 80% complete. Something in the instructions has me confused, however and I cannot figure out how to go about this, even using arrays. I am apparently missing that "aha!" moment. If any of you can help me, I would greatly appreciate it. the portion of the problem I am stuck on is this:
"...you will add $7.50 to their bank. Treat one higher than 10 as 1 and 1 lower than 1 as 10. In other words, you wrap the number around if you go one higher than the max or 1 lower than the min. "
The only thing that I can think of is to set up 4 variable (not counting the actual roll) as "RollMax", "RollMin", "RollMaxOne", "RollMinOne",, setting the values to wrap, but I KNOW that is not the actual solution. It's a gambling/chance/dice program, and it's coming along nicely but for this one part...
javascript:PostPreview()
Can anyone help?
(I have my "rand()" seeded off time and running fine)
int rollDice()
{
int result = ((rand()%10) +1);
return result;
}
and my game function calls that. The problem I ma having is applying that to user input. If I use modulus, and the user gives me a single digit number, (such as 4), won't it return a "0", as that is the number after the decimal?
/*
Paul Wiklund
CSIS 111B 07 May 2011
Project "A Simple Game of Chance"
Write a program that asks the user to guess the next roll of one ten sided die.
*/
#include <iostream>
#include <ctime>//include ctime for srand, rand and time.
#include <iomanip>
usingnamespace std;
int rollDice();
int game(double UserBankRoll);
double UserRolling(double UserBankRoll);
double UserBankRoll = 20.00;//The player will start out with a $20 bankroll.
int main()
//Your main function should only have srand and a Game function.
{
srand((unsigned)time(0));//You should seed the random number generator using the time function.
game(UserBankRoll);
return 0;
}
int game(double UserBankRoll)
/*Your program should at least have a Game function and you may create any other functions you need*/
{
UserBankRoll;
char UserAnswer;
if (UserBankRoll >= 1.00)
{
cout << "\nWould you like to roll some dice? Press Yes or No." << endl;
cin >> UserAnswer;
while (UserAnswer == 'Y' || UserAnswer == 'y')
/*when you ask the user if he wants to play again you should only play again if the user enters
a y or Y.*/
{
UserRolling(UserBankRoll);
}
if (UserAnswer == 'N' || UserAnswer == 'n')
{
cout << "\nThanks for playing. Please come back again soon!" << endl;
cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
/*Print the total winnings or loss at the end of the game.*/
exit(0);
}
elseif (UserAnswer != 'Y' && UserAnswer != 'y' && UserAnswer != 'N' && UserAnswer != 'n')
{
cout << "\nThat was not a valid statement" << endl;
game(UserBankRoll);
}
}
else
{
cout << "\nYou have insufficient funds to complete this transaction." << endl;
cout << "Thanks for playing. Please come back again soon!" << endl;
cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
/*Print the total winnings or loss at the end of the game.*/
main();
}
return 0;
}
double UserRolling(double UserBankRoll)
{
signedint MaxUserVal=10;
signedint MinUserVal=1;
signedint ActualUserVal=0;
signedint result=rollDice();
if (UserBankRoll >= 01.00 )
{
cout << "\nYour bank is $" << fixed << setprecision(2) << UserBankRoll << endl;
cout << "It costs $1.00 to guess the next roll of a 10 sided die. If you \n";
cout << "guess correctly, I will pay you $7.50. If you miss the number by \n";
cout << "one I will pay you $2.00. Enter your guess (1-10): " ;
cin >> ActualUserVal;
cout << "\n";
if (ActualUserVal < MinUserVal || ActualUserVal > MaxUserVal)
/*When you ask the user for his guess the program should not accept any answer that is
outside the 1-10range.*/
{
cout << "That was not a valid number.\nThe die cannot roll " << ActualUserVal << "\n";
UserRolling(UserBankRoll);
}
else
{
while (result == 10 && ActualUserVal == 1)
/*Treat one higher than 10 as 1 and one lower than 1 as 10. In other words you wrap the
number around if you go one higher than the max or one lower than the min. */
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
while (result == 1 && ActualUserVal == 10)
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
if (result == ActualUserVal)
/*Each time they guess correctly, you add $7.50 to their bank.*/
{
cout << "\nYou won $7.50! The die rolled a " << result << endl;
UserBankRoll = (UserBankRoll-1)+07.50;//Each guess costs $1.
game(UserBankRoll);
}
elseif (result == ActualUserVal +1)/*If the user’s guess is one number higher or lower than
the random number, you add $2 to the bankroll.*/
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
elseif (result == ActualUserVal -1)
/*If the user’s guess is one number higher or lower than the random number, you add $2 to the
bankroll.*/
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
else
{
cout << "\nSorry! You were too far off. You predicted " << ActualUserVal << " and you rolled " << result << "." <<endl;
cout << "You lost $1.00."<< endl;
UserBankRoll = (UserBankRoll-1);//Each guess costs $1.
game(UserBankRoll);
}
}
}
return UserBankRoll;
}
int rollDice()
{
int result = ((rand()%10) +1);
return result;
}
Sure. I can do that. I only posted the snippet because I don't want the entire project done for me. As it is, I have worked a lot of the bugs out of the code already. The only question I am having is integer wrapping. Here is the assignment:
CSIS 111B Programming Project 1
A Simple Game of Chance (250 points)
Write a program that asks the user to guess the next roll of one ten sided die. Each guess costs $1. If they guess correctly, they get $7.50. The player will start out with a $20 bankroll. Each time he (or she) guesses correctly or incorrectly you will subtract $1 from their bankroll. Each time they guess correctly, you add $7.50 to their bank. If the user’s guess is one number higher or lower than the random number, you add $2 to the bankroll. Treat one higher than 10 as 1 and one lower than 1 as 10. In other words you wrap the number around if you go one higher than the max or one lower than the min. Print the total winnings or loss at the end of the game.
Your main function should only have srand and a Game function. Your program should at least have a Game function and you may create any other functions you need. When you ask the user for his guess the program should not accept any answer that is outside the 1-10 range. Likewise when you ask the user if he wants to play again you should only play again if the user enters a y or Y.
Hint: Since the program uses random numbers, include ctime for srand, rand and time. You should seed the random number generator using the time function.
Your program output should look something like this:
Your bank is $20
It costs $1.00 to guess the next roll of a ten sided die. If you
guess correctly I will pay you $7.50. If you miss the number by
one I will pay you $2.00. Enter your guess (1-10): 10
Sorry, you guessed wrong - the die rolled a 5 - you lost $1.00.
/*
Paul Wiklund
CSIS 111B 07 May 2011
Project "A Simple Game of Chance"
Write a program that asks the user to guess the next roll of one ten sided die.
*/
#include <iostream>
#include <ctime>//include ctime for srand, rand and time.
#include <iomanip>
usingnamespace std;
int rollDice();
int game(double UserBankRoll);
double UserRolling(double UserBankRoll);
double UserBankRoll = 20.00;//The player will start out with a $20 bankroll.
int main()
//Your main function should only have srand and a Game function.
{
srand((unsigned)time(0));//You should seed the random number generator using the time function.
game(UserBankRoll);
return 0;
}
int game(double UserBankRoll)
/*Your program should at least have a Game function and you may create any other functions you need*/
{
UserBankRoll;
char UserAnswer;
if (UserBankRoll >= 1.00)
{
cout << "\nWould you like to roll some dice? Press Yes or No." << endl;
cin >> UserAnswer;
while (UserAnswer == 'Y' || UserAnswer == 'y')
/*when you ask the user if he wants to play again you should only play again if the user enters
a y or Y.*/
{
UserRolling(UserBankRoll);
}
if (UserAnswer == 'N' || UserAnswer == 'n')
{
cout << "\nThanks for playing. Please come back again soon!" << endl;
cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
/*Print the total winnings or loss at the end of the game.*/
exit(0);
}
elseif (UserAnswer != 'Y' && UserAnswer != 'y' && UserAnswer != 'N' && UserAnswer != 'n')
{
cout << "\nThat was not a valid statement" << endl;
game(UserBankRoll);
}
}
else
{
cout << "\nYou have insufficient funds to complete this transaction." << endl;
cout << "Thanks for playing. Please come back again soon!" << endl;
cout << "\nYour total winnings this session were: $" << fixed << setprecision(2) << UserBankRoll - 20.00 << endl;
/*Print the total winnings or loss at the end of the game.*/
main();
}
return 0;
}
double UserRolling(double UserBankRoll)
{
signedint MaxUserVal=10;
signedint MinUserVal=1;
signedint ActualUserVal=0;
signedint result=rollDice();
if (UserBankRoll >= 01.00 )
{
cout << "\nYour bank is $" << fixed << setprecision(2) << UserBankRoll << endl;
cout << "It costs $1.00 to guess the next roll of a 10 sided die. If you \n";
cout << "guess correctly, I will pay you $7.50. If you miss the number by \n";
cout << "one I will pay you $2.00. Enter your guess (1-10): " ;
cin >> ActualUserVal;
cout << "\n";
if (ActualUserVal < MinUserVal || ActualUserVal > MaxUserVal)
/*When you ask the user for his guess the program should not accept any answer that is
outside the 1-10range.*/
{
cout << "That was not a valid number.\nThe die cannot roll " << ActualUserVal << "\n";
UserRolling(UserBankRoll);
}
else
{
while (result == 10 && ActualUserVal == 1)
/*Treat one higher than 10 as 1 and one lower than 1 as 10. In other words you wrap the
number around if you go one higher than the max or one lower than the min. */
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
while (result == 1 && ActualUserVal == 10)
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
if (result == ActualUserVal)
/*Each time they guess correctly, you add $7.50 to their bank.*/
{
cout << "\nYou won $7.50! The die rolled a " << result << endl;
UserBankRoll = (UserBankRoll-1)+07.50;//Each guess costs $1.
game(UserBankRoll);
}
elseif (result == ActualUserVal +1)/*If the user’s guess is one number higher or lower than
the random number, you add $2 to the bankroll.*/
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
elseif (result == ActualUserVal -1)
/*If the user’s guess is one number higher or lower than the random number, you add $2 to the
bankroll.*/
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed " << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;//Each guess costs $1.
game(UserBankRoll);
}
else
{
cout << "\nSorry! You were too far off. You predicted " << ActualUserVal << " and you rolled " << result << "." <<endl;
cout << "You lost $1.00."<< endl;
UserBankRoll = (UserBankRoll-1);//Each guess costs $1.
game(UserBankRoll);
}
}
}
return UserBankRoll;
}
int rollDice()
{
int result = ((rand()%10) +1);
return result;
}
EDIT:
The problem that I am having is that my console display is capable of reading
"Sorry! You were too far off. You predicted a 1 and you rolled a 10.
You lost $1.00."
I can't have that, but I can't find a solution anywhere (though I know there is one or it wouldn't be part of the assignment)
If the user’s guess is one number higher or lower than the random number, you add $2 to the bankroll. Treat one higher than 10 as 1 and one lower than 1 as 10.
With respect to the user guess being within 1 of the dice value, yt's just saying that the next one up from 10 is 1 and the next one down from 1 is 10.
kbw: Thanks, I understand that portion in theory, I am just at a loss as how to implement it in code. The rest of my code is pretty straightforward. I believe I have met all of the other assignment criteria on my own, I am just stuck at the very part that you mentioned.
I did my best at that part with lines 97-111 of the above code. I just can't make the 10 wrap to 1 or the 1 wrap to 10
What about using relational opderators in a 'while' statement?
i.e.:
1 2 3 4 5 6 7
while (result == 10 && ActualUserVal == 1)
{
cout << "\nYou were close! The die rolled a " << result << " and you guessed" << ActualUserVal << "." <<endl;
cout << "You won $2.00."<< endl;
UserBankRoll = (UserBankRoll-1)+2.0;
game(UserBankRoll);
}
I think that might be the ticket. If I have solved this thing then I am going to mark the query as solved.
That worked. Conditionals with a &&relational operator seem to have done the trick.
I am removing the bulk of my code because it is a project that is worth 1/4 the final grade, and I don't want to carry others through the class. I will re post the code after the project is graded. Than k you KBW for your assistance.
so recursion is where my functions call themselves? ( I had to look that up lol)
Some of the code you posted wasn't within the scope of my class, so there are certain options you used that I didn't know were available...I can't wait to get deeper into this, I really overloaded myself this semester.