|
From: Danny A. <tam...@ms...> - 2002-03-25 00:51:31
|
HEY GUYS,
I'm that newbie guy that keeps having trouble. I got a book "C++ for the=
absolute beginner," well this beginner doesn't still get it. So I'm won=
dering if you can explain what the book is failing to explain. Below is =
a program I typed in, that is giving me fits over "enum" and all its "ret=
urns" I feel like I missing something in my lib.'s but I'm not sure. I h=
ave double checked everything several times to the book and from all that=
I can see I have typed it out just as the book has it. I take it though=
one complier will do things differently than another and I know the auth=
or is using a different complier. So if anyone can help me I'm all ears.=
=20
THANKS DANNY!!
//6.5 - Tic Tac Toe
#include <iostream>
#include <string>
using namespace std;
enum SquareState { blank =3D ' ' , X =3D 'X', 0=3D '0' };
class gameBoard
{
private:
const int WIDTH;
const int HEIGHT;
int* GameBoard;
public:
gameBoard() : WIDTH(3), HEIGHT(3)
{
GameBoard =3D new int[9];
for (int i =3D 0; i < 9; i++)
*(GameBoard + i) =3D blank;
}
~gameBoard() {delete[] GameBoard;}
void setX(int h, int w);
void set0(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void gameBoard::setX(int h, int w)
{
*(GameBoard + h*HEIGHT + w) =3D X;
}
void gameBoard::set0(int h, int w)
{
*(GameBoard + h*HEIGHT + w) =3D 0;
}
bool gameBoard::isTaken (int h, int w)
{
return *(GameBoard + h*HEIGHT + w) !=3D ' ';
}
SquareState gameBoard::isLine()
{
if(*GameBoard=3D=3DX && *(GameBoard +1)=3D=3DX && *(GameBoard +2)=3D=
=3DX)
return X;
if(*GameBoard=3D=3D0 && *(GameBoard +1)=3D=3D0 && *(GameBoard +2)=3D=
=3D0)
return 0;
if(*(GameBoard +3)=3D=3DX && *(GameBoard +4)=3D=3DX && *(GameBoard +=
5)=3D=3DX)
return X;
if(*(GameBoard +3)=3D=3D0 && *(GameBoard +4)=3D=3D0 && *(GameBoard +=
5)=3D=3D0)
return 0;
if(*(GameBoard +6)=3D=3DX && *(GameBoard +7)=3D=3DX && *(GameBoard +=
8)=3D=3DX)
return X;
if(*(GameBoard +6)=3D=3D0 && *(GameBoard +7)=3D=3D0 && *(GameBoard +=
8)=3D=3D0)
return 0;
=20
if(*GameBoard=3D=3DX && *(GameBoard +3)=3D=3DX && *(GameBoard +6)=3D=
=3DX)
return X;
if(*GameBoard=3D=3D0 && *(GameBoard +3)=3D=3D0 && *(GameBoard +6)=3D=
=3D0)
return 0;
if(*(GameBoard +1)=3D=3DX && *(GameBoard +4)=3D=3DX && *(GameBoard +=
7)=3D=3DX)
return X;
if(*(GameBoard +1)=3D=3D0 && *(GameBoard +4)=3D=3D0 && *(GameBoard +=
7)=3D=3D0)
return 0;
if(*(GameBoard +2)=3D=3DX && *(GameBoard +5)=3D=3DX && *(GameBoard +=
8)=3D=3DX)
return X;
if(*(GameBoard +2)=3D=3D0 && *(GameBoard +5)=3D=3D0 && *(GameBoard +=
8)=3D=3D0)
return 0;
=20
if(*GameBoard=3D=3DX && *(GameBoard +4)=3D=3DX && *(GameBoard +8)=3D=
=3DX)
return X;
if(*GameBoard=3D=3D0 && *(GameBoard +4)=3D=3D0 && *(GameBoard +8)=3D=
=3D0)
return 0;
if(*(GameBoard +2)=3D=3DX && *(GameBoard +4)=3D=3DX && *(GameBoard +=
6)=3D=3DX)
return X;
if(*(GameBoard +2)=3D=3D0 && *(GameBoard +4)=3D=3D0 && *(GameBoard +=
6)=3D=3D0)
return 0;
return blank;
=20
}
void gameBoard::draw()
{
cout << endl;
for(int i=3D0; i < HEIGHT; i++)
{
cout << (char)*(GameBoard + i*HEIGHT);
for(int c=3D1; c < WIDTH; c++)
cout << " | " << (char)*(GameBoard + i*WIDTH + c);
cout << endl << "-------" << endl;
}
}
class Game
{
public:
gameBoard* doInput(string player, gameBoard* gb);
bool inRange(int test);
};
gameBoard* Game::doInput(string player, gameBoard* gb)
{
gb->draw();
=20
string letter;
if (player.compare("one") =3D=3D 0)
letter =3D "X";
else if (player.compare("two") =3D=3D 0)
letter =3D "0";
else return gb;
=20
int input1, input2;
=20
do {
=20
do {
cout << "\nPlayer " << player.c_str()
<< ", please enter a row number to put an "
<< letter.c_str() << ": ";
cin >> input1;
}while(!inRange(input1));
=20
do {
cout << "\nPlease enter a column number to put an "
<< letter.c_str() << ": ";
cin >> input2;
}while(!inRange(input2));
=20
}while (gb->isTaken(input1,input2));
=20
if (player.compare("one") =3D=3D 0)
gb->setX(input1, input2);
else gb->set0(input1, input2);
=20
return gb;
=20
}
bool Game::inRange(int test)
{
return test > -1 && test < 3;
)
int main( void )
{
gameBoard* gb =3D new gameBoard;
Game g;
string player1, player2;
cout << "Welcome to Tic Tac Toe!"
<< "\nPlayer one, please enter your name: ";
cin >> player1;
cout << "\nPlayer two, please enter your name: ";
cin >> player2;
=20
while (gb->isLine() =3D=3D ' ')
{
gb =3D g.doInput("one",gb);
gb =3D g.doInput("two",gb);
}
gb->draw();
if(gb->isLine() =3D=3D X)
cout << "\nPlayer one, you win!"
<< "\nGame Over.";
else cout << "\nPlayer two, you win!"
<< "\nGame Over.";
return 0;
}
|
|
From: <Ib...@ao...> - 2002-03-28 08:26:33
|
/* file:omosefe.cpp
description:program to input 5 floating point numbers
programmer:Omosefe
Date:27th March 2002
*/
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
float number
{
float number 1, number 2, number 3, number 4, number 5;
cout << "Enter 5 floating point numbers" << endl;
cin >> number 1;
cin >> number 2;
cin >> number 3;
cin >> number 4;
cin >> number 5;
cout << "The floating point numbers are" << endl;
cout << " number 1 is " << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint)
cout << " number 2 is " << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint)
cout << " number 3 is " << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint)
cout << " number 4 is " << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint)
cout << " number 5 is " << setprecision(2)
<< setiosflags(ios::fixed | ios::showpoint)
return 0;
}
hi,
experts please,i really need ur help on this one,the complier results is that
i have syntax errors and i have tried everything ,please help.
omosefe.
|
|
From: <gi...@li...> - 2002-03-28 17:59:41
|
At 03:26 28/03/02 -0500, Ib...@ao... wrote:
> ...
>hi,
>experts please,i really need ur help on this one,the complier results is that
>i have syntax errors and i have tried everything ,please help.
>omosefe.
the following code works, check it against the lines you posted. If you
have questions, let us know.
#include <iomanip>
// all ANSI C++ programs require a "main" function.
int
main()
{
float number1; // first number
float number2; // second number
std::cout << "Enter 2 floating point numbers" << std::endl;
std::cin >> number1 >> number2; // one instruction reads both
// remember that some settings are active until you change them,
// the precision and the way real numbers are displayed are an
// example.
std::cout << "The floating point numbers are:\n"
<< std::setprecision(2)
<< std::setiosflags(std::ios::fixed | std::ios::showpoint)
<< "number1 is: " << number1 << '\n'
<< "number2 is: " << number2 << '\n';
}
--
gisan
gi...@li...
|
|
From: <tam...@ms...> - 2002-09-07 16:47:44
|
Hey guys
I'm still very green at this and I just inputted a small program (listed =
below) from "Teach Yourself C++", and when I try to compile it gives me =
errors
LINE 26 PARSE ERROR BEFORE ';'
LINE 30 PARSE ERROR BEFORE ';'
I took the int i and the ++ off both lines and the program compiled but d=
idn't work properly. I was wondering is the book wrong or am I doing som=
ething wrong?
#include <iostream>
char msg[] =3D "Now is the time\n";
int main()
{
char* cp; //---a pointer to char
int i; //---an integer subscript
=20
//---Pointer access, pointer notation
for (cp =3D msg; *cp; cp++)
std::cout << *cp; =20
=20
//---Subscript access, subscript notation =20
for (i =3D 0; msg[i]; i++)
std::cout << msg[i]; =20
=20
//---Pointer access, subscript notation =20
for (cp =3D msg; cp[0]; cp++)
std::cout << cp[0];
=20
//---Subscript access, pointer notation
for (i =3D 0; *(msg+i); i++)
std::cout << *(msg+i);
=20
//---Pointer and subscript access, pointer notation
for (i =3D 0; cp =3D msg; *(cp+i); i++)
std::cout << *(cp+i);
=20
//---Pointer and subscript access, subscript notation
for (i =3D 0; cp =3D msg; cp[i]; i++)
std::cout << cp[i];
=20
//---system pause
char end;
std::cin >> end;
return 0;
}
|
|
From: Carlos d. M. <cg...@wo...> - 2002-09-07 19:41:03
|
You mispelled the statement, the firt semicolon is a comma (,) so you
have a first field with two assigments, the second with a condition and
the third with the operation.
tam...@ms... escribi=F3:
> Hey guys I'm still very green at this and I just inputted a small
> program (listed below) from "Teach Yourself C++", and when I try to
> compile it gives me errors LINE 26 PARSE ERROR
> BEFORE ';' LINE 30 PARSE ERROR BEFORE ';'I took
> the int i and the ++ off both lines and the program compiled but
> didn't work properly. I was wondering is the book wrong or am I doing
> something wrong? #include <iostream>
> char msg[] =3D "Now is the time\n";int main()
> {
> char* cp; //---a pointer to char
> int i; //---an integer subscript
>
> //---Pointer access, pointer notation
> for (cp =3D msg; *cp; cp++)
> std::cout << *cp;
>
> //---Subscript access, subscript notation
> for (i =3D 0; msg[i]; i++)
> std::cout << msg[i];
>
> //---Pointer access, subscript notation
> for (cp =3D msg; cp[0]; cp++)
> std::cout << cp[0];
>
> //---Subscript access, pointer notation
> for (i =3D 0; *(msg+i); i++)
> std::cout << *(msg+i);
>
> //---Pointer and subscript access, pointer notation
> for (i =3D 0; cp =3D msg; *(cp+i); i++)
> std::cout << *(cp+i);
>
> //---Pointer and subscript access, subscript notation
> for (i =3D 0; cp =3D msg; cp[i]; i++)
> std::cout << cp[i];
>
> //---system pause
> char end;
> std::cin >> end;
> return 0;
> }
>
|
|
From: Danny A. <tam...@ms...> - 2002-11-05 00:19:24
|
Hey guys;
this newbie is back at it again trying to figure out what I'm doing below=
is a very long piece of code in 5 different files that I'm trying to com=
pile in one project but for some reason even though I have typed it out o=
f a book and I have played with it and I have even gone to the authors so=
urce code and copied and paste from them and it still doesn't want to wor=
k. I'm beginning to believe it could be a difference in compilers the au=
thors used "CodeWarrior" and the book I'm using is "C++ Programming for t=
he absolute beginner". if anyone can be of help I would appreciate it bec=
ause it would truly teach me something I can't figure out.
Thanks in advance
Danny
//8.4 - Dragon Lord - Mark Lee - Prima Publishing
// Dragon.cpp
#include <string>
#include <ctime>
#include <cstdlib>
#define MAX(a,b) a>b? a:b
using namespace std;
class Dragon
{
private:
int speed;
string name;
int hitPoints;
int armour;
int treasure;
int clawDamage;
int size;
protected:
Dragon(int theSize);
int getArmour() {return armour;}
int& getHitPoints() {return hitPoints;}
int getClawDamage() { return clawDamage;}
int getSize() { return size;}
virtual int attack(int targetArmour, int specialDamage);
public:
virtual int attack(int targetArmour) =3D 0;
virtual void defend(int damage) =3D 0;
int getTreasure() {return treasure;}
virtual string getName() {return name;}
int getSpeed() {return speed;}
bool isAlive() { return hitPoints >0;}
};
Dragon::Dragon(int theSize) : =20
size(theSize)
{
if (size < 1 || size > 4)
size =3D 3;
clawDamage =3D 2 * size;
speed =3D 3 * size;
hitPoints =3D 4 * size;
armour =3D size;
treasure =3D 1000 * size;
srand(time(0));
}
int Dragon::attack(int targetArmour, int specialDamage)
{
int useSpecial =3D rand() % 2; // 0 or 1
int damage;
if (useSpecial) =20
damage =3D specialDamage;
else damage =3D getClawDamage();
return MAX(damage - targetArmour,0);
}
//BlackDragon.cpp
#include "Dragon.cpp"
class BlackDragon : public Dragon
{
private:
int poisonDamage;
public:
BlackDragon(int theSize);
int attack(int targetArmour);
void defend(int damage);
string getName() {return "Black Dragon";}
};
BlackDragon::BlackDragon(int theSize) :
Dragon(theSize)
{
poisonDamage =3D getSize();
}
int BlackDragon::attack(int targetArmour)
{
return Dragon::attack(targetArmour, poisonDamage);
}
void BlackDragon::defend(int damage)
{
getHitPoints() -=3D damage - getArmour();
}
//BlueDragon.cpp
#include "Dragon.cpp"
class BlueDragon : public Dragon
{
private:
int iceDamage;
public:
BlueDragon(int theSize);
int attack(int targetArmour);
void defend(int damage);
string getName() {return "Blue Dragon";}
};
BlueDragon::BlueDragon(int theSize) :
Dragon(theSize)
{
iceDamage =3D 3 * getSize();
}
int BlueDragon::attack(int targetArmour)
{
return Dragon::attack(targetArmour, iceDamage);
}
void BlueDragon::defend(int damage)
{
getHitPoints() -=3D (damage - getArmour())/2;
}
//RedDragon.cpp
#include "Dragon.cpp"
class RedDragon : public Dragon
{
private:
int fireDamage;
public:
RedDragon(int theSize);
int attack(int targetArmour);
void defend(int damage);
string getName() {return "Red Dragon";}
};
RedDragon::RedDragon(int theSize) :
Dragon(theSize)
{
fireDamage =3D 4 * getSize();
}
int RedDragon::attack(int targetArmour)
{
return Dragon::attack(targetArmour, fireDamage);
}
void RedDragon::defend(int damage)
{
getHitPoints() -=3D (damage - getArmour())/3;
}
//DragonLord.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Dragon.cpp"
#include "RedDragon.cpp"
#include "BlueDragon.cpp"
#include "BlackDragon.cpp"
using namespace std;
int menuChoice();
int main (void)
{
srand(time(0));
Dragon* dragons[3];
int hp =3D 15;
int armour =3D 2;
int tempArmour;
int tempAttack;
dragons[0] =3D new RedDragon(rand()%4+1);
dragons[1] =3D new BlackDragon(rand()%4+1);
dragons[2] =3D new BlueDragon(rand()%4+1);
Dragon* d =3D dragons[rand()%3];
cout << "Welcome noble knight.\n"
<< "You must save a princess."
<< " She has been captured by a "
<< d->getName() << ".\n"
<< "You must defeat the dragon.\n";
cout << "Your hit points are: " << hp << endl;
while (d->isAlive() && hp>0)
{
int choice =3D menuChoice();
if (choice =3D=3D 3) goto RUN;
else if (choice =3D=3D 1)
{
tempAttack =3D rand()%16+5;
tempArmour =3D armour;
}
else {
tempAttack =3D rand()%11;
tempArmour =3D armour + 4;
}
hp -=3D d->attack(armour);
d->defend(rand()%16-5);
cout << "\nYou deliver a mighty blow and deal "
<< tempAttack << " damage.\n";
cout << "Your hit points are: " << hp;
}
if (d->isAlive())
cout << "\nYou have perished before"
<< " the might of the dragon.\n";
else
cout << "\n\nYou have slain the dragon!"
<< " Congratulations.\n"
<< "The princess is saved.\n";
return 0;
RUN: =20
cout <<"\nYou have fled in cowardice.\n";
return 0;
}
int menuChoice()
{
int choice;
do {
cout << endl
<< "[1]Attack\n"
<< "[2]Defensive Mode\n"
<< "[3]Run Away\n";
cin >> choice;
} while (choice < 1 && choice > 3);
return choice;
} =20
|
|
From: Daniel K. O. <dan...@ya...> - 2002-03-25 01:41:28
|
Try putting your includes like this:
#include <iostream.h>
#include <string.h>
And I noticed the identifiers you're using in the enum type:
enum SquareState { blank = ' ' , X = 'X', 0= '0' };
Look: you're using 0 (zero) instead O (letter). Identifiers shouldn't start
with a digit, like 0 (zero).
The same where you're returning zero instead O.
Hope this can help you.
=============================
Daniel K. O.
-----------------------------
Computer Science - UNISC
http://www.unisc.br
=============================
ICQ# 117887205
http://danielko.cjb.net
dan...@ie...
dan...@ya...
-----Mensagem original-----
De: dev...@li...
[mailto:dev...@li...]Em nome de Danny Austin
Enviada em: domingo, 24 de março de 2002 21:56
Para: dev...@li...
Assunto: [Dev-C++] errors
HEY GUYS,
I'm that newbie guy that keeps having trouble. I got a book "C++ for the
absolute beginner," well this beginner doesn't still get it. So I'm
wondering if you can explain what the book is failing to explain. Below is
a program I typed in, that is giving me fits over "enum" and all its
"returns" I feel like I missing something in my lib.'s but I'm not sure. I
have double checked everything several times to the book and from all that I
can see I have typed it out just as the book has it. I take it though one
complier will do things differently than another and I know the author is
using a different complier. So if anyone can help me I'm all ears.
THANKS DANNY!!
//6.5 - Tic Tac Toe
#include <iostream>
#include <string>
using namespace std;
enum SquareState { blank = ' ' , X = 'X', 0= '0' };
class gameBoard
{
private:
const int WIDTH;
const int HEIGHT;
int* GameBoard;
public:
gameBoard() : WIDTH(3), HEIGHT(3)
{
GameBoard = new int[9];
for (int i = 0; i < 9; i++)
*(GameBoard + i) = blank;
}
~gameBoard() {delete[] GameBoard;}
void setX(int h, int w);
void set0(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void gameBoard::setX(int h, int w)
{
*(GameBoard + h*HEIGHT + w) = X;
}
void gameBoard::set0(int h, int w)
{
*(GameBoard + h*HEIGHT + w) = 0;
}
bool gameBoard::isTaken (int h, int w)
{
return *(GameBoard + h*HEIGHT + w) != ' ';
}
SquareState gameBoard::isLine()
{
if(*GameBoard==X && *(GameBoard +1)==X && *(GameBoard +2)==X)
return X;
if(*GameBoard==0 && *(GameBoard +1)==0 && *(GameBoard +2)==0)
return 0;
if(*(GameBoard +3)==X && *(GameBoard +4)==X && *(GameBoard +5)==X)
return X;
if(*(GameBoard +3)==0 && *(GameBoard +4)==0 && *(GameBoard +5)==0)
return 0;
if(*(GameBoard +6)==X && *(GameBoard +7)==X && *(GameBoard +8)==X)
return X;
if(*(GameBoard +6)==0 && *(GameBoard +7)==0 && *(GameBoard +8)==0)
return 0;
if(*GameBoard==X && *(GameBoard +3)==X && *(GameBoard +6)==X)
return X;
if(*GameBoard==0 && *(GameBoard +3)==0 && *(GameBoard +6)==0)
return 0;
if(*(GameBoard +1)==X && *(GameBoard +4)==X && *(GameBoard +7)==X)
return X;
if(*(GameBoard +1)==0 && *(GameBoard +4)==0 && *(GameBoard +7)==0)
return 0;
if(*(GameBoard +2)==X && *(GameBoard +5)==X && *(GameBoard +8)==X)
return X;
if(*(GameBoard +2)==0 && *(GameBoard +5)==0 && *(GameBoard +8)==0)
return 0;
if(*GameBoard==X && *(GameBoard +4)==X && *(GameBoard +8)==X)
return X;
if(*GameBoard==0 && *(GameBoard +4)==0 && *(GameBoard +8)==0)
return 0;
if(*(GameBoard +2)==X && *(GameBoard +4)==X && *(GameBoard +6)==X)
return X;
if(*(GameBoard +2)==0 && *(GameBoard +4)==0 && *(GameBoard +6)==0)
return 0;
return blank;
}
void gameBoard::draw()
{
cout << endl;
for(int i=0; i < HEIGHT; i++)
{
cout << (char)*(GameBoard + i*HEIGHT);
for(int c=1; c < WIDTH; c++)
cout << " | " << (char)*(GameBoard + i*WIDTH + c);
cout << endl << "-------" << endl;
}
}
class Game
{
public:
gameBoard* doInput(string player, gameBoard* gb);
bool inRange(int test);
};
gameBoard* Game::doInput(string player, gameBoard* gb)
{
gb->draw();
string letter;
if (player.compare("one") == 0)
letter = "X";
else if (player.compare("two") == 0)
letter = "0";
else return gb;
int input1, input2;
do {
do {
cout << "\nPlayer " << player.c_str()
<< ", please enter a row number to put an "
<< letter.c_str() << ": ";
cin >> input1;
}while(!inRange(input1));
do {
cout << "\nPlease enter a column number to put an "
<< letter.c_str() << ": ";
cin >> input2;
}while(!inRange(input2));
}while (gb->isTaken(input1,input2));
if (player.compare("one") == 0)
gb->setX(input1, input2);
else gb->set0(input1, input2);
return gb;
}
bool Game::inRange(int test)
{
return test > -1 && test < 3;
)
int main( void )
{
gameBoard* gb = new gameBoard;
Game g;
string player1, player2;
cout << "Welcome to Tic Tac Toe!"
<< "\nPlayer one, please enter your name: ";
cin >> player1;
cout << "\nPlayer two, please enter your name: ";
cin >> player2;
while (gb->isLine() == ' ')
{
gb = g.doInput("one",gb);
gb = g.doInput("two",gb);
}
gb->draw();
if(gb->isLine() == X)
cout << "\nPlayer one, you win!"
<< "\nGame Over.";
else cout << "\nPlayer two, you win!"
<< "\nGame Over.";
return 0;
}
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
|
|
From: Ioannis V. <no...@ho...> - 2002-03-26 19:31:30
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Daniel K. O. > Sent: Monday, March 25, 2002 3:41 AM > To: Dev-Cpp-Users > Subject: RES: [Dev-C++] errors > > > Try putting your includes like this: > > #include <iostream.h> > #include <string.h> This is a mistake. The proper ANSI C++ header style is #include <iostream> . iostream.h e.t.c is illegal, in all modern compilers is depricated and is about to removed from future versions. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys |
|
From: Jason H. <jas...@bt...> - 2002-03-25 03:35:31
|
The errors you are getting are all due to simple typing errors. If you =
look in the enum you have substituted the number 0 with the letter O, =
notice the subtle difference in width between letter and number. So =
where you have previously put zero you need to put the letter O, but =
only for the lines dealing with the enumeration, and returning an =
enumeration type. Another mistype you have is where a ) bracket has been =
substituted for a } brace.
If you go through the errors double clicking the top one and correct the =
error, and then recompiling eventualy you will get a full compile. =
However I suggest you look at the code as the game doesn't apear to work =
properly, so there may be other more subtle erros, maybe mistyped =
numbers etc...
Jason.
----- Original Message -----=20
From: Danny Austin=20
To: dev...@li...=20
Sent: Monday, March 25, 2002 12:55 AM
Subject: [Dev-C++] errors
HEY GUYS,
I'm that newbie guy that keeps having trouble. I got a book "C++ for =
the absolute beginner," well this beginner doesn't still get it. So I'm =
wondering if you can explain what the book is failing to explain. Below =
is a program I typed in, that is giving me fits over "enum" and all its =
"returns" I feel like I missing something in my lib.'s but I'm not sure. =
I have double checked everything several times to the book and from all =
that I can see I have typed it out just as the book has it. I take it =
though one complier will do things differently than another and I know =
the author is using a different complier. So if anyone can help me I'm =
all ears. =20
THANKS DANNY!!
//6.5 - Tic Tac Toe
#include <iostream>
#include <string>
using namespace std;
enum SquareState { blank =3D ' ' , X =3D 'X', 0=3D '0' };
class gameBoard
{
private:
const int WIDTH;
const int HEIGHT;
int* GameBoard;
public:
gameBoard() : WIDTH(3), HEIGHT(3)
{
GameBoard =3D new int[9];
for (int i =3D 0; i < 9; i++)
*(GameBoard + i) =3D blank;
}
~gameBoard() {delete[] GameBoard;}
void setX(int h, int w);
void set0(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void gameBoard::setX(int h, int w)
{
*(GameBoard + h*HEIGHT + w) =3D X;
}
void gameBoard::set0(int h, int w)
{
*(GameBoard + h*HEIGHT + w) =3D 0;
}
bool gameBoard::isTaken (int h, int w)
{
return *(GameBoard + h*HEIGHT + w) !=3D ' ';
}
SquareState gameBoard::isLine()
{
if(*GameBoard=3D=3DX && *(GameBoard +1)=3D=3DX && *(GameBoard =
+2)=3D=3DX)
return X;
if(*GameBoard=3D=3D0 && *(GameBoard +1)=3D=3D0 && *(GameBoard =
+2)=3D=3D0)
return 0;
if(*(GameBoard +3)=3D=3DX && *(GameBoard +4)=3D=3DX && =
*(GameBoard +5)=3D=3DX)
return X;
if(*(GameBoard +3)=3D=3D0 && *(GameBoard +4)=3D=3D0 && =
*(GameBoard +5)=3D=3D0)
return 0;
if(*(GameBoard +6)=3D=3DX && *(GameBoard +7)=3D=3DX && =
*(GameBoard +8)=3D=3DX)
return X;
if(*(GameBoard +6)=3D=3D0 && *(GameBoard +7)=3D=3D0 && =
*(GameBoard +8)=3D=3D0)
return 0;
=20
if(*GameBoard=3D=3DX && *(GameBoard +3)=3D=3DX && *(GameBoard =
+6)=3D=3DX)
return X;
if(*GameBoard=3D=3D0 && *(GameBoard +3)=3D=3D0 && *(GameBoard =
+6)=3D=3D0)
return 0;
if(*(GameBoard +1)=3D=3DX && *(GameBoard +4)=3D=3DX && =
*(GameBoard +7)=3D=3DX)
return X;
if(*(GameBoard +1)=3D=3D0 && *(GameBoard +4)=3D=3D0 && =
*(GameBoard +7)=3D=3D0)
return 0;
if(*(GameBoard +2)=3D=3DX && *(GameBoard +5)=3D=3DX && =
*(GameBoard +8)=3D=3DX)
return X;
if(*(GameBoard +2)=3D=3D0 && *(GameBoard +5)=3D=3D0 && =
*(GameBoard +8)=3D=3D0)
return 0;
=20
if(*GameBoard=3D=3DX && *(GameBoard +4)=3D=3DX && *(GameBoard =
+8)=3D=3DX)
return X;
if(*GameBoard=3D=3D0 && *(GameBoard +4)=3D=3D0 && *(GameBoard =
+8)=3D=3D0)
return 0;
if(*(GameBoard +2)=3D=3DX && *(GameBoard +4)=3D=3DX && =
*(GameBoard +6)=3D=3DX)
return X;
if(*(GameBoard +2)=3D=3D0 && *(GameBoard +4)=3D=3D0 && =
*(GameBoard +6)=3D=3D0)
return 0;
return blank;
=20
}
void gameBoard::draw()
{
cout << endl;
for(int i=3D0; i < HEIGHT; i++)
{
cout << (char)*(GameBoard + i*HEIGHT);
for(int c=3D1; c < WIDTH; c++)
cout << " | " << (char)*(GameBoard + i*WIDTH + =
c);
cout << endl << "-------" << endl;
}
}
class Game
{
public:
gameBoard* doInput(string player, gameBoard* gb);
bool inRange(int test);
};
gameBoard* Game::doInput(string player, gameBoard* gb)
{
gb->draw();
=20
string letter;
if (player.compare("one") =3D=3D 0)
letter =3D "X";
else if (player.compare("two") =3D=3D 0)
letter =3D "0";
else return gb;
=20
int input1, input2;
=20
do {
=20
do {
cout << "\nPlayer " << player.c_str()
<< ", please enter a row number to put an "
<< letter.c_str() << ": ";
cin >> input1;
}while(!inRange(input1));
=20
do {
cout << "\nPlease enter a column number to put an "
<< letter.c_str() << ": ";
cin >> input2;
}while(!inRange(input2));
=20
}while (gb->isTaken(input1,input2));
=20
if (player.compare("one") =3D=3D 0)
gb->setX(input1, input2);
else gb->set0(input1, input2);
=20
return gb;
=20
}
bool Game::inRange(int test)
{
return test > -1 && test < 3;
)
int main( void )
{
gameBoard* gb =3D new gameBoard;
Game g;
string player1, player2;
cout << "Welcome to Tic Tac Toe!"
<< "\nPlayer one, please enter your name: ";
cin >> player1;
cout << "\nPlayer two, please enter your name: ";
cin >> player2;
=20
while (gb->isLine() =3D=3D ' ')
{
gb =3D g.doInput("one",gb);
gb =3D g.doInput("two",gb);
}
gb->draw();
if(gb->isLine() =3D=3D X)
cout << "\nPlayer one, you win!"
<< "\nGame Over.";
else cout << "\nPlayer two, you win!"
<< "\nGame Over.";
return 0;
}
|
|
From: Ioannis V. <no...@ho...> - 2002-03-26 19:28:50
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Danny
Austin
Sent: Monday, March 25, 2002 2:56 AM
To: dev...@li...
Subject: [Dev-C++] errors
HEY GUYS,
I'm that newbie guy that keeps having trouble. I got a book "C++ for
the absolute beginner," well this beginner doesn't still get it. So I'm
wondering if you can explain what the book is failing to explain. Below
is a program I typed in, that is giving me fits over "enum" and all its
"returns" I feel like I missing something in my lib.'s but I'm not sure.
I have double checked everything several times to the book and from all
that I can see I have typed it out just as the book has it. I take it
though one complier will do things differently than another and I know
the author is using a different complier. So if anyone can help me I'm
all ears.
enums are user defined discrete integer types.
THANKS DANNY!!
//6.5 - Tic Tac Toe
#include <iostream>
#include <string>
using namespace std;
enum SquareState { blank = ' ' , X = 'X', 0= '0' };
0='0'?
enum SquareState { blank=' ', X='X', O='0'};
class gameBoard
{
private:
const int WIDTH;
const int HEIGHT;
int* GameBoard;
public:
gameBoard() : WIDTH(3), HEIGHT(3)
{
GameBoard = new int[9];
for (int i = 0; i < 9; i++)
*(GameBoard + i) = blank;
}
~gameBoard() {delete[] GameBoard;}
void setX(int h, int w);
void set0(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void gameBoard::setX(int h, int w)
{
*(GameBoard + h*HEIGHT + w) = X;
}
void gameBoard::set0(int h, int w)
{
*(GameBoard + h*HEIGHT + w) = 0;
}
bool gameBoard::isTaken (int h, int w)
{
return *(GameBoard + h*HEIGHT + w) != ' ';
}
SquareState gameBoard::isLine()
{
if(*GameBoard==X && *(GameBoard +1)==X && *(GameBoard +2)==X)
return X;
if(*GameBoard==0 && *(GameBoard +1)==0 && *(GameBoard +2)==0)
return 0;
0 doesn't belong to type SquareState. Result is truncated.
Ioannis
* Ioannis Vranos
* Programming pages: <http://www.noicys.cjb.net/>
http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|