Hey everyone,
so I'm new in object oriented programming so I'm not sure about how to write this program. I need to create a die game with the class: DIE, that rolls the die. it needs to have a method called: "roll" that rolls the dice again and it needs to print the value. the second time we roll the die, it need to print the previous value as well.

HELP!!!

Recommended Answers

All 8 Replies

Help with what?

You have to ask a question we can answer. All you did is describe the assignment.

Help with what?

You have to ask a question we can answer. All you did is describe the assignment.

I really don't know how to do it, I'm lost!
first, how do I make the random thing work??!

#include <cstdlib>
#include <iostream>
#include <cmath>
using std::rand;
using namespace std;

class Die
{
      private:     

            
             int x; 
           
             
      public:
             
           Die():x ( 0 )
      {}             
             
             void print ();    
             int getX ();   
             void setX (int a);
             
};    

int Die::getX()
{
return x;
}

void Die::setX(int x)
{
     x=1 + rand() % 6;
}    

void Punto::print ()
{ 
     cout << x << endl;
}

int main()
{
    int x;
    Die p; 
    
    p.setX (x);
    p.imprimir ();
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

don't hate me! I know it's wrong!

Help with what?

You have to ask a question we can answer. All you did is describe the assignment.

I think He/She meant to ask how to do the functions. Unless I am wrong and they wanted us to write a whole program for them.

Why would I hate you? You posted code with CODE tags! You are already one of my favorites!

So what does your code do now?
What the the next thing you need it to do?
I'm assuming it compiles.

Remember, we are programmers, not psychics. You need to ask questions, not let us figure out what you need to do next. Then we can guide to in the right direction. After all, it's your program, not ours.

Why would I hate you? You posted code with CODE tags! You are already one of my favorites!

So what does your code do now?
What the the next thing you need it to do?
I'm assuming it compiles.

Remember, we are programmers, not psychics. You need to ask questions, not let us figure out what you need to do next. Then we can guide to in the right direction. After all, it's your program, not ours.

hahahaha...
it compiles, but I think there's something wrong with the initialization of the constructor because I only get 0s and I'm supposed to get numbers from 1 to 6... Do you know what's wrong with my code?
and after that, I need to create a method called: "roll" that rolls the die again. how do I do that?

ok, it was wrong, now it compiles, but with the same thing... I only get 0s

#include <cstdlib>
#include <iostream>
#include <cmath>
using std::rand;
using namespace std;

class Die
{
      private:     

            
             int x; 
           
             
      public:
             
           Die():x ( 0 )
      {}             
             
             void print ();    
             int getX ();   
             void setX (int a);
             
};    

int Die::getX()
{
return x;
}

void Die::setX(int x)
{
     x=1 + rand() % 6;
}    

void Die::print ()
{ 
     cout << x << endl;
}

int main()
{
    int x;
    Die p; 
    
    p.setX (x);
    p.print ();
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

ok, I think I got it with this:

#include <cstdlib>
#include <iostream>
#include <cmath>
using std::rand;
using namespace std;


class Die
{
      private:     

             int x; 
      public:
             
           Die():x ( 0 )
           {}             
             
             void print ();    
             int getX ();   
             void setX ();
             
};    

int Die::getX()
{
return x;
}

void Die::setX()
{
     x=1 + rand() % 6;
}    

void Die::print ()
{ 
     cout << "el numero es: " <<  x << endl;
}

int main()
{
    
    Die p; 
    
    p.setX ();
    p.print ();
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

But now, please help me with the: "roll" method... how do I do it??

Change the name of setX to Roll. Add a statement to Roll to set the old value to another private member variable. Make a new setX that has the signature void setX(int value) which is only used to set the value of the die if necessary.

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.