954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Dice Roll Game

I am trying to get my homework assignment to work for class, but I cant seem to get it. Heres the assignment.

Write a program that asks the user to guess the next roll of a six sided die. Each guess costs $ 1. If they guess correctly, they get $ 100.00. The player will start out with a $10.00 bank. Each time he (or she) guesses incorrectly you will subtract $1.00 from their bank. Each time they guess correct, you add $100.00 to their bank. Print the total winnings or loss at the end of the game. [Each die toss is simulated using die = (int)(rand() % 6) + 1;]

Your program should look something like this:

Your bank is $10.00 Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100.00:

6 Sorry, the dice rolled a 5. continue?

y

Your bank is $9.00 Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100.00:

4 Winner! the dice rolled a 4. continue? n Thanks for playing your bank is $109.00. Come back real soon

Seems not to bad doesnt it? Well I have been working for a while and it still isnt working for me. I enter lets say a 4 it will tell me Sorry, the dice rolled a 5. I need a little help if anyone can please. I am not looking for you to complete my assignment, but take a look and give some advice for a beginner.

Thanks :)

#include <iostream>
#include <ctime> 

using namespace std; 

int main()
{
int die;
int money=10;
int num;
char roll;
{
cout<<"Your bank is $" <<money<<endl;
cout<<"Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100.00:" <<endl;
cin>>num;
cin.get(roll);
die=(int)(rand()%6)+1;
if (num==die)
{
cout<<"Winner! The dice rolled a" <<num<<endl;
money += 100;
}
else if (num < die)
{
cout<<"Sorry the dice rolled a "<<num<<endl;
money -= 1;
}
}
return 0;

}
FtKShadow
Newbie Poster
24 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

I am trying to get my homework assignment to work for class, but I cant seem to get it. Heres the assignment.

Seems not to bad doesnt it? Well I have been working for a while and it still isnt working for me. I enter lets say a 4 it will tell me Sorry, the dice rolled a 5. I need a little help if anyone can please. I am not looking for you to complete my assignment, but take a look and give some advice for a beginner.

Thanks :)

#include <iostream>
#include <ctime> 

using namespace std; 

int main()
{
int die;
int money=10;
int num;
char roll;
{
cout<<"Your bank is $" <<money<<endl;
cout<<"Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100.00:" <<endl;
cin>>num;
cin.get(roll);
die=(int)(rand()%6)+1;
if (num==die)
{
cout<<"Winner! The dice rolled a" <<num<<endl;
money += 100;
}
else if (num < die)
{
cout<<"Sorry the dice rolled a "<<num<<endl;
money -= 1;
}
}
return 0;

}


What is the purpose of roll ? Look carefully at these lines and make sure they are what you want. See red highlight.

die=(int)(rand()%6)+1;
if (num==die)
{
cout<<"Winner! The dice rolled a" <<num<<endl;
money += 100;
}
else if (num < die)
{
cout<<"Sorry the dice rolled a "<<num<<endl;
money -= 1;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

#include

#include

using namespace std;

int main()

{
char ans;
int die;

int money=10;

int num;

char roll;


do{

cout<<"Your bank is $" <>num;

cin.get(roll);

die=(int)(rand()%6)+1;

if (num==die)

{

cout<<"Winner! The dice rolled a " << die << " You win $100 :)" <> ans;
}while (ans== 'y');


}

janaan
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

In your second if statement you are saying if (num < die) . what would happen if I guessed 5 and the die was 2? A better way would be to do if (num != die) .

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You