whats wrong with my code??C++

whats wrong with my codes?? it runs but it doesn't execute the logic inside??
//handles other alphabet and numbers
its a rockpaperscissor game in a windows application form ,must accept S,F,R only and changes the label to who the winner is. Help u.u

private void Form1_KeyPress( Object^ /*sender*/, System::Windows::Forms::KeyPressEventArgs^ e )
{
if ((e->KeyChar>='0' )&&(e->KeyChar>='9' ))
{e->Handled=true;}

if((e->KeyChar==char(80))||(e->KeyChar==char(82))||(e->KeyChar==char(83)))
{}
else
{e->Handled=true;}

}
#pragma endregion
private: System::Void btnExecute_Click(System::Object^ sender, System::EventArgs^ e) {


int main()//main program
{
char P1 ;//variables for choosing P1
char P2Choice[ 2 ]={'S','P','R'};//array for player2/computer choices
char P2;

//random seed for player2/computer
srand ( time(NULL) ); //initialize the random seed
int RandIndex = rand() % 3; //generates a random number between 0 and 2
P2= P2Choice[RandIndex];


//get choice of player 1
P1 = this->txtbxPlayer1->Text;
this->txtbxPlayer2->Text=P2;

if((P1=='R' && P2=='S') || (P1=='S' && P2=='P')||(P1=='r' && P2=='S')||(P1=='s' && P2=='P'))
{this->lblWinner->Text = "Player1 Wins!"; }

if((P1=='S' && P2=='R') || (P1=='P' && P2=='S')||(P1=='s' && P2=='R')||(P1=='p' && P2=='S'))
{this->lblWinner->Text = "Player2 Wins!";}//player 2 winner

if((P1=='S' && P2=='S') || (P1=='P' && P2=='P')||(P1=='s' && P2=='S')||
(P1=='R' && P2=='R') || (P1=='s' && P2=='S')||(P1=='r' && P2=='R'))
{this->lbWinner->Text="We have a Tie!";}
}
}

}

I'm not really sure why you used all of the this-> terms. Instead, I just used cout and cin. I also added a few lines to output player two's choice. Otherwise, you would enter your choice then win or loose without knowing what the other player's choice was. The following code compiled just fine for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()//main program
{
char P1 ;//variables for choosing P1
char P2Choice[ 3 ]={'S','P','R'};//array for player2/computer choices
char P2;

//random seed for player2/computer
srand ( time(NULL) ); //initialize the random seed
int RandIndex = rand() % 3; //generates a random number between 0 and 2
P2= P2Choice[RandIndex];


//get choice of player 1
cout << "Enter S, P, or R: ";
cin >> P1;

cout << "Player two's choice: " << P2 << endl;

if((P1=='R' && P2=='S') || (P1=='S' && P2=='P')||(P1=='r' && P2=='S')||(P1=='s' && P2=='P'))
{cout << "Player1 Wins!"; }

if((P1=='S' && P2=='R') || (P1=='P' && P2=='S')||(P1=='s' && P2=='R')||(P1=='p' && P2=='S'))
{cout << "Player2 Wins!";}//player 2 winner

if((P1=='S' && P2=='S') || (P1=='P' && P2=='P')||(P1=='s' && P2=='S')||
(P1=='R' && P2=='R') || (P1=='p' && P2=='P')||(P1=='r' && P2=='R'))
{cout << "We have a Tie!";}
}


Also, where you tested the tie conditions, you had two of these (P1=='s' && P2=='S'), but no (P1=='p' && P2=='P').
Last edited on
If you use the toupper function you can avoid a lot of testing in the if statement. That is, you only need to test the uppercase values only.
your code os correct but little bit modification as THeIdeasMan told.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()//main program
{
char P1 ;//variables for choosing P1
char P2Choice[ 3 ]={'S','P','R'};//array for player2/computer choices
char P2;

//random seed for player2/computer
srand ( time(NULL) ); //initialize the random seed
int RandIndex = rand() % 3; //generates a random number between 0 and 2
P2= P2Choice[RandIndex];


//get choice of player 1
cout << "Enter S, P, or R: ";
cin >> P1;
P1 = toupper(P1);
cout << "Player two's choice: " << P2 << endl;
P2= toupper(P2);
if((P1=='R' && P2=='S') || (P1=='S' && P2=='P'))
{cout << "Player1 Wins!"; }

if((P1=='S' && P2=='R') || (P1=='P' && P2=='S'))
{cout << "Player2 Wins!";}//player 2 winner

if((P1=='S' && P2=='S') || (P1=='P' && P2=='P')||(P1=='R' && P2=='R'))
{cout << "We have a Tie!";}
}
Topic archived. No new replies allowed.