Problem with "if" in the code
Hello Guys, I am new to programming so I don't know what is wrong in
if / else if statements. No matter what it displays else statement.
Help will be appreciated, thanks :)
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
{
int c1,c2;
cout<<"Player1, Enter your Name"<<endl;
string player1;
cin>>player1;
cout<<"Player2, Enter your Name"<<endl;
string player2;
cin>>player2;
cout<<"So rock=1, scissors=2 and paper=3"<<endl<<endl;
repeatme:
cout<< player1 <<" Enter your choice"<<endl;
c1 = _getch();
cout<< player2 <<" Enter your choice"<<endl;
c2 = _getch();
cout<<endl;
if (c1 == 1 && c2 == 3 || c1 == 2 && c2 == 1 || c1 == 3 && c2 == 2)
{
cout<< player1 << " wins"<<endl<<endl;
}
else if (c2 == 1 && c1 == 3 || c2 == 2 && c1 == 1 || c2 == 3 && c1 == 2)
{
cout<< player2 << " wins"<<endl<<endl;
}
else
{
cout<<"Its a Draw"<<endl<<endl<<endl;
}
cout<<"________________________________________________";
cout<<"ITS A LOOP, LETS PLAY FOREVER"<<endl<<endl<<endl;
goto repeatme;
}
return 0;
}
|
When you don't know what's wrong with your code you have to cout the values to troubleshoot.
add this above your if statement
cout << "P1 = " << c1 << " P2 = " << c2 << endl;
Then take a look at
http://www.asciitable.com/
Last edited on
A few problems here.
The
if
statements need additional parentheses:
|
if ((c1 == 1 && c2 == 3) || (c1 == 2 && c2 == 1) || (c1 == 3 && c2 == 2))
|
Also, the values c1 and c2 contain ASCII codes 1, 2, 3 would give ASCII 49, 50, 51.
Either change the code to test for those characters:
|
if ((c1 == '1' && c2 == '3')
|
note the single quotes.
Or possibly, convert the ASCII to an integer in the range 1 to 3.
|
c1 = _getch() - '0'; // convert ASCII to int
|
You might also replace the goto with a while loop. gotos have their uses, but mostly should be avoided.
I didn't check the logic here, I'll leave that for now:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
string things[4] = { "", "Rock", "Scissors", "Paper"};
int c1,c2;
cout << "Player1, Enter your Name" << endl;
string player1;
cin >> player1;
cout << "Player2, Enter your Name" << endl;
string player2;
cin >> player2;
cout << "So rock=1, scissors=2 and paper=3" << endl << endl;
while (true)
{
cout << player1 <<" Enter your choice ";
do {
c1 = _getch() - '0'; // convert ASCII to int
} while (c1 < 1 || c1 > 3);
cout << things[c1] << '\n';
cout << player2 <<" Enter your choice ";
do {
c2 = _getch() - '0'; // convert ASCII to int
} while (c2 < 1 || c2 > 3);
cout << things[c2] << '\n';
cout << endl;
if ((c1 == 1 && c2 == 3) || (c1 == 2 && c2 == 1) || (c1 == 3 && c2 == 2))
{
cout<< player1 << " wins"<<endl<<endl;
}
else if ((c2 == 1 && c1 == 3) || (c2 == 2 && c1 == 1) || (c2 == 3 && c1 == 2))
{
cout<< player2 << " wins"<<endl<<endl;
}
else
{
cout<<"Its a Draw"<<endl<<endl<<endl;
}
cout << "________________________________________________\n";
cout << "ITS A LOOP, LETS PLAY FOREVER\n\n" << endl;
}
}
|
Last edited on
Thanks @Chervil. I am new so I didn't knew about that. Now I figured I must use commas('value') to make it work.
and thanks for the while tip and that code, I'll try to understand it. =)
Topic archived. No new replies allowed.