TheOneWhoisDrunk's C++ Adventures

Hey TekSyndicate Forum.
I am in the process of learning C++ and I would really appreciate some help if I ever get stuck on some projects I am doing.

As for now, the project I am currently working on is a Dice Rolling game where the program will seed a random number for two dice and will display the outcome as well as add them up and give a response as to what the two dice are.

So far the program does what I want it to and it will loop if the user inputs 'y' but the problem I have run into is that I am getting the result of die1 the same as die2. I tried to distinguish them by separating the call function into two functions but I am getting the same result.

Here is the source code so far:
http://puu.sh/keecS/54f79f229d.txt

1 Like

Great Effort :) I'm also in the process of learning both Ruby, and Haskell; I find Ruby an accessible programming script.

1 Like

Here's the issue:

int die1 = rand() % 6 + 1; //random dice 1
int die2 = rand() % 6 + 1; //random dice 2

This doesn't generate an evenly distributed random numbers. rand() returns a number from [0, RAND_MAX) or [0, 32767) which is not exactly divisible by 6.

This is the proper way to roll a dice:

int dieRoll() {
     static int const max = RAND_MAX/6*6;

     int r = rand();

     while(r >= max) { 
         r = rand();
     }

     return r%6+1;
}

Full code:

#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int dieRoll();

int main() {
	srand(time(NULL)); //seed number
	char re;
	
	cout << "Lets roll some dice!\n";

	do {
		int die1 = dieRoll();
		int die2 = dieRoll();
		cout << "\nYour first die is a " << die1 << "\nYour second die is a " << die2 << endl;
		cout << "Your sum of the dice is " << die1 + die2 << endl;
		if (die1 == die2) {
			cout << "\nDoubles!";
		} else if ((die1 + die2) == 2) {
			cout << "\nSnake Eyes!";
		} else {
			cout << "\nNothing special.";
		}

		cout << "\nWould you like to roll again? (y or n)\n\n";
		cin >> re;
	} while (re == 'y');
}

int dieRoll() {
	static int const max = RAND_MAX/6*6;

    int r = rand();

    while(r >= max) { 
        r = rand();
    }

    return r%6+1;
}
2 Likes

Ah, thank you very much. I now understand why I was always getting the same values regardless of "re-rolling the dice"
I had declared the calculation globally and the program save that calculation globally instead of the calculation taking place every time in the do {codeblock}
I don't really understand the math behind the number generator besides making it easier to divide it by 6.
Thank you again for leading me in the right direction.

Having fun coding this project! I find this one easier to code than the dice rolling program.
http://puu.sh/kfkAF/f729be2c1e.txt
Its a calculator for finding roots of a quadratic equation. No bumps in the road so far but I think my code is a little messy, especially with the call function dicrim(). Just have to code the function for finding the imaginary roots.

In probability a discrete uniform distribution is a symmetric probability distribution whereby a finite number of values are equally likely to be observed; every one of n values has equal probability 1/n.
Where in this case n = 6, so each "face" of our dice has the same probability to be rolled, i.e. 1/6.
If you didn't know this, you'll probably study it in a statistics / probability course.

You don't need to declare these as global variables:

double z;
double x1;
double x2;
double xonly;

Plus you don't need to return a double here double discrim(int a, int b, int c); so I made it void.
Another thing is that in your code, here:

if ( re == 'y')
{
    cout << "\nEnter your coeffecients of your quadratic.\n";
}

You ask to enter the coefficients, but I already entered them before and you don't actually return the roots of the equation I gave you as an input. So I fixed that too.

I did some other changes, but they are straightforward, I shouldn't need to explain them. But if you want, just ask.

Full code here: http://pastebin.com/h14kMczv

P.S.: I used your indentation style, even tho I don't like it.

Thank you, also I am still working on the code for returning the roots and I will display them once I write the code to return the imaginary roots of a quadratic. The if else in the main is just for displaying the quadratic nicely and since positive integers wont include a + in in front of them I made it so it would and regardless of entering a negative its going to display the way the quadratic is written.
Thank you for the suggestions about the void and not having the integers/doubles defined globally and instead locally. Also I will be adopting the way the brackets are in your code. It seems that it is a more widely used way of enclosing code blocks from other source code I have seen. Again thank you very much for the tips and leading me into a good direction.

Okay so I finished writing the code for complex solutions and it does display the information that I want it to.
http://puu.sh/kgwEA/e36a46ebf7.txt
I have run into a problem where outputting the roots for two roots and complex roots ends up adding the discriminant a line below the solutions like so

It works well for the single root since it has return xonly; at the end of the code block. Any way around the extra line of discriminant?
I could not make the call function void discrim(int a, int b, int c) since it would end up with me unable to execute cout << discrim(a, b, c); which displays the roots.

Yes you can make it void. You don't need to return roots, because you already print them.
So change the discrim to void, delete all the returns and change cout << discrim(a, b, c) << endl; to discrim(a, b, c);

Plus you can't return multiple values in a function. You can't do return a, b, c, d, e, .... you can only return one value.

Now the compiler doesn't rise up any errors because the expression return r1, i1, r2, i2; (or return x1, x2;) is logically correct. But when you do:

cout << discrim(a, b, c) << endl;

This will translate into:

  • execute the discrim(a, b, c) function, which it does and it prints the roots as expected;
  • print (cout << ... << endl; means print into the standard output) the return value of the discrim function, which ends up printing that extra line.

Full code: http://pastebin.com/8xLEticH

It's the second time I see you returning multiple values from a function, I don't get where you got that from.

I hope my explanation make sense.

Thank you and I understand now. It was not occurring to me that the void function was already printing my roots.

Just remember what you are creating isn't really an random number, but if you someday are able to create a true random number, me and the rest of the Dev world will want to steal that code from you :P.

Yeah ha ha. My professor was explaining how complicated it would be to develop a true random number.

Long story short: a machine (such as a computer) can only generate pseudo-random numbers because they are generated in a predictable fashion using a mathematical formula or an algorithm which is very predictable.

Ha this is a good one to use :D

And ironically it's probably one of the best methods.

and just for the hell of it

The Ultimate Top 25 Chuck Norris “The Programmer” Jokes

Hmm, I was very hesitant to post my problems here because I though I have to figure them by myself, but so far I see that quite some explanation is provided for the issue. Keep doing this @Theonewhoisdrunk

1 Like

Okay so I was messing with arrays, [LINK TO CODE] and the program works as intended but I keep getting the error Run-Time Check Failure #2 when I exit the shell through Visual Studio.
When I compile it at http://cpp.sh/ I get no such errors and the program still works as intended. Perhaps something to do with Visual Studio's compiling?

Does it give you something else after Run-Time Check Failure #2?

Just a -S after and points to line 31 which is the closing bracket in my main.

Mmh might be not linked to your issue but this is incorrect: double average(int a[3], int size) it should be double average(int a[], int size).

1 Like

Ah thank you, and no not linked, same error still pops up but now does not point to any line.