Hi im just trying to make a simpel dice roll game which seems very hard because srand always generates the number 0 to me.
I also ask the user to type roll to roll his dices which i try check if he really does type roll.
this is my code:

#include <iostream>
#include <cstdlib>

using namespace std;

int random_integer;
int pcdice1;
int pcdice2;
int humdice1;
int humdice2;

int result (int a,int b)
{
    int res;
    res=a+b;
    return(res);
}


int main()
{
    srand((unsigned)time(0));
    for (int i=0 ;i<100 ; i++){
    int random_integer = (rand()%6)+1;
    random_integer = pcdice1;
    }

    srand((unsigned)time(0));
    for (int i=0 ;i<100 ; i++){
    int random_integer = (rand()%6)+1;
    random_integer = pcdice2;
    }

    int pcdiceres = result(pcdice1,pcdice2);
    cout << "the computer throwed " << pcdice1 << " and \n" << pcdice2 << endl;
    cout << "resulting in: "<< pcdiceres << endl << endl;

    srand((unsigned)time(0));
    for (int i=0 ;i<50 ; i++){
    int random_integer = (rand()%6)+1;
    random_integer = humdice1;
    }

    srand((unsigned)time(0));
    for (int i=0 ;i<50 ; i++){
    int random_integer = (rand()%6)+1;
    random_integer = humdice2;
    }
    char humaninput[100];
    cout << "its your turn to roll, type roll to roll the dices \n";
    while (humaninput != "roll")
    {
        cin >> humaninput;
        if (humaninput != "roll")
        {
            cout <<"please type roll to roll the dice anything else counts as invalid: \n";
        }
    }
    int humdiceres = result(humdice1,humdice2);
    cout << "you throwed " << humdice1 << " and \n" << humdice2 << endl;
    cout << "resulting in: "<< humdiceres << endl;


cout << "Press enter to continue..." << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

also the compiler seems to give the error that it forbids to compare pointers with integers

Which line do you get the pointer comparison error?

In this code:

int random_integer = (rand()%6)+1;
random_integer = pcdice1;

You are storing the random number in random_integer, and then immediately replacing it with pcdice1. That is why you aren't seeing a different number each time.

Dave

Only call srand( ) one time in your program. As fast as this code executes, you are probably going to get the same value for both dice.

Why call rand( ) 100 times, storing the value to random_integer, then (incorrectly) storing that to pdice1 and pdice2? Just call the rand( ) function, storing your computed result directly to the dice variables.

Why are some of your variables declared in global scope? And then passed as parameters to functions? These variables should be declared within main( ).

Generally speaking, avoid global variables as the habit of using them will come back to bite you in the rump later on.

You cannot compare the string humaninput to the literal string with the == operator. You must use a string comparison function.

How do i store the value of random_integer correctly?

You don't need the variable "random_integer". Just store the randomly generate value to the variable you want it in, like pdice1 = rand( ) % 6 + 1; That's it.

One problem you had in your code is that you appeared to be doing the assignment in the wrong direction. Remember LHS <- RHS is the order that assignment works.

thx for the help i got it now:D

if we anyone of you want to try out this mini Roll The Dice game here is the code:
[spoiler]

#include <iostream>
#include <cstdlib>
#include <string.h>


using namespace std;


int result (int a,int b)
{
    int res;
    res=a+b;
    return(res);
}

void game ()
{
    srand((unsigned)time(0));

    cout << "Welcome to roll the dice !!! \n \n";
    cout << "To start the game please type play: \n";
    char playword[] = "play";
    char play[80];
    while (strcmp (playword,play) != 0)
    {
       cin >> play;
        if (strcmp (playword,play) != 0)
        {
            cout <<" \n Please type play to play anything else counts as invalid: \n";
        }
    }
    int pcdice1 = (rand()%6)+1;
    int pcdice2 = (rand()%6)+1;
    int pcdiceres = result(pcdice1,pcdice2);
    cout << " \nThe computer rolled: " << pcdice1 << " and " << pcdice2 << endl;
    cout << "Resulting in: "<< pcdiceres << endl << endl;
    cout << "Its your turn to roll, type roll to roll the dices: \n";
    char keyword[] = "roll";
    char humaninput[80];
    while (strcmp (keyword,humaninput) != 0)
    {
       cin >> humaninput;
        if (strcmp (keyword,humaninput) != 0)
        {
            cout <<"\nPlease type roll to roll the dice anything else counts as invalid: \n";
        }
    }
    int humdice1 = (rand()%6)+1;
    int humdice2 = (rand()%6)+1;
    int humdiceres = result(humdice1,humdice2);
    cout << "you rolled: " << humdice1 << " and " << humdice2 << endl;
    cout << "Resulting in: "<< humdiceres << endl << endl;

    if (humdiceres == pcdiceres){
        cout << "Its a tie! \n ";
    }
    if (humdiceres < pcdiceres){
        cout << "The computer won , by a difference of: " << pcdiceres - humdiceres << endl << "Please try again. \n";
}
    if (humdiceres > pcdiceres){
        cout << "You won, by a difference of: " << humdiceres - pcdiceres << endl << "Congratulations! \n";
    }

    }



int main()
{

    system("color 40");

    game();
    char choice;
    while (choice != 'n'){
    cout << "\nWanna play again? y/n \n";
    cin >> choice;
    cout << "\n";
    switch(choice)
    {
        case 'y':
        {
            game();
        }
        break;
        case 'n':
        {
            cout << "Thank you for playing this game. \n \n";
            break;
        }
        break;
        default:
        cout << "Error: invalid input... now closing... \n \n";
        system("pause");
        return (0);

    }
    }

system("pause");
}

[/spoiler]

Now you should put a loop in main that actually lets the player play again.

it does let the player play again weither or not he types y (yes) or n(no)
see : while (choice != 'n'){

oops, yes it does. hard to see with the indenting scheme you used. how about:

int main()
{

   //system("color 40");

   game();
   char choice = 'y';

   while (choice != 'n')
   {
      cout << "\nWanna play again? y/n \n";
      cin >> choice;
      cout << "\n";
      switch(choice)
      {
      case 'y':
         {
            game();
         }
         break;
      case 'n':
         {
            cout << "Thank you for playing this game. \n \n";
            break;
         }
         break;
      default:
         cout << "Error: invalid input... now closing... \n \n";
         system("pause");
         return (0);
      }
   }

   system("pause");
}

Don't be afraid to use a little vertical whitespace to separate chunks of code.
And that setting the background to dark red really makes the program hard to read.

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.