You are using a version without Ads of this website. Please, consider donating:

Some help please if you dont mind.

closed account (N8pE3TCk)
so im trying to create a simple program that rolls a 1-6 die and gives me a random 1-6 number. I have about 2 weeks worth of experience so practically none at all , any help would be appreciated.

this is what I have written so far...

/*
Module Name: Random Die!

Function: Rolls a standard six face die

Inputs: None

Outputs: Screen

Usage: The user will run the program from the command line. The program will randomly generate a number one through six.

Design Notes: Generates random number one through six
*/
#include <iostream>
#include <ctime>
#include <cstdlib>
//Taylor Mon
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int main()
{
int a =1;
int b =2;
int c =3;
int d =4;
int e =5;
int f =6;
int rand(int a, int b, int c, int d, int e, int f);
cout<< rand(int) << endl;
return 0;
}

thanks again, as is what I wrote will not run at all...
rand() returns a random number out of a very large range. % is the modulus operator which gets you the integer remainder of what's on the left side when divided by what is on the right side. If you divide a number by 6, the possible remainders are going to be 0, 1, 2, 3, 4, 5. So a large random number % 6 will be a random number between 0 and 5. Add 1, and you have a number from 1 to 6.
 ``12345`` ``````int main() { srand(time(0)); int random_number_between_1_and_6 = rand() % 6 + 1; .... }``````

Also the below doesn't make sense to do.
 ``1234`` ``````using namespace std; using std::cout; using std::cin; using std::endl;``````

using namespace std; makes the whole std namespace visible, including std::cout, std::cin, and std::endl. It's good practice to not use the using directive because in larger projects it could lead to naming conflicts. Instead you would just put std:: before each use of cout, cin, endl, etc. But if not, then it is at least better practice to do this
 ``123`` ``````using std::cout; using std::cin; using std::endl;``````

`using namespace std;`
because you now only are risking naming conflicts with the names cout cin and endl, which are unlikely to arise.

Also remember for future reference to never use the using directive in header files, only implementation files (.c, cpp).
Last edited on
closed account (N8pE3TCk)
Thank you so much , and your right that does make alot more sence to me seeing it that way as opposed to how I wrote it down.

my problem now is I get the number 0 displayed everytime...I need to figure this out further yet..
Thanks again tho. I officially love this website
I got it figured out now, thanks again your great !!!!!
Last edited on
Topic archived. No new replies allowed.