Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was just programming in c++, when all of a sudden all the "cout"s and "cin"s were errors and "Ambiguous". Including System.
I don't know why this happened. Everything was fine, I was coding the same program for about 2 hours, when it just... happened.
EDIT
I can still run the program without errors, but they show as errors on the text, the red scribbly line. What happened?
I'm using the Visual Studio 2013 IDE, whatever it comes with.
#include <iostream>
#include <ctime>
#include <string>
#include <Windows.h>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
int main()
{
struct Gun
{
string name;
int damage;
int cost;
bool purchased;
bool equipped;
} M4A1, FAMAS;
//WEAPONS INFO
M4A1.cost = 50;
M4A1.damage = 5;
M4A1.purchased = false;
M4A1.equipped = false;
FAMAS.cost = 300;
FAMAS.damage = 10;
FAMAS.purchased = false;
FAMAS.equipped = false;
//WEAPONS INFO
//-----PLAYER(BEGIN)-----
struct Player
{
int health;
string name;
int money;
int energy;
string l_a;
string r_a;
string l_l;
string r_l;
string rank;
} Player;
//GAME PLAYER BEGIN
Player.l_a = "Normal";
Player.r_a = "Normal";
Player.l_l = "Normal";
Player.r_l = "Normal";
Player.health = 100;
Player.money = 100;
Player.energy = 100;
string plyrname;
string rank = "Private";
Player.name = plyrname;
//GAME PLAYER END
//-----PLAYER(END)-----
cout << "What is your name? ";
cin >> plyrname;
goto mmenu;
mmenu:
//-----MAIN MENU(BEGIN)-----
system("CLS");
if (Player.l_a == "Damaged")
{
cout << "Your Left Arm is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.r_a == "Damaged")
{
cout << "Your Right Arm is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.l_l == "Damaged")
{
cout << "Your Left Leg is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.r_l == "Damaged")
{
cout << "Your Right Leg is damaged! Sleep for a while to fix it!";
Sleep(1600);
}
if (Player.money >= 500 && Player.rank == "Private")
{
system("CLS");
cout << "You have been promoted to Private 2!";
Player.rank = "Private 2";
Sleep(1600);
}
if (Player.money >= 1000 && Player.rank == "Private 2")
{
system("CLS");
cout << "You have been promoted to Private First Class!";
Player.rank = "Private First Class";
Sleep(1600);
}
system("CLS");
cout << "Health: " << Player.health << ". Money: " << Player.money << " dollars." << " Energy: " << Player.energy;
if (M4A1.equipped)
cout << "\nGun: M4A1";
if (FAMAS.equipped)
cout << "\nGun: FAMAS";
cout << "\n\nRank: " << Player.rank;
cout << "\n\n1) Go to Gunstore\n2) Sleep\n3) Fight\n\nAction: ";
int mmenuch1;
cin >> mmenuch1;
if (mmenuch1 == 1)
{
goto gunstore;
}
if (mmenuch1 == 2)
{
system("CLS");
cout << "You sleep, restoring your energy.";
Player.energy = 100;
if (Player.l_a == "Damaged")
{
cout << "\n\nYour Left Arm was healed.";
Player.l_a = "Normal";
}
if (Player.r_a == "Damaged")
{
cout << "\n\nYour Right Arm was healed.";
Player.r_a = "Normal";
}
if (Player.l_l == "Damaged")
{
cout << "\n\nYour Left Leg was healed.";
Player.l_l = "Normal";
}
if (Player.r_l == "Damaged")
{
cout << "Your Right Leg was healed.";
}
Sleep(1400);
goto mmenu;
}
if (mmenuch1 == 3)
{
system("CLS");
goto fight;
}
//-----MAIN MENU(END)-----
fight:
srand(time(0));
system("CLS");
if (Player.r_a == "Damaged" || Player.r_l == "Damaged" || Player.l_a == "Damaged" || Player.l_l == "Damaged")
{
cout << "You're injured, sleep to heal.";
Sleep(1400);
goto mmenu;
}
if (Player.energy < 40)
{
cout << "You don't have enough energy to fight.";
Sleep(1400);
goto mmenu;
}
if (M4A1.equipped == false && FAMAS.equipped == false)
{
cout << "You don't have a gun equipped.";
Sleep(1400);
goto gunstore;
}
if (M4A1.equipped == true && Player.energy > 40)
{
int randnum1 = rand() % (M4A1.damage - 2 + 1) + 2;
Player.money = Player.money + (randnum1 * 15);
Player.energy = Player.energy - 40;
int randnum3 = rand() % (10 - 1 + 1) + 1;
if (randnum3 < 4)
{
int randnum4 = rand() % (13 - 1 + 1) + 1;
if (randnum4 < 3)
{
Player.l_a = "Damaged";
}
if (randnum4 <= 6 && randnum4 >= 4)
{
Player.r_a = "Damaged";
}
if (randnum4 <= 9 && randnum4 >= 7)
{
Player.l_l = "Damaged";
}
if (randnum4 <= 13 && randnum4 >= 10)
{
Player.r_l = "Damaged";
}
}
cout << "You fight, killing " << randnum1 << " enemies, making " << randnum1 * 15 << " dollars!";
Sleep(1600);
goto mmenu;
}
if (FAMAS.equipped == true && Player.energy > 40)
{
int randnum2 = rand() % (FAMAS.damage - 4 + 1) + 4;
Player.money = Player.money + (randnum2 * 15);
Player.energy = Player.energy - 40;
int randnum5 = rand() % (10 - 1 + 1) + 1;
if (randnum5 < 4)
{
int randnum6 = rand() % (13 - 1 + 1) + 1;
if (randnum6 < 3)
{
Player.l_a = "Damaged";
}
if (randnum6 <= 6 && randnum6 >= 4)
{
Player.r_a = "Damaged";
}
if (randnum6 <= 9 && randnum6 >= 7)
{
Player.l_l = "Damaged";
}
if (randnum6 <= 13 && randnum6 >= 10)
{
Player.r_l = "Damaged";
}
}
cout << "You fight, killing " << randnum2 << " enemies, making " << randnum2 * 15 << " dollars!";
Sleep(1600);
goto mmenu;
}
//-----GUNSTORE(BEGIN)-----
gunstore:
system("CLS");
cout << "Welcome to the gunstore! You have " << Player.money << " dollars.";
cout << "\n\n1)M4A1 | Assault Rifle | $50\n2)FAMAS | Assault Rifle | $300\n\n3)Back\n\nAction: ";
int gschoice1;
cin >> gschoice1;
if (gschoice1 == 1)
{
goto prchs_M4A1;
}
else if (gschoice1 == 2)
{
goto prchs_FAMAS;
}
else if (gschoice1 == 3)
{
goto mmenu;
}
prchs_M4A1:
system("CLS");
if (M4A1.purchased == true)
{
cout << "You already purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice6;
cin >> gschoice6;
if (gschoice6 == 1)
{
system("CLS");
M4A1.equipped = true;
FAMAS.equipped = false;
goto mmenu;
}
else if (gschoice6 == 2)
{
goto gunstore;
}
}
if (Player.money >= 0)
{
system("CLS");
cout << "Would you like to buy the M4A1?";
cout << "\n\n1)Yes\n2)No\n\nAction: ";
int gschoice2;
cin >> gschoice2;
if (gschoice2 == 1)
{
system("CLS");
Player.money = Player.money - M4A1.cost;
M4A1.purchased = true;
cout << "You've purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice3;
cin >> gschoice3;
if (gschoice3 == 1)
{
system("CLS");
M4A1.equipped = true;
FAMAS.equipped = false;
cout << "You've equipped the M4A1";
Sleep(1400);
goto gunstore;
}
if (gschoice3 == 2)
{
system("CLS");
M4A1.equipped = false;
goto gunstore;
}
}
if (gschoice2 == 2)
{
system("CLS");
goto gunstore;
}
}
else if (Player.money < 0)
{
system("CLS");
cout << "You don't have enough money.";
Sleep(1400);
goto gunstore;
}
prchs_FAMAS:
if (FAMAS.purchased == true)
{
cout << "You already purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice7;
cin >> gschoice7;
if (gschoice7 == 1)
{
system("CLS");
FAMAS.equipped = true;
M4A1.equipped = false;
goto mmenu;
}
else if (gschoice7 == 2)
{
goto gunstore;
}
}
if (Player.money >= 100)
{
system("CLS");
cout << "Would you like to buy the FAMAS?";
cout << "\n\n1)Yes\n2)No\n\nAction: ";
int gschoice4;
cin >> gschoice4;
if (gschoice4 == 1)
{
system("CLS");
Player.money = Player.money - FAMAS.cost;
FAMAS.purchased = true;
cout << "You've purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
int gschoice5;
cin >> gschoice5;
if (gschoice5 == 1)
{
system("CLS");
FAMAS.equipped = true;
M4A1.equipped = false;
cout << "You've equipped the FAMAS";
Sleep(1400);
goto gunstore;
}
if (gschoice5 == 2)
{
system("CLS");
FAMAS.equipped = false;
goto gunstore;
}
}
if (gschoice4 == 2)
{
system("CLS");
goto gunstore;
}
}
else if (Player.money < 100)
{
system("CLS");
cout << "You don't have enough money.";
Sleep(1400);
goto gunstore;
}
//-----GUNSTORE-----
}
This kind of thing doesn't just magically happen on its own; you changed something! In industry we use version control to make regular savepoints, so when something goes wrong we can trace back the specific changes we made that resulted in that problem.
Since you haven't done that here, we can only really guess. In Visual Studio, Intellisense (the technology that gives you auto-complete dropdowns and those squiggly red lines) works separately from the actual C++ compiler under the bonnet, and sometimes gets things a bit wrong.
In this case I'd ask why you're including both cstdlib and stdlib.h; you should only use one of them, and I recommend the former. They are basically the same header, a C header, but cstdlib puts them in the namespace std in order to "C++-ise" them. In theory, including both wouldn't conflict but, well, this is Microsoft we're talking about. Their C++ toolchain sometimes leaves something to be desired. Any time the Intellisense disagrees with the compiler has to be considered a bug, whichever way you look at it!
Anyway, your use of using namespace std (which I would recommend against, in future) means that std::system from cstdlib now conflicts with system from stdlib.h. I can't explain what's going on with std::cout and std::cin.
Try removing #include <stdlib.h> and see what happens.
If your program is building successfully then you don't need to worry too much about this, but I can imagine the false positives being annoying when you're working in your IDE.
Related
I found an RPS game and wanted to improve on it by creating a scoring system but the //int compwin = compwin + 1; keeps giving me errors. I am still fairly new to coding in C++ and have no clue where the problem is standing so thanks for helping in advance. Also this code was taken from somewhere this is not my code but I wanted to try and improve on it.
#include <iostream>
#include <cstdlib>
#include <limits>
#include <ctime>
#include <string>
using namespace std;
int main()
{
int choice;
int i;
int y;
int Y;
int comp;
char res;
int compwin = 0;
int choicewin = 0;
unsigned seed;
while (1==1) {
//The choices
cout << "Game Choices.\n\n";
cout << "1.Rock\n";
cout << "2.Paper\n";
cout << "3.Scissors\n";
cout << "4.Quit, exits the game.\n\n";
cout << "Please enter your choice.\n\n";
cin >> choice;
//-----------------------Player Imputs-----------------------------------
if (choice == 1) //Rock
{
cout << "You picked Rock.\n";
cout << "Now here was my choice.\n\n";
}
else if (choice == 2) //Paper
{
cout << "You picked Paper.\n";
cout << "Now here was my choice.\n\n";
}
else if (choice == 3) //Scissors
{
cout << "You picked Scissors.\n";
cout << "Now here was my choice.\n\n";
}
else if (choice == 4)
{
return 0;
}
else if (choice != 1 || 2 || 3 || 4) // Debug
{
cout << "Uhhhh thats not one of the following.\n\nGoodbye!\n\n";
system("pause");
return 0;
}
//-------------------------Computer Choice-------------------------------
seed = time(0);
srand(seed); //RNG TIME
comp = rand() % 3 + 1; //Computer picks
if (comp == 1) //Computer rock
{
res = 1;
cout << "Rock!\n\n";
}
else if (comp == 2) //Computer paper
{
res = 2;
cout << "Paper!\n\n";
}
else if (comp == 3) // Computer scissors
{
res = 3;
cout << "Scissors!\n\n";
}
//-----------------------Victory Conditions------------------------------
if (comp == 1 && choice == 1) {
std::cout << "It was a tie!" << endl;
}
else if (comp == 1 && choice == 3) {
std::cout << "I Won! Better luck next time!" << endl;
//int compwin = compwin + 1; This is where the problem occurs
}
else if (comp == 2 && choice == 2) {
std::cout << "It was a tie!" << endl;
}
else if (comp == 2 && choice == 1) {
std::cout << "I Won! Better luck next time!" << endl;
//int compwin = compwin + 1; This is where the problem occurs
}
else if (comp == 2 && choice == 3) {
std::cout << "It was a tie!" << endl;
}
else if (comp == 2 && choice == 2) {
std::cout << "I Won! Better luck next time!" << endl;
//int compwin = compwin + 1; This is where the problem occurs
}
else {
std::cout << "Congrats! You won!" << endl;
//int choicewin = choicewin + 1; This is where the problem occurs
}
cout << "Heres the score, computer =" << compwin << "and player =" << choicewin;
cout << "Want to go again? (y/n)";
cin >> res;
system("cls");
}
while (res == y || Y);
system("pause");
return 0;
}
This
int compwin = compwin + 1;
is a declaration and initialization. A rather weird one, and to discuss why it is allowed would take us on a too big detour. So lets look at a simpler example:
int compwin = 1; // declares compwin and initializes it with 1
int compwin = 2; // compiler error, because compwin is already declared
You can only declare the same variable once (again the long complete truth is more involved, search for "shadowing" in case you care). You also only need to initialize a variable only once.
If you want to assign something to an already declared variable, you use assignment, as in
int compwin = 1; // declare and initialize
compwin = 2; // assign
Further note that
else if (choice != 1 || 2 || 3 || 4) // Debug
is not doing what you expect. The correct way is
else if (choice != 1 && choice != 2 && choice != 3 && choice != 4)
The mistake is a combination of ignoring De Morgan's Law and a wrong combination of several conditions. The operator|| expects a bool on both sides and unfortunately numbers happily convert to true (only 0 becomes false), but a good compiler may spit out warnings on that. Similar mistake is on while (res == y || Y);.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm simply trying to get the user to put in their name/age and verify if it's correct. If not then they get 4 tries before the program will abort. However my while loops don't loop, instead they just continue on to the next loop. I've tried a variation of things inside the while parenthesis (op != 1) (!(op = 1)) etc.
int main() {
system("Color 0A");
string name;
int age;
int tries = 0;
int op = 0;
cout << "Hello User" << endl;
Sleep(3000);
while ((op != 1) && (tries < 4)) {
name = entName(name);
cout << "So your name is " << name << "?" << endl;
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op;
if (op == 1) {
cout << "Perfect!";
}
if (op == 2) {
cout << "Please Try Again!";
tries+ 1;
}
if (tries = 4) {
//abort the program
}
}
int op2 = 0;
int tries2 = 0;
while ((op2 != 1) && (tries2 < 4)) {
op2 = 3;
age = entAge();
cout << "So you are " << age << " years old?" << endl;
while ((op2 != 1) && (op2 != 2)) {
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op2;
if (op2 == 1) {
cout << "Perfect!\n";
}
if (op2 == 2) {
cout << "Please Try Again!\n";
tries2++;
}
if (tries2 = 4) {
//abort the programhi
}
}
}
return 0;
}
I'm fairly new to C++ so I'm sorry if it does have a simple answer. But anyway, I've been debugging this for over half an hour and I looked online for 20+ minutes.
if (tries = 4) {
//abort the program
}
Change this to
if (tries == 4) {
//abort the program
}
And
f (op == 2) {
cout << "Please Try Again!";
tries+= 1; // tries+ 1;
}
You can increment value in C++ like this tries+ 1;. Either use tries+= 1; or tries++;
tries+ 1; should be tries += 1; or tries++;
And,
if (tries = 4) {
//abort the program
}
should be:
if (tries == 4) {
//abort the program
}
Your program should look like this:
int main()
{
system("Color 0A");
string name;
int age;
int tries = 0;
int op = 0;
cout << "Hello User" << endl;
Sleep(3000);
while ((op != 1) && (tries < 4)) {
name = entName(name);
cout << "So your name is " << name << "?" << endl;
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op;
if (op == 1) {
cout << "Perfect!";
}
if (op == 2) {
cout << "Please Try Again!";
tries+= 1;
}
if (tries == 4) {
//abort the program
}
}
int op2 = 0;
int tries2 = 0;
while ((op2 != 1) && (tries2 < 4)) {
op2 = 3;
age = entAge();
cout << "So you are " << age << " years old?" << endl;
while ((op2 != 1) && (op2 != 2)) {
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op2;
if (op2 == 1) {
cout << "Perfect!\n";
}
if (op2 == 2) {
cout << "Please Try Again!\n";
tries2++;
}
if (tries2 == 4) {
//abort the programhi
}
}
}
You have forget to use = sign at multiple places. tries = 4 should be tries == 4 for comparing variable tries with numeric 4. tries = 4 was reassigning the variable tries to four and your while loop was getting terminated after it's first run. Also, tries + 1 should be tries += 1 or tries++ to increment the value of tries variable by one.
Having issues with my code not working. I'm trying to get it to exit the do while by pressing any key on the keyboard but without hitting enter. My code snippet is below.
void LemonadeBuyANDCheck()
{
int iCupsToBuy = rand() % (MAX_CUPS - MIN_CUPS) + MIN_CUPS;
bool continueLoop = true;
do
{
int Userinput = 1;
Userinput = !_kbhit();
fflush(stdin);
if (Userinput == 1)
{
continueLoop = false;
}
StoreIsOpen();
int randomNumber = rand() % 100;
if (iCupsOfLemonadeInStock == 0)
{
cout << "Customer came to buy some lemonade but you didnt have any in stock :(\n";
//continueLoop = false;
}
else if (randomNumber <= 25)
{
cout << "Wow your lemonade is way too expensive!\n";
iCustomerCounter++;
}
else if (randomNumber >= 75)
{
cout << "Eww your recipe for lemonade... Wow..\n";
iCustomerCounter++;
}
else if (iCupsOfLemonadeInStock >= iCupsToBuy)
{
cout << "You sold " << iCupsToBuy << "cups of lemonade!\n";
iCupsOfLemonadeInStock = iCupsOfLemonadeInStock - iCupsToBuy;
iCupsOfLemonadeSold = iCupsOfLemonadeSold - iCupsToBuy;
//Money
fMoneyEarned += (iCupsToBuy * fCurrentLemonadePrice);
fCurrentMoney += (iCupsToBuy * fCurrentLemonadePrice);
iCustomerCounter++;
}
else if (iCupsOfLemonadeInStock < iCupsToBuy)
{
cout << "You didnt have enough lemonade in stock so you only sold " << iCupsToBuy - iCupsOfLemonadeInStock << endl;
fMoneyEarned += (iCupsOfLemonadeInStock * fCurrentLemonadePrice);
fCurrentMoney += (iCupsOfLemonadeInStock * fCurrentLemonadePrice);
iCupsOfLemonadeInStock = 0;
iCustomerCounter++;
}
} while (continueLoop != true);
fflush(stdin);
GameMenuSelectionVerifier();
}
I think you can use a function named "kbhit", it's in the "conio.h" header file. You can click this for details. And you can modify it like this:
while(!kbhit());
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
here is my main . I get the error for the part I am checking whether the game had a winner or not. the errors are referring to the if parts where i wanna check the returned data from chkwin method
#include<iostream>
#include<string>
#include "TicTacToe.h"
using namespace std;
int main()
{
string choice = "";
string name1 = "";
string name2 = "";
string change = "";
int choice_num;
TicTacToe game;
cout << "Welcome to TicTacToe World!\n"
<< "In order to Start Please Enter the name of the first player\n\n";
getline(cin, name1);
cout << "\nGreat, Now Please Enter the name of the second player\n\n";
getline(cin,
name2);
cout << "\nAwesome Let's Get Started!\n";
do
{
cout << "\nPlease choose what do you want to do by entering the number of your choice\n"
<< "1.Start the game.\n"
<< "2.Change the names.\n"
<< "3.View Scores\n"
<< "4.Exit the Game :(\n\n";
getline(cin, choice);
if (choice == "1")
{
for (int i = 1; 1 <= 9; i++)
{
if (i % 2 != 0)
{
cout << "It's " << name1 << " Turn. Please Make Your move.";
cin >> choice_num;
cin.ignore();
game.setMove(choice_num, 1);
if (game.chkWin == 1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout <<"The Game is a Draw! ";
}
}
else
{
cout << "It's " << name1 << " Turn. Please Make Your move.";
cin >> choice;
cin.ignore();
game.setMove(choice_num, 2);
if ( 1 == game.chkWin)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout << "The Game is a Draw! ";
}
}
}
}
else if (choice == "2")
{
do
{
cout << "\nwhich player do you want its name to be changed? (Enter 1 for the first, 2 for the second and 3 for both)\n";
getline(cin, change);
if (change == "1")
{
cout << "\nPlease Enter the new name for the player one.\n";
getline(cin, name1);
}
else if (change == "2")
{
cout << "\nPlease Enter the new name for the player two.\n";
getline(cin, name2);
}
else if (change == "3")
{
cout << "\nPlease Enter the new name for the player one.\n";
getline(cin, name1);
cout << "\nPlease Enter the new name for the player two.\n";
getline(cin, name2);
}
else
{
cout << "\nPlease Enter a Valid Choice.\n";
}
} while (change != "1" && change != "2" && change != "3");
}
else if (choice == "3")
{
cout << "\n" << game.getResults(name1, name2);
}
else if (choice != "4")
{
cout << "\nPlease Enter a Correct number\n";
}
} while (choice!= "4");
cout << "\nFinal Results are: \n\n"
<< game.getResults(name1, name2);
cout << "\nThank you for using our program. Hope to see You Again. Bye Bye!!\n";
system("Pause");
return 0;
}
my class
#include<iostream>
#include<string>
using namespace std;
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe
{
private:
const static int SIZE = 3;
int table[SIZE][SIZE];
int results[SIZE];
int winner = 2;
public:
TicTacToe();
void setMove(int , int );
int chkWin();
string getResults(string , string );
};
#endif
here is my methods declaration:
#include<iostream>
#include<string>
#include "TicTacToe.h"
using namespace std;
TicTacToe::TicTacToe()
{
for (int i = 0; i < SIZE; i++)
{
results[i] = 0;
for (int j = 0; j < SIZE; j++)
{
table[i][j] = 0;
}
}
}
void TicTacToe::setMove(int place, int player)
{
int field = place;
if (field == 1)
{
if (table[0][0] == 0)
{
table[0][0] = player;
}
else
{
cout << "\nYou cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 2)
{
if (table[0][1] == 0)
{
table[0][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 3)
{
if (table[0][2] == 0)
{
table[0][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 4)
{
if (table[1][0] == 0)
{
table[1][0] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 5)
{
if (table[1][1] == 0)
{
table[1][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 6)
{
if (table[1][2] == 0)
{
table[1][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 7)
{
if (table[2][0] == 0)
{
table[2][0] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 8)
{
if (table[2][1] == 0)
{
table[2][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 9)
{
if (table[2][2] == 0)
{
table[2][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
}
int TicTacToe::chkWin()
{
for (int i = 0; i < SIZE; i++)
{
if (table[i][0] == table[i][1] && table[i][0] == table[i][2])
{
if (table[i][0] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[i][0] == 2)
{
results[2] += 1;
winner = -1;
}
}
else if (table[0][i] == table[1][i] && table[0][i] == table[2][i])
{
if (table[0][i] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[0][i] == 2)
{
results[2] += 1;
winner = -1;
}
}
}
if ((table[0][0] == table[1][1] && table[0][0] == table[2][2]) || (table[0][2] == table[1][1] && table[0][2] == table[2][0]))
{
if (table[1][1] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[1][1] == 2)
{
results[2] += 1;
winner = -1;
}
}
else
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if(table[i][j] == 0)
{
winner = 2;
}
}
}
results[1] += 1;;
return winner;
}
}
string TicTacToe::getResults(string name1, string name2)
{
return ( name1 + " : " + to_string(results[0]) + "\n"
+ "Draws : " + to_string(results[1]) + "\n"
+ name2 + " : " + to_string(results[2]) + "\n");
}
Compareing functions with integers doesn't make sense. You have to use () operator to call functions like this:
if (game.chkWin() == 1)
instead of
if (game.chkWin == 1)
Poor:
game.setMove(choice_num, 1);
if (game.chkWin == 1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout <<"The Game is a Draw! ";
}
Better:
game.setMove(choice_num, 1);
switch (game.chkWin()) {
case 1:
cout << name1 << " has won this Game!";
break;
case -1:
cout << name1 << " has won this Game!";
break;
case 0:
cout <<"The Game is a Draw! ";
break;
}
The basic problem is using "==" with function game.chkWin, instead of with the result of function game.chkWin().
But in cases like this (pun intended) a "switch()" block is probably more readable and better style than multiple "if/else" statements.
'Hope that helps...
This is the function:
void LoseRollDice(int Result1, int Result2, int i)
{
if (i == 1 && (Result1 == 2 || Result1 == 3 || Result1 == 12))
{
cout << "You Lose." << endl;
return;
}
else if (i == 1 && (Result2 == 2 || Result2 == 3 || Result2 == 12))
{
cout << "Computer Loses." << endl;
return;
}
if (Result1 == 7 && i > 1)
{
cout << "You Lose." << endl;
return;
}
if (Result2 == 7 && i > 1)
{
cout << "Computer Loses." << endl;
return;
}
}
And here i used it:
for (int i = 1; i <= 5; i++)
{
cout << "Enter 1 to play, or 2 to exit: ";
cin >> chose;
cout << endl;
if (chose == 1)
{
ResultPlayer = RollDice();
ResultComputer = RollDice();
DisplayDice(ResultPlayer, ResultComputer, i);
cout << endl;
LoseRollDice(ResultPlayer, ResultComputer, i);
// if the player lose, how to break after this function if the condition is true in it??
WinRollDice(ResultPlayer, ResultComputer, i, SumPlayer, SumComputer);
}
else break;
}
How to break out of for loop if the condition is true in the function?
If i put break after the function in the loop it will break out of the loop even if the condition is not met
Change LoseRollDice to:
bool LoseRollDice(int Result1, int Result2, int i)
{
if (i == 1 && (Result1 == 2 || Result1 == 3 || Result1 == 12))
{
cout << "You Lose." << endl;
return true;
}
else if (i == 1 && (Result2 == 2 || Result2 == 3 || Result2 == 12))
{
cout << "Computer Loses." << endl;
return true;
}
if (Result1 == 7 && i > 1)
{
cout << "You Lose." << endl;
return true;
}
if (Result2 == 7 && i > 1)
{
cout << "Computer Loses." << endl;
return true;
}
return false;
}
Then your other code to:
for (int i = 1; i <= 5; i++)
{
cout << "Enter 1 to play, or 2 to exit: ";
cin >> chose;
cout << endl;
if (chose == 1)
{
ResultPlayer = RollDice();
ResultComputer = RollDice();
DisplayDice(ResultPlayer, ResultComputer, i);
cout << endl;
if (LoseRollDice(ResultPlayer, ResultComputer, i))
break;
WinRollDice(ResultPlayer, ResultComputer, i, SumPlayer, SumComputer);
}
else break;
}
Douglas Dawson's answer is correct. If you REALLY REALLY REALLY wanted that method to be void, you could also have the method throw an Exception, then catch the exception outside of the loop. Thats an absolutely terrible way of implementing it though.
Or you could catch it in the loop and call break;
Or use pointer... Send the same as parameter in function.. Change the pointer value inside if.. After that if you call function check point.. If that's changed means if had been called.. You can break your loop.