Beginner coder -- first C++ class.
I keep getting errors at line 75 onward, and I cannot figure out where I went wrong. I know my code is pretty sticky...I need help on cleaning it up. Anyone know how I can code this correctly?
This is the assignment:
Dice (if/else, loop, random):
Using if/else, loops, and random numbers, write a Dice Game program where the user rolls 2 dice and decides whether they want to "hold" those numbers or roll again. It should let the user roll as many times as they like until they hold.
The computer then should roll 2 dice as well. The object of the game is to get a higher score than the computer.
===
Hint: use random numbers b/w 1-6, make 4 random numbers
When I tried to build the program, I get a bunch of errors and don't really know what I am doing wrong (tried to debug myself). Here are the errors:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
||In function 'int main()':|
|75|error: expected 'while' before 'cout'|
|75|error: expected '(' before 'cout'|
|75|error: expected ')' before ';' token|
|87|error: expected 'while' before 'While'|
|87|error: expected '(' before 'While'|
|87|error: 'While' was not declared in this scope|
|87|error: expected ')' before ';' token|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Here is the code I have so far--can anyone show me the correct code?:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
srand(time(0));
int Roll1 = 1+(rand()%6);
int Roll2 = 1+(rand()%6);
int UserRoll = Roll1 + Roll2;
int Roll3 = 1+(rand()%6);
int Roll4 = 1+(rand()%6);
int ComputerRoll = Roll3 + Roll4;
string Hold;
string ThrowDice;
string Again;
do
{
do
{
cout << "Please enter \"throw\" (lowercase) to roll the dice: ";
cin >> ThrowDice;
} while(ThrowDice != "throw");
do
{
cout << "You rolled: " << Roll1 << " and " << Roll2 << endl;
cout << "Would you like to hold onto these numbers or roll again? " << endl;
cout << "Enter (lowercase) \"hold\" to keep this roll, or \"throw\" to roll again): ";
cin >> Hold;
if (Hold != "hold" && Hold != "throw")
{
cout << "Your choice isn't valid! Please try again by typing \"hold\" or \"throw\": " << endl;
}
}while (Hold != "hold" && Hold != "throw");
do
{
if (Hold == "hold")
{
cout << "You chose to keep these numbers, totaling: " << UserRoll << endl;
cout << "The computer rolled " << Roll3 << " and " << Roll4 << " totaling: " << ComputerRoll << endl;
} break;
if (Hold == "throw")
{
cout << "You chose to roll again. " << endl;
}
}while (Hold == "throw");
do
{
if (UserRoll < ComputerRoll)
{
cout << "Sorry. You lose. " << endl;
} break;
if (ComputerRoll < UserRoll)
{
cout << "Congratulations! You win! " << endl;
} break;
if (ComputerRoll == UserRoll)
{
cout << "It's a draw. " << endl;
} break;
}
cout << "Would you like to play again? [Yes/No]: " << endl;
cin >> Again;
if (Again == "Yes")
{
continue;
}
else if (Again == "No")
{
break;
}
}While (Again == "Yes");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire