coinjo said:
#include<iostream.h>
void main()
{
char selection;
int stones=13;
int done1=0;
while(stones>0 && done1!=1)
{
if(stones>0)
{
selection=0;
cout<<"Player 1 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
stones=stones-1;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='2')
{
if(stones>1)
stones=stones-2;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
stones=stones-3;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(stones==0)
{
cout<<"Player 1 Wins"<<endl;
done1=1;
}
}
if(stones>0 && done1!=1)
{cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
{
stones=stones-1;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='2')
{
if(stones>10)
{
stones=stones-2;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
{
stones=stones-3;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(stones==0)
{
cout<<"Player 2 wins"<<endl;
done1=1;
}
}
}
}
This is the best i can come up with. But there are problems in its
output. When i constantly enter 1. It should stop at player 1 wins but
it stops at player 2 wins. Now that i have tried the code. Can anyone
help me?
Here's a more readable version of the program:
#include<iostream> // N.B., iostream.h is deprecated
using namespace std;
int main() // N.B., not void main()
{
char selection;
int stones=13;
int done1=0; // Could be a bool instead of an int
while(stones>0 && done1!=1)
{
if(stones>0) // Redundant. The while condition ensures that
// this is true.
{
selection=0;
cout<<"Player 1 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0) // Redundant
stones=stones-1; // stones -= 1; is more
compact
else
{ // This else condition will never be reached
// because stones will always be > 0. If it were
reached,
// however, it would ask the user for input but not
test
// to see if the input is 1 again. The same thing
applies
// to the other else's below, except they can be
reached!
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='2') // Should be: else if( selection=='2' )
{
if(stones>1)
stones=stones-2;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3') // Use else if here, too
{
if(stones>2)
stones=stones-3;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(stones==0)
{
cout<<"Player 1 Wins"<<endl;
done1=1;
// continue; // Would skip the next part
}
}
if(stones>0 && done1!=1)
{
cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0) // Redundant
{
stones=stones-1;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='2')
{
if(stones>10)
{
stones=stones-2;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
{
stones=stones-3;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(stones==0)
{
cout<<"Player 2 wins"<<endl;
done1=1;
}
}
}
return 0;
}
On a cursory reading, I'd guess that the faulty error checking is your
problem. If you enter the thirteen 1's without error, it might work
(though I didn't test it). What you need to do is either learn to use
the debugger that is undoubtedly included with your development
environment, or more simply put some extra cout's in the code so you
can tell what the program is doing at each point.
Cheers! --M