Код для начинающих - первый класс C ++.
Я продолжаю получать сообщения об ошибках в строке 75 и не могу понять, где я ошибся.Я знаю, что мой код довольно липкий ... Мне нужна помощь в его очистке.Кто-нибудь знает, как я могу кодировать это правильно?
Это назначение:
Dice (if / else, loop, random):
Использование if / else, циклы и случайные числа, напишите программу «Игра в кости», в которой пользователь бросает 2 кубика и решает, хотят ли они «держать» эти числа или бросить снова.Это должно позволить пользователю катиться столько раз, сколько ему нужно, пока он не удержится.
Затем компьютер должен бросить еще 2 кубика.Цель игры - набрать больше очков, чем у компьютера.
===
Подсказка: используйте случайные числа от 1 до 6, сделайте 4 случайных числа
Когда я пытался собрать программу, я получаю кучу ошибок и не знаю, что я делаю неправильно (пытался отлаживать себя).Вот ошибки:
||=== 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)) ===|
Вот код, который у меня есть, может ли кто-нибудь показать мне правильный код?
#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;
}