Thirteen Stones

A

Alf P. Steinbach

* mlimber:
Mike said:
#include <iostream> /* declares 'cin' and 'cout' */
#include <ostream> /* declares 'endl' */
[snip]

There's no need to include ostream if iostream is already included.

Formally there is, or might be, depending on one's faith in intentions instead
of the Literal Word (TM) of the standard.

<iostream> only forward-declares the eight or so standard iostream objects.

Practically I doubt there is any compiler where <iostream> isn't enough,
because it would break so much code to require <ostream> in addition, but just
to help people understand that compilesWithMyCompiler != isStandard, I usually
add <ostream> in my examples.


Cheers,

- Alf
 
K

Karl Heinz Buchegger

coinjo said:
#include<iostream.h>
void main()
{
int selection=0;
int stones=13;
int player=0;

while(stones>0)

{

cout<<"Enter Player"<<endl;

cin>>player;

{

cout<<"Enter Your Selection"<<endl;

cin>>selection;

if(stones>=selection)

{

stones=stones-selection;

}

else

cout<<"Invalid Selestion"<<endl;

}

if(stones==0)

{

if(player==1)

{

cout<<"Player 1 Wins!"<<endl;

}

else

cout<<"Player 2 Wins!"<<endl;

}

}
}

Thanks to all of you for your generous help. I was finally able to make
the program work with the above syntax. Please Check it and tell me if
you find any errors in it.

Well. First of all, as already noted, when the program is run
your users have no idea of how many stones are left on the pile.
Actually, the users *never* see (unless they study your code), how
many piles are there on the pile (eg. in europe this game is played
with 21 stones :)

But then, try the following:
start out with 13 stones. Let the porgram ask user 1 for his choice.
And now user 1 wants 13 stones. user 1 wins. :)

Start with fixing that. (A user cannot draw more then 3 or less then 1 stone,
it is a simple if)

All other things already mentioned, like inputting 'abc' can wait
until you get this right.

Also there seems to be something wrong with your overall game logic.
How come the program asks *me* whose turn it is. Shouldn't the program
know this? Otherwise I always enter 0 when the program asks me for the
player, and player 1 thus always wins.

If you need a variable to toggle between 0 and 1 you can use:

player = 1 - player;

each time that assignment is executed, it changes from 0 to 1 or vice versa
depending on the previous state.

I know, programmers are the worst testing persons. Nevertheless all of the
above mentioned things are simple ones. You should have, no, you *must* have
found them all by yourself.
 
D

Default User

Alf said:
* mlimber:
Mike said:
#include <iostream> /* declares 'cin' and 'cout' */
#include <ostream> /* declares 'endl' */
[snip]

There's no need to include ostream if iostream is already included.

Formally there is, or might be, depending on one's faith in
intentions instead of the Literal Word (TM) of the standard.


There's supposed to be an outstanding issue before the committee on
this subject (or there was a couple years ago). I'm sure what the
current status is.



Brian
 
M

Mike Wahler

mlimber said:
Mike said:
#include <iostream> /* declares 'cin' and 'cout' */
#include <ostream> /* declares 'endl' */
[snip]

There's no need to include ostream if iostream is already included.

Not true. 'std::endl' is only guaranteed to be declared
by <ostream>. It might indeed be indirectly declared with
only <iostream> (via headers including headers), but you have
no guarantee of that from the standard language.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top