Answer to Question #57613 in C++ for William

Question #57613
Write code that simulates the roll of two dice. If the two dice have the same value, output the statement "Doubles. Roll again." Otherwise, print "Your turn is over.";
1
Expert's answer
2016-02-10T00:01:18-0500
// This program simulates the roll of two dice. If the two dice have the same value,
// output the statement "Doubles. Roll again." Otherwise, print "Your turn is over.";

// input: command "roll" or "quit")

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


int main()
{
int dice1;
int dice2;
std::string command;

srand ( time(NULL) );

do
{
std::cout << "Enter your command (\"quit\" or \"roll\"): ";
std::cin >> command;
if (command == "roll" )
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
std::cout << " dice1 = " << dice1 << std::endl;
std::cout << " dice2 = " << dice2 << std::endl;
if ( dice1 != dice2 )
{
std::cout << "Your turn is over." << std::endl;
}
else
{
std::cout << "Doubles. Roll again." << std::endl;
}
}
else if (command == "quit" )
{
return 0;
}
else
{
std::cout << "You have entered wrong command. " << std::endl;
}

}while(true);

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS