Need help making Dice Game "NOOB"

I'm a complete noob to programming and im having some difficulty understanding the language. Basically for my c++ class he is having us do an assignment...well here is the description.

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.

Play again? (y/n) y


Your bank is $19

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): 1


You won $7.50! The die rolled a 1.

Play again? (y/n) y


Your bank is $25.5

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): 9


Close - the die rolled a 10

You won $2.00! You missed by one! The dice rolled a 10.

Play again? (y/n) n

Thanks for playing, your bank is $26.5. Come back and play again real soon.



Here is the mess I have created so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<ctime>
#include<cmath>
using namespace std;

double gameOfChance();

int main()
{
   srand(time(NULL));
   gameOfChance();
   return 0;
}

double gameOfChance()
{
   int userGuess;
   int diceRoll;
   char userInput;
   double userBank = 20;
   
   cout << "\t|---------------------------------------------------------|" << endl;
   cout << "\t|                        :RULES:                          |" << endl;
   cout << "\t|Your bank is $20.                                        |" << endl;
   cout << "\t|It costs $1.00 to guess the next roll of a ten sided die.|" << endl;
   cout << "\t|If yo guess correctly I will pay you $7.50.              |" << endl; 
   cout << "\t|If you miss the number by one I will pay you $2.00.      |" << endl;
   cout << "\t|---------------------------------------------------------|\n" << endl;

   cout << "Your bank is currently $" << userBank << ", would you like to guess for $1.00? (y/n)" << endl;
   cin >> userInput;

do{
   if(userInput == 'y'||'Y')
   {
      cout << "your bank is now $ " << userBank-1 << endl;
      cout <<"Please enter your guess (1-10): ";
      cin >> userGuess;

      diceRoll = rand() % 10 + 1;

      if(userGuess == diceRoll)
      {
         cout << "You Win! Your bank is now $" << userBank+7.50 << endl;
      }
      if(userGuess == diceRoll+1 || userGuess == diceRoll-1)
      {
         cout << "You were only 1 away! You won $2.00. Your bank is now $" << userBank+2 << endl;
      }
      if(userGuess != diceRoll)
      {
         cout << "Sorry, the die rolled " << diceRoll << endl;
         cout << "Your bank is now " << userBank - 1 << endl;
         cout << " play again? (y/n)" << endl;
         cin >> userInput;
      }     
   }
   else
   {
      cout << "Goodbye." << endl;
   }
   
}while(userInput == 'n' || 'N');
  
   return userBank;
}


Any help whatsoever would be greatly appreciated!
Last edited on
line 34/63 are incorrect: You must always write it like so: if((userInput == 'y')|| (userInput == 'Y')) (and line 63 alike) The parentheses are not necessary but it'll be more legible
Thanks for your time coder! Yes, I remember now. I had a feeling I was doing the OR statement wrong; my teacher teaches us to put parenthesis too. I know im doing something wrong with my loop too, when I run the program it ask the user if he or she wants to play again and when the user inputs y/n the program shuts down. I also don't understand how to keep updating the Bank, I always have "$19" every time I roll when it should be decreasing by $1 every time from the updated bank when the user chooses to play. So here it is again with the fixed syntax you pointed out. I also took out line 34. and changed a few other things.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<ctime>
#include<cmath>
using namespace std;

double gameOfChance();

int main()
{
   srand(time(NULL));
   gameOfChance();
   return 0;
}

double gameOfChance()
{
   int userGuess;
   int diceRoll;
   char userInput;
   double userBank = 20;
   
   cout << "\t|---------------------------------------------------------|" << endl;
   cout << "\t|                        :RULES:                          |" << endl;
   cout << "\t|Your bank is $20.                                        |" << endl;
   cout << "\t|It costs $1.00 to guess the next roll of a ten sided die.|" << endl;
   cout << "\t|If yo guess correctly I will pay you $7.50.              |" << endl; 
   cout << "\t|If you miss the number by one I will pay you $2.00.      |" << endl;
   cout << "\t|---------------------------------------------------------|\n" << endl;

   cout << "Your bank is currently $" << userBank << ", would you like to guess for $1.00? (y/n)" << endl;
   cin >> userInput;
   if (userInput == 'n' || userInput =='N')
   {
      cout << "Goodbye." << endl;
      exit(1);
   }

do{
      cout << "your bank is now $" << userBank-1 << endl;
      cout << "Please enter your guess (1-10): ";
      cin >> userGuess;

      diceRoll = rand() % 10 + 1;

      if(userGuess == diceRoll)
      {
         cout << "You Win! Your bank is now $" << userBank+7.50 << endl;
      }
      else if((userGuess == diceRoll+1 || userGuess == diceRoll-1))
      {
         cout << "You were only 1 away! You won $2.00. Your bank is now $" << userBank+2 << endl;
      }
      else if(userGuess != diceRoll)
      {
         cout << "Sorry, the die rolled " << diceRoll << endl;
         cout << "Your bank is now " << userBank - 1 << endl;
         cout << "play again? (y/n)" << endl;
         cin >> userInput;        
      }
   
}while((userInput == 'y')||(userInput == 'Y'));

      cout << "Goodbye." << endl;
  
   return userBank;
}
Last edited on
bump.
Last edited on
I always have "$19" every time I roll when it should be decreasing by $1 every time from the updated bank
That is because you don't decreas at all. After line 54 you should write --userBank; which decreases the value and omit the '- 1' on line 56.

The reason why it ends no matter whether you type 'y/n' is that line 42 leaves a line end in the buffer which you read on line 58. After line 41 adding something like cin.ignore(1000, '\n'); http://www.cplusplus.com/reference/iostream/istream/ignore/ should help
Topic archived. No new replies allowed.