samedi 29 janvier 2022

Generating a random number for players in a dice game

I am writing a simple dice game where two players play against each other. The winner is the one who gets the largest number when the dice is rolled. However, when I press enter to make a random number get generated for the first player, I just get new empty lines. Here is the code:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    srand(time(0));
    
    int p1 = (rand() % 6) + 1;
    cout << "Player 1: Press enter to roll your dice" << endl;
    cin >> p1;

    
    int p2 = (rand() % 6) + 1;
    cout<<"Player 2: Press enter to roll your dice" << endl;
    cin >> p2; 
    

     if(p1 == p2) 
      {
          cout<<"You tied! \n";
      }
      
      else if(p1 > p2)
      {
          cout<<"You won! \n";
      }
      
      else
      {
          cout<<"You lost! \n";
      }
}



Aucun commentaire:

Enregistrer un commentaire