Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,665 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,163 people online right now. Registration is fast and FREE... Join Now!




Simple dice program c++

 
Reply to this topicStart new topic

Simple dice program c++, please some1 check my code and help me

killakev
post 4 Sep, 2008 - 01:06 PM
Post #1


New D.I.C Head

*
Joined: 1 Sep, 2008
Posts: 20

ok here is my code... I want to ask the user to guess the roll of the dice. The program tells the user that they are either right or wrong... I did that but I have problems... Please someone help me! thanks

cpp
#include <iostream>

#include <ctime>

using namespace std;

int main()

{

int die;

int money=10;

int num;

int dice;

{

cout<<"Your bank is $" <<money<<endl;

cout<<"Please enter your guess for the next roll. It only costs $1.00 to play,"<<endl;

cout<<"If you are correct I will pay you $100.00:" <<endl;

cin>>num;

srand(time(0));

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

while (die==num)

{

cout<<"Winner. The dice rolled a "<<die<<endl;
num=die;

}

if (die!=num)

cout<<"Sorry. The dice rolled a "<<die<<endl;

num!=die;

}

return 0;

}

MOD EDIT: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 4 Sep, 2008 - 01:27 PM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,441



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Just a few simple errors, I've edited them into comments, you can delete them.

Here:
cpp
#include <iostream> // missed the #
#include <ctime>

using namespace std;

int main()
{
int die;
int money=10;
int num;
// int dice; this variable is unused, it can be edited out

// { is unnecessary here!

cout<<"Your bank is $" <<money<<endl;
cout<<"Please enter your guess for the next roll. It only costs $1.00 to play,"<<endl;
cout<<"If you are correct I will pay you $100.00:" <<endl;
cin>>num;

srand(time(0));
die=(int)(rand()%6)+1;

while (die==num)
{
cout<<"Winner. The dice rolled a "<<die<<endl;
num=die;
}

if (die!=num)
cout<<"Sorry. The dice rolled a "<<die<<endl;

cin.get (); // hold the window open
return 0;

}
Hope this helps smile.gif

EDIT:
Only just checked the code properly. You need to fix your while loop. A bool will do the job nicely:
cpp
#include <iostream> // missed the #
#include <ctime>

using namespace std;

int main()
{
int die;
int money=10;
int num;
bool exit = false;
// int dice; this variable is unused, it can be edited out

// { is unnecessary here!


srand(time(0));

do
{
die=(int)(rand()%6)+1;
cout<<"Your bank is $" <<money<<endl;
cout<<"Please enter your guess for the next roll. It only costs $1.00 to play,"<<endl;
cout<<"If you are correct I will pay you $100.00:" <<endl;
cin>>num;
if (num == 0) // give an exit number
exit = true; // quit the loop
else
{
money--;

if (die == num)
{
cout<<"Winner. The dice rolled a "<<die<<endl;
money += 100;
}
else if (die!=num)
{
cout<<"Sorry. The dice rolled a "<<die<<endl;
}
}
} while (!exit);

cin.get (); // hold the window open
return 0;

}

It could also be modified to exit when money == 0

smile.gif
User is online!Profile CardPM

Go to the top of the page

red_4900
post 4 Sep, 2008 - 01:27 PM
Post #3


Code Dreamers

****
Joined: 22 Feb, 2008
Posts: 792



Thanked 10 times
My Contributions


your problem is in the if statement. you forgot the opening bracket.

in your while loop, num=die; has no meaning whatsoever. I haven't have tested your code, your while loop will becomes an infinite loop as it have no condition where the loop will end. I suggest you change your while loop to if loop.

this could do the trick :
cpp
if(num == die)
{
cout<<"Winner. The dice rolled a "<<die<<endl; //winner declaration
money++; //add prize money
}
else
{
cout<<"Sorry. The dice rolled a "<<die<<endl; //loser declaration
money--; //decrease the player's money
}


hope this helps you smile.gif

edit : dammit gabe. you beat me to it, and only by a few seconds. zzz

edit again : gabe, have you noticed the while loop in the code? hehe~

This post has been edited by red_4900: 4 Sep, 2008 - 01:32 PM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 4 Sep, 2008 - 01:35 PM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,441



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Yep, edited it.

tongue.gif
User is online!Profile CardPM

Go to the top of the page

killakev
post 4 Sep, 2008 - 01:48 PM
Post #5


New D.I.C Head

*
Joined: 1 Sep, 2008
Posts: 20

QUOTE(gabehabe @ 4 Sep, 2008 - 02:35 PM) *

Yep, edited it.

tongue.gif



Thanks for the help!! One more thing if I wanted to ask the user to Continue? Y or N how would you implement that in the code. Right now the loop will close when the money reaches 0 and I want it to ask the user to continue each time they guess wrong...
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 4 Sep, 2008 - 02:00 PM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,441



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Before your loop, declare a char.
cpp
char yn;

Then, in your loop, prompt the user:
cpp
cout << "Do you wish to continue? (y/n)";
cin >> yn;

Then perform a check. If it is n, then you should exit:
cpp
if (yn == 'n' || yn == 'N')
exit = true;

Remember to check for upper & lower case characters!

Was this helpful? wink.gif
User is online!Profile CardPM

Go to the top of the page

killakev
post 4 Sep, 2008 - 03:00 PM
Post #7


New D.I.C Head

*
Joined: 1 Sep, 2008
Posts: 20

QUOTE(gabehabe @ 4 Sep, 2008 - 03:00 PM) *

Before your loop, declare a char.
cpp
char yn;

Then, in your loop, prompt the user:
cpp
cout << "Do you wish to continue? (y/n)";
cin >> yn;

Then perform a check. If it is n, then you should exit:
cpp
if (yn == 'n' || yn == 'N')
exit = true;

Remember to check for upper & lower case characters!

Was this helpful? wink.gif



this is how i did it... It gave me a debug error when i input the number...
CODE

#include <iostream> // missed the #  
#include <ctime>  
  
using namespace std;  
  
int main()  
{  
    int die;  
    int money=10;  
    int num;
    char yn;
    bool exit = false;

    // int dice; this variable is unused, it can be edited out  
  
    // { is unnecessary here!  
  
  
    srand(time(0));  
  
    do  
    {  
        die=(int)(rand()%6)+1;  
        cout<<"Your bank is $" <<money<<endl;  
        cout<<"Please enter your guess for the next roll. It only costs $1.00 to play,"<<endl;  
        cout<<"If you are correct I will pay you $100.00:" <<endl;  
        cin>>num;  
        if (yn == 'n' || yn == 'N')  
        exit = true;
        else  
        {  
            money--;  
  
            if (die == num)  
            {  
                cout<<"Winner. The dice rolled a "<<die<<endl;  
                money += 100;  
            }  
            else if (die!=num)  
            {  
                cout<<"Sorry. The dice rolled a "<<die<<endl;
                cout << "Do you wish to continue? (y/n)";  
                cin >> yn;
            }

        }  
    } while (!exit);
User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 4 Sep, 2008 - 08:59 PM
Post #8


It's all about the code ♥

Group Icon
Joined: 11 Jul, 2007
Posts: 1,503



Thanked 14 times

Dream Kudos: 125
My Contributions


Two things I saw. One, you need another } at the very end of your code (which is probably just a result of copy/paste, sometimes we don't get it all).
The second thing is you need to move your check down a bit, like this:
cpp

else if (die!=num)
{
cout<<"Sorry. The dice rolled a "<<die<<endl;
cout << "Do you wish to continue? (y/n)";
cin >> yn;
if (yn == 'n' || yn == 'N')
exit = true;
}


Also, if you're getting an error when you input numbers, you can clear the error and then make the program ignore whatever's left in the buffer (if the wrong characters get input). Thanks to NickDMax for telling me about this. I use it a lot.
cpp

cin.clear();//clear the error state
cin.ignore(numeric_limits<streamsize>::max(),'\n');
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:48AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month