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;

}``````

All 4 Replies

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;
}``````
``````  #include <iostream>

#include <ctime>

using namespace std;

int main()

{
char ans;
int die;

int money=10;

int num;

char roll;

do{

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 " << die << " You win \$100 :)" <<endl;

money += 100;

}

else if (num < die)

{

cout<<"Sorry the dice rolled a   " <<die <<endl;

money -= 1;

}

cout<< "Would you like to try another one? y/n?" << endl;
cin>> ans;
}while (ans== 'y');

}
``````

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)` .

``````#include <iostream>
#include <ctime>

using namespace std;

int main()
{
int dice;
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);
srand(time(NULL));
dice = (rand()%6)+1;
if(num > 6)
{
system("cls");
cout << "You can't enter hieger number of 6!" << endl;
cout << "Try again !" << endl;
system("PAUSE");
}
if (num == dice)
{
cout<<"Winner! The dice rolled " << dice <<endl;
money += 100;
cout << "Your bank acc is now \$" << money << endl;
system("PAUSE");
}
else if (num != dice)
{
cout << "Sorry the dice rolled " << dice <<endl;
money -= 1;
cout << "Your bank acc is now \$" << money << endl;
system("PAUSE");
}
}
return 0;
}
``````

I just see this post and figure out how can be this game runing smooth .. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.