I don't know why this code wont build. I am using xcode and it has to be in C++ //
// NickSchmidtProj3>cpp
// NickSchmidtProj3.Cpp //
// Created by Nick Schmidt on 9/18/19.
// Copyright © 2019 Nick Schmidt. All rights reserved.
//
#include <iostream>
using namespace std;
int ()
{
//declare variables of integer type and set the tooth_pick_left is 23
int user_pick, computer_pick;
int tooth_pick_left;
//int user_tooth_pick
cout << "let's play a game of "23"! \n";
//using do while loop to prompt until the user_pick is less than 4 do
{
//using another do while loop, compute untill tooth_pick_left isless than 0
do
{
//prompt player to play
cout << "It is your turn! pick your stick(s).";
cin >> user_pick;
if (user_pick < 1 || user_pick >3)
cout << "Error! You can pick 1, 2, or 3 sticks only! \n";
}while (user_pick > 1 || user_pick < 4);
//compute total_pick_left
tooth_pick_left = tooth_pick_left - user_pick;
//check the condition for computer_draw
//tooth_pick_left greater than 4 then pick 4-x
if (tooth_pick_left > 4)
{
computer_pick = 4 - user_pick;
}
//check the condition for sticks left between 2 and 4
else if (tooth_pick_left >= 2 && tooth_pick_left <= 4)
{
if (tooth_pick_left == 2)
computer_pick = tooth_pick_left - 1;
if (tooth_pick_left == 3)
computer_pick = tooth_pick_left - 2;
if (tooth_pick_left == 4)
computer_pick = tooth_pick_left - 3; }
//if computer draw is 1 then computer loses
else if (tooth_pick_left == 1)
{
computer_pick = 1;
cout << "Congratulations!! You Won The Game!!\n"; }
else
{
cout << "The Computer won the game... Sorry, Better luck next tim!\n'" break;
}
//display computer turn
cout << "Computers Turn:" << computer_pick
tooth_pick_left = tooth_pick_left - computer_pick;
//display the left toothpick
cout << " Tooth picks left: " << tooth_pick_left ;
}
while (tooth_pick_left > 0)
return 0;
}
Answer:
Errors have been pointed out using comments
#include <iostream>
using namespace std;
int main() // this will int main() instead of just int()
{
//declare variables of integer type and set the tooth_pick_left is 23
int user_pick, computer_pick;
int tooth_pick_left=23;
//int user_tooth_pick
cout << "let's play a game of '23'! \n";
//using do while loop to prompt until the
user_pick is less than 4
do
{
//using another do while loop, compute until tooth_pick_left is less than 0
do
{
//prompt player to play
cout << "\nIt is your turn! pick your stick(s): ";
cin >> user_pick;
if (user_pick < 1 || user_pick >3)
cout << "Error! You can pick 1, 2, or 3 sticks only!
\n";
else
//give a break statement in the else condition otherwise the
program will only keep
break;
//asking for your input
}
while (user_pick > 0
|| user_pick < 4); /*user_pick must be > 0 and not > 1
because if you do the latter then even if you
input 1 it will again ask you for input */
//compute total_pick_left
tooth_pick_left = tooth_pick_left - user_pick;
//check the condition for computer_draw
//tooth_pick_left greater than 4 then pick 4-x
if (tooth_pick_left > 4)
{
computer_pick = 4 - user_pick;
}
//check the condition for sticks left between 2 and 4
else if (tooth_pick_left >= 2 && tooth_pick_left <= 4)
{
if (tooth_pick_left == 2)
computer_pick = tooth_pick_left - 1;
if (tooth_pick_left == 3)
computer_pick = tooth_pick_left - 2;
if (tooth_pick_left == 4)
computer_pick = tooth_pick_left - 3;
}
//if computer draw is 1 then computer loses
else if (tooth_pick_left == 1)
{
computer_pick = 1;
cout << "\nCongratulations!! You Won The Game!!\n";
}
else
{
cout << "\nThe Computer won the game... Sorry, Better luck
next time!\n";
break;
}
//display computer turn
cout << "\nComputers Turn: " << computer_pick; //you forgot the semicolon here
tooth_pick_left = tooth_pick_left - computer_pick;
//display the left toothpick
cout << " \nTooth picks left: " << tooth_pick_left ;
}
while (tooth_pick_left > 0);
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.