Beginners C++ Help

anishannayya

Member
Jun 10, 2008
136
0
0
Hi,

I have to create a rock-paper-scissor game. Each of two users types in either P, R, or S. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or Nobody wins. I have to allow the users to use lowercase as well as uppercase letters. The program should include a loop that lets the user play again until the user says she or he is done.

Here is my code:

#include <iostream>
using namespace std;

// Filename: Programming Project 1 Chapter 3.cpp
// Author: Anish
// Description: A program to play rock-paper-scissors.
// Date: Saturday, July 20, 2008
// Compiler: Visual C++ 2008 Express Edition

int main ( )
{
char play, one, two, y, Y, p, P, r, R, s, S;

cout << "This is a program that allows you to play rock-paper-scissors.\n"
<< "Would you like to play a game?\n"
<< "Press y for yes or n for no and press enter.\n\n";
cin >> play;
while (play == 'y' || play == 'Y')
{
cout << "Welcome to rock-paper-scissors.\n\n"
<< "Player one: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> one;
cout << "Player one chose: " << one << ".\n"
<< "Player two: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> two;
cout << "Player two chose: " << two << ".\n\n";

{
if (one == 'p' || one == 'P'; two == 'p' || two == 'P')
cout << "Nobody wins because you both chose paper.\n";
else if (one == 'p' || one == 'P'; two == 'r' || two == 'R')
cout << "Player one wins because paper covers rock.\n";
else if (one == 'p' || one == 'P'; two == 's' || two == 'S')
cout << "Player two wins because scissors cut paper.\n";
else if (one == 'r' || one == 'R'; two == 'p' || two == 'P')
cout << "Player two wins because paper covers rock.\n";
else if (one == 'r' || one == 'R'; two == 'r' || two == 'R')
cout << "Nobody wins because you both choose rock.\n";
else if (one == 'r' || one == 'R'; two == 's' || two == 'S')
cout << "Player one wins because rock breaks scissors.\n";
else if (one == 's' || one == 'S'; two == 'p' || two == 'P')
cout << "Player one wins because scissors cut paper.\n";
else if (one == 's' || one == 'S'; two == 'r' || two == 'R')
cout << "Player two wins because rock breaks scissors.\n";
else if (one == 's' || one == 'S'; two == 's' || two == 'S')
cout << "Nobody wins because you both choose scissors.\n";
else if (!(one == 'p' || one == 'P' || one == 'r' || one == 'R' || one == 's' || one 'S'))
cout << "Player one: " << player_ one << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if (!(two == 'p' || two == 'P' || two == 'r' || two == 'R' || two == 's' || two 'S'))
cout << "Player two: " << two << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
}
}

char c;
cout << "\n"
<< "Press a key and hit enter to exit.\n";
cin >> c;

return 0;
}

Thanks for the help. My compiler says I have 41 errors.
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
A few quick things:

1) Do your own homework, but feel free to ask specific questions. Seriously.
2) Stop using so many '\n' escape characters for new lines! Use endl - it flushes the buffer as well as makes a new line. Your code never flushes the buffer.
C) It would help to post your errors...
D) Which compiler are you using?
 

anishannayya

Member
Jun 10, 2008
136
0
0
I changed my code a bit. Here it is:

#include <iostream>
using namespace std;

// Filename: Programming Project 1 Chapter 3.cpp
// Author: Anish
// Description: A program to play rock-paper-scissors.
// Date: Saturday, July 20, 2008
// Compiler: Visual C++ 2008 Express Edition

int main ( )
{
char play, one, two, y, Y, p, P, r, R, s, S;

cout << "This is a program that allows you to play rock-paper-scissors.\n"
<< "Would you like to play a game?\n"
<< "Press y for yes or n for no and press enter.\n\n";
cin >> play;
while (play == 'y' || play == 'Y')
{
cout << "Welcome to rock-paper-scissors.\n\n"
<< "Player one: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> one;
cout << "Player one chose: " << one << ".\n"
<< "Player two: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> two;
cout << "Player two chose: " << two << ".\n\n";

{
if (!(one == 'p' || one == 'P' || one == 'r' || one == 'R' || one == 's' || one 'S'))
cout << "Player one: " << player_ one << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if (!(two == 'p' || two == 'P' || two == 'r' || two == 'R' || two == 's' || two 'S'))
cout << "Player two: " << two << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if (one == 'p' || one == 'P', two == 'p' || two == 'P')
cout << "Nobody wins because you both chose paper.\n";
else if (one == 'p' || one == 'P')(two == 'r' || two == 'R')
cout << "Player one wins because paper covers rock.\n";
else if (one == 'p' || one == 'P')(two == 's' || two == 'S')
cout << "Player two wins because scissors cut paper.\n";
else if (one == 'r' || one == 'R')(two == 'p' || two == 'P')
cout << "Player two wins because paper covers rock.\n";
else if (one == 'r' || one == 'R')(two == 'r' || two == 'R')
cout << "Nobody wins because you both choose rock.\n";
else if (one == 'r' || one == 'R')(two == 's' || two == 'S')
cout << "Player one wins because rock breaks scissors.\n";
else if (one == 's' || one == 'S')(two == 'p' || two == 'P')
cout << "Player one wins because scissors cut paper.\n";
else if (one == 's' || one == 'S')(two == 'r' || two == 'R')
cout << "Player two wins because rock breaks scissors.\n";
else if (one == 's' || one == 'S')(two == 's' || two == 'S')
cout << "Nobody wins because you both choose scissors.\n";
}
}

char c;
cout << "\n"
<< "Press a key and hit enter to exit.\n";
cin >> c;

return 0;
}

These are my errors: 1>------ Build started: Project: Programming Project 1 Chapter 3, Configuration: Debug Win32 ------
1>Compiling...
1>Programing Project 1 Chapter 3.cpp
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(41) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(44) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(45) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(46) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(47) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(48) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(49) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(50) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(51) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(52) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(53) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(54) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(55) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(56) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(57) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(58) : error C2146: syntax error : missing ';' before identifier 'cout'
1>Build log was saved at "file://c:\Documents and Settings\Anish\My Documents\Visual Studio 2008\Projects\Programming Project 1 Chapter 3\Programming Project 1 Chapter 3\Debug\BuildLog.htm"
1>Programming Project 1 Chapter 3 - 21 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

As the build log shows, I am using Visual Studio 2008.

BTW, not homework, just interested in learning C++, so I picked up a textbook online.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
It's been a few years since I've done C++... But this statement doesn't look right.


--- Original code
else if (one == 'p' || one == 'P', two == 'p' || two == 'P')
cout << "Nobody wins because you both chose paper.\n";

Don't you need curly brackets with the if statements?

--- Suspected change
else if (one == 'p' || one == 'P', two == 'p' || two == 'P')
{
cout << "Nobody wins because you both chose paper.\n";
}

This also doesn't look right:

---- Original code
(one == 's' || one == 'S')(two == 's' || two == 'S')

You have the or's in the if statements, which is good, but you don't have the operators between the two players.

--- Suspectec ahnge
(one == 's' || one == 'S') && (two == 's' || two == 'S')

If one == 's' or 'S' AND two == 's' or 'S'

Fix those up and post us your compiler error log... You also have a comma in an if statement, that doesn't work. You have to do it like above...
 

anishannayya

Member
Jun 10, 2008
136
0
0
Okay here is what I did:

#include <iostream>
using namespace std;

// Filename: Programming Project 1 Chapter 3.cpp
// Author: Anish
// Description: A program to play rock-paper-scissors.
// Date: Saturday, July 20, 2008
// Compiler: Visual C++ 2008 Express Edition

int main ( )
{
char play, one, two, y, Y, p, P, r, R, s, S;

cout << "This is a program that allows you to play rock-paper-scissors.\n"
<< "Would you like to play a game?\n"
<< "Press y for yes or n for no and press enter.\n\n";
cin >> play;
while (play == 'y' || play == 'Y')
{
cout << "Welcome to rock-paper-scissors.\n\n"
<< "Player one: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> one;
cout << "Player one chose: " << one << ".\n"
<< "Player two: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> two;
cout << "Player two chose: " << two << ".\n\n";

{
if (!(one == 'p' || one == 'P' || one == 'r' || one == 'R' || one == 's' || one 'S'))
cout << "Player one: " << player_ one << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if (!(two == 'p' || two == 'P' || two == 'r' || two == 'R' || two == 's' || two 'S'))
cout << "Player two: " << two << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if (one == 'p' || one == 'P') && (two == 'p' || two == 'P')
cout << "Nobody wins because you both chose paper.\n";
else if (one == 'p' || one == 'P') && (two == 'r' || two == 'R')
cout << "Player one wins because paper covers rock.\n";
else if (one == 'p' || one == 'P') && (two == 's' || two == 'S')
cout << "Player two wins because scissors cut paper.\n";
else if (one == 'r' || one == 'R') && (two == 'p' || two == 'P')
cout << "Player two wins because paper covers rock.\n";
else if (one == 'r' || one == 'R') && (two == 'r' || two == 'R')
cout << "Nobody wins because you both choose rock.\n";
else if (one == 'r' || one == 'R') && (two == 's' || two == 'S')
cout << "Player one wins because rock breaks scissors.\n";
else if (one == 's' || one == 'S') && (two == 'p' || two == 'P')
cout << "Player one wins because scissors cut paper.\n";
else if (one == 's' || one == 'S') && (two == 'r' || two == 'R')
cout << "Player two wins because rock breaks scissors.\n";
else if (one == 's' || one == 'S') && (two == 's' || two == 'S')
cout << "Nobody wins because you both choose scissors.\n";
}
}

char c;
cout << "\n"
<< "Press a key and hit enter to exit.\n";
cin >> c;

return 0;
}


New error log:

1>------ Build started: Project: Programming Project 1 Chapter 3, Configuration: Debug Win32 ------
1>Compiling...
1>Programing Project 1 Chapter 3.cpp
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(41) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(41) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(41) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(43) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(43) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(43) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(45) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(45) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(45) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(47) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(47) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(47) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(49) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(49) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(49) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(51) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(51) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(51) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(53) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(53) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(53) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(55) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(55) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(55) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(57) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(57) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(57) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>Build log was saved at "file://c:\Documents and Settings\Anish\My Documents\Visual Studio 2008\Projects\Programming Project 1 Chapter 3\Programming Project 1 Chapter 3\Debug\BuildLog.htm"
1>Programming Project 1 Chapter 3 - 23 error(s), 9 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I think that I am missing or adding simple stuff, causing syntax errors. Commas and the like.
 

anishannayya

Member
Jun 10, 2008
136
0
0
Here it is with the curly braces:

#include <iostream>
using namespace std;

// Filename: Programming Project 1 Chapter 3.cpp
// Author: Anish
// Description: A program to play rock-paper-scissors.
// Date: Saturday, July 20, 2008
// Compiler: Visual C++ 2008 Express Edition

int main ( )
{
char play, one, two, y, Y, p, P, r, R, s, S;

cout << "This is a program that allows you to play rock-paper-scissors.\n"
<< "Would you like to play a game?\n"
<< "Press y for yes or n for no and press enter.\n\n";
cin >> play;
while (play == 'y' || play == 'Y')
{
cout << "Welcome to rock-paper-scissors.\n\n"
<< "Player one: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> one;
cout << "Player one chose: " << one << ".\n"
<< "Player two: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> two;
cout << "Player two chose: " << two << ".\n\n";

{
if (!(one == 'p' || one == 'P' || one == 'r' || one == 'R' || one == 's' || one 'S'))
{
cout << "Player one: " << player_ one << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
}
else if (!(two == 'p' || two == 'P' || two == 'r' || two == 'R' || two == 's' || two 'S'))
{
cout << "Player two: " << two << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
}
else if (one == 'p' || one == 'P') && (two == 'p' || two == 'P')
{
cout << "Nobody wins because you both chose paper.\n";
}
else if (one == 'p' || one == 'P') && (two == 'r' || two == 'R')
{
cout << "Player one wins because paper covers rock.\n";
}
else if (one == 'p' || one == 'P') && (two == 's' || two == 'S')
{
cout << "Player two wins because scissors cut paper.\n";
}
else if (one == 'r' || one == 'R') && (two == 'p' || two == 'P')
{
cout << "Player two wins because paper covers rock.\n";
}
else if (one == 'r' || one == 'R') && (two == 'r' || two == 'R')
{
cout << "Nobody wins because you both choose rock.\n";
}
else if (one == 'r' || one == 'R') && (two == 's' || two == 'S')
{
cout << "Player one wins because rock breaks scissors.\n";
}
else if (one == 's' || one == 'S') && (two == 'p' || two == 'P')
{
cout << "Player one wins because scissors cut paper.\n";
}
else if (one == 's' || one == 'S') && (two == 'r' || two == 'R')
{
cout << "Player two wins because rock breaks scissors.\n";
}
else if (one == 's' || one == 'S') && (two == 's' || two == 'S')
{
cout << "Nobody wins because you both choose scissors.\n";
}
}
}

char c;
cout << "\n"
<< "Press a key and hit enter to exit.\n";
cin >> c;

return 0;
}

Error log:

1>------ Build started: Project: Programming Project 1 Chapter 3, Configuration: Debug Win32 ------
1>Compiling...
1>Programing Project 1 Chapter 3.cpp
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(36) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(37) : error C2065: 'player_' : undeclared identifier
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(37) : error C2146: syntax error : missing ';' before identifier 'one'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2297: '<<' : illegal, right operand has type 'const char [28]'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2297: '<<' : illegal, right operand has type 'const char [49]'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(40) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(40) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(40) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(41) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(45) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(45) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(45) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(46) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(49) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(49) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(49) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(50) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(53) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(53) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(53) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(54) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(57) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(57) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(57) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(58) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(61) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(61) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(61) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(62) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(65) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(65) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(65) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(66) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(69) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(69) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(69) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(70) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(73) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(73) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(73) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(74) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(77) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(77) : error C2143: syntax error : missing ';' before '&&'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(77) : warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(78) : error C2143: syntax error : missing ';' before '{'
1>Build log was saved at "file://c:\Documents and Settings\Anish\My Documents\Visual Studio 2008\Projects\Programming Project 1 Chapter 3\Programming Project 1 Chapter 3\Debug\BuildLog.htm"
1>Programming Project 1 Chapter 3 - 38 error(s), 10 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thanks a million.
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Your if statements are not formed correctly.

All combinations of conditions need to be placed within the outer-most parenthesis.

For example, you are doing:

if (cond1 || cond2) && (cond3 || cond4) ...

That's not valid syntax.

You should be doing:

if ((cond1 || cond2) && (cond3 || cond4)) ...

Likewise, the ; and , characters are not valid within the if conditional block.

These are topics that are fundemental to the understanding of the syntax of not just C++, but of programming in general. You need to go over this assignment with your professor and make sure that he makes sure you understand these concepts. You should have learned these concepts in the first or second lecture. Not knowing proper syntax will more or less guarantee failure in your endeavor to learn how to program.
 

anishannayya

Member
Jun 10, 2008
136
0
0
Like I said, I am not taking a class. This is merely out of interest. I guess I should just read the text a couple times before I do things. I will fix those errors. This is why I love the Internet though; you have everybody's knowledge at your fingertips, all you have to do is ask, and best of all, it is free.
 

anishannayya

Member
Jun 10, 2008
136
0
0
Thank your drebo!

Here is my code now:

#include <iostream>
using namespace std;

// Filename: Programming Project 1 Chapter 3.cpp
// Author: Anish
// Description: A program to play rock-paper-scissors.
// Date: Saturday, July 20, 2008
// Compiler: Visual C++ 2008 Express Edition

int main ( )
{
char play, one, two, y, Y, p, P, r, R, s, S;

cout << "This is a program that allows you to play rock-paper-scissors.\n"
<< "Would you like to play a game?\n"
<< "Press y for yes or n for no and press enter.\n\n";
cin >> play;
while (play == 'y' || play == 'Y')
{
cout << "Welcome to rock-paper-scissors.\n\n"
<< "Player one: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> one;
cout << "Player one chose: " << one << ".\n"
<< "Player two: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n";
cin >> two;
cout << "Player two chose: " << two << ".\n\n";

{
if (!(one == 'p' || one == 'P' || one == 'r' || one == 'R' || one == 's' || one 'S'))
cout << "Player one: " << player_ one << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if (!(two == 'p' || two == 'P' || two == 'r' || two == 'R' || two == 's' || two 'S'))
cout << "Player two: " << two << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if ((one == 'p' || one == 'P') && (two == 'p' || two == 'P'))
cout << "Nobody wins because you both chose paper.\n";
else if ((one == 'p' || one == 'P') && (two == 'r' || two == 'R'))
cout << "Player one wins because paper covers rock.\n";
else if ((one == 'p' || one == 'P') && (two == 's' || two == 'S'))
cout << "Player two wins because scissors cut paper.\n";
else if ((one == 'r' || one == 'R') && (two == 'p' || two == 'P'))
cout << "Player two wins because paper covers rock.\n";
else if ((one == 'r' || one == 'R') && (two == 'r' || two == 'R'))
cout << "Nobody wins because you both choose rock.\n";
else if ((one == 'r' || one == 'R') && (two == 's' || two == 'S'))
cout << "Player one wins because rock breaks scissors.\n";
else if ((one == 's' || one == 'S') && (two == 'p' || two == 'P'))
cout << "Player one wins because scissors cut paper.\n";
else if ((one == 's' || one == 'S') && (two == 'r' || two == 'R'))
cout << "Player two wins because rock breaks scissors.\n";
else if ((one == 's' || one == 'S') && (two == 's' || two == 'S'))
cout << "Nobody wins because you both choose scissors.\n";
}
}

char c;
cout << "\n"
<< "Press a key and hit enter to exit.\n";
cin >> c;

return 0;
}


Here are my errors:

1>------ Build started: Project: Programming Project 1 Chapter 3, Configuration: Debug Win32 ------
1>Compiling...
1>Programing Project 1 Chapter 3.cpp
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(35) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2181: illegal else without matching if
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2143: syntax error : missing ')' before 'constant'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(38) : error C2059: syntax error : ')'
1>c:\documents and settings\anish\my documents\visual studio 2008\projects\programming project 1 chapter 3\programming project 1 chapter 3\programing project 1 chapter 3.cpp(41) : error C2181: illegal else without matching if
1>Build log was saved at "file://c:\Documents and Settings\Anish\My Documents\Visual Studio 2008\Projects\Programming Project 1 Chapter 3\Programming Project 1 Chapter 3\Debug\BuildLog.htm"
1>Programming Project 1 Chapter 3 - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I have tried looking up the errors on the MSDN, but I have no clue what they are talking about. I noticed that I only have 3 errors, the rest are duplicates of the first. Also, when the compiler gives warnings, it is just guessing at the intent of the program because it cannot really detect anything except straight out syntax errors? Finally, how is a compiler created? I highly doubt someone sat down and started typing 01001010 01011000 etc... Sorry about my noobage.
 

anishannayya

Member
Jun 10, 2008
136
0
0
I am such an idiot. I looked at my code carefully and realized that I had forgotten some equal signs, and had an undeclared identifier.

Thanks to all those who helped me! You help is much appreciated.
 

anishannayya

Member
Jun 10, 2008
136
0
0
BTW this is my final code:

#include <iostream>
using namespace std;

// Filename: Programming Project 1 Chapter 3.cpp
// Author: Anish
// Description: A program to play rock-paper-scissors.
// Date: Saturday, July 20, 2008
// Compiler: Visual C++ 2008 Express Edition

int main ( )
{
char play, one, two, y, Y, p, P, r, R, s, S;

cout << "This is a program that allows you to play rock-paper-scissors.\n"
<< "Would you like to play a game?\n"
<< "Press y for yes or n for no and press enter.\n\n";
cin >> play;
cout << endl;
while (play == 'y' || play == 'Y')
{
cout << "Welcome to rock-paper-scissors.\n\n"
<< "Player one: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n"
<< endl;
cin >> one;
cout << endl
<< "Player one chose: " << one << "\n" << endl
<< "Player two: Please choose rock, paper, or scissors.\n"
<< "For paper press p and then enter.\n"
<< "For rock press r and then enter.\n"
<< "For scissors press p and then enter.\n"
<< endl;
cin >> two;
cout << endl
<< "Player two chose: " << two << "\n\n";

{
if (!(one == 'p' || one == 'P' || one == 'r' || one == 'R' || one == 's' || one == 'S'))
cout << "Player one: " << one << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if (!(two == 'p' || two == 'P' || two == 'r' || two == 'R' || two == 's' || two == 'S'))
cout << "Player two: " << two << " is not a valid selection.\n"
<< "Please play again and choose a valid selection.\n";
else if ((one == 'p' || one == 'P') && (two == 'p' || two == 'P'))
cout << "Nobody wins because you both chose paper.\n";
else if ((one == 'p' || one == 'P') && (two == 'r' || two == 'R'))
cout << "Player one wins because paper covers rock.\n";
else if ((one == 'p' || one == 'P') && (two == 's' || two == 'S'))
cout << "Player two wins because scissors cut paper.\n";
else if ((one == 'r' || one == 'R') && (two == 'p' || two == 'P'))
cout << "Player two wins because paper covers rock.\n";
else if ((one == 'r' || one == 'R') && (two == 'r' || two == 'R'))
cout << "Nobody wins because you both choose rock.\n";
else if ((one == 'r' || one == 'R') && (two == 's' || two == 'S'))
cout << "Player one wins because rock breaks scissors.\n";
else if ((one == 's' || one == 'S') && (two == 'p' || two == 'P'))
cout << "Player one wins because scissors cut paper.\n";
else if ((one == 's' || one == 'S') && (two == 'r' || two == 'R'))
cout << "Player two wins because rock breaks scissors.\n";
else if ((one == 's' || one == 'S') && (two == 's' || two == 'S'))
cout << "Nobody wins because you both choose scissors.\n";
}

cout << endl
<< "Would you like to play another game?\n"
<< "Press y for yes or n for no and press enter.\n\n";
cin >> play;
cout << endl;
}

char c;
cout << endl
<< "Press a key and hit enter to exit\n."
<< endl;
cin >> c;

return 0;
}
 
Last edited:

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
Pro tip: Your code is unbearably sloppy. This can lead to a multitude of problems, including (but not limited to):

1) It's hard for other people to understand your code when looking at it.
2) It's hard for YOU to understand your code when looking at it after not looking at it for some time.
3) It's hard to add more functionality to your code later.
4) Requires massive editing to make minor functional changes.

Here are some tips:
1) Your if/then/else logic is called a "sieve" method. This is fine for very small things (~3 possibilities maybe), but as you can see, it looks pretty jumbled. There's a better way.

Look up "switch statements" in your text book. This will provide a better option for what you're doing. It's a lot cleaner, easier to read, and probably even more efficient (depending on the compiler).

2) Separate your code into logical "blocks", then comment every block. Write comments in plain english, spanish, or what have you. Make your comments so that ANYONE, even people that don't program, can understand them.

3) Make your identifiers (variable names) more descriptive. I honestly had no idea what the names "play, one, two, y, Y, p, P, r, R, s, and S" were supposed to represent.

4) In general, it sounds like you're jumping the gun... go back a couple sections/chapters in your book and take it slow. It looks like you missed a lot of basic, really important skills.


^^ That's all I got for you right now. When I'm bored later tomorrow, I'll re-write your entire program and show you a cleaner, easier way to do it. Hopefully you'll be able to learn from it :)
 

bharatwaja

Senior member
Dec 20, 2007
431
0
0
Player 2 shouldn't be able to see what player one has chosen, cause there is no point in playing the game after that..... You could use a getch() to get player one's input and not print it to the screen until both players have chosen.... Logically that is the way to do this, i'm sure others wud agree to this too....

even when using
cin>>one;
what player one enters can be view by player 2, so its not even a game, player 2 can win all the time....
you could use
one=getch();
This wouldn't print one's selection to the screen... I guess this is still correct, its been a long time since i've done programming with c/c++....

**Edit**
Is there a getch() in visual studio? I used turbo-c while in school, so this would be valid in that... but have no idea if this is supported in Visual studio....
 

tuteja1986

Diamond Member
Jun 1, 2005
3,676
0
0
I will fix it :!

err... the code is so messy :!

Some one needs to teach you how to write clean code :! I am just going to rewrite the program :(
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
y, Y, p, P, r, R, s and S were never used. Also, you may want to do error checking (i.e. not a valid selection) first and then play the game once both players have a valid selection. Along the lines of what others said, that bunch of If statements is a jumbled mess... you may want to think of at least nesting the IFs or using a case statement as mentioned.
 

anishannayya

Member
Jun 10, 2008
136
0
0
Originally posted by: bharatwaja
Player 2 shouldn't be able to see what player one has chosen, cause there is no point in playing the game after that..... You could use a getch() to get player one's input and not print it to the screen until both players have chosen.... Logically that is the way to do this, i'm sure others wud agree to this too....

even when using
cin>>one;
what player one enters can be view by player 2, so its not even a game, player 2 can win all the time....
you could use
one=getch();
This wouldn't print one's selection to the screen... I guess this is still correct, its been a long time since i've done programming with c/c++....

**Edit**
Is there a getch() in visual studio? I used turbo-c while in school, so this would be valid in that... but have no idea if this is supported in Visual studio....

I know that the player can see what the other is doing. All I have done so far is flow control, multiway branches, data types and expressions, and variables and assignments. So, I haven't got that far yet.
 

anishannayya

Member
Jun 10, 2008
136
0
0
Originally posted by: Aikouka
y, Y, p, P, r, R, s and S were never used. Also, you may want to do error checking (i.e. not a valid selection) first and then play the game once both players have a valid selection. Along the lines of what others said, that bunch of If statements is a jumbled mess... you may want to think of at least nesting the IFs or using a case statement as mentioned.

I have done switch. as in:

switch (month)
{
case 1:
cout << "There are 31 days in Janurary.\n\n";
break;
case 2:
if (year % 4==0)
cout << "February had 29 days. It is not a leap year.\n\n";
else
cout << "February had 28 days. It is not a leap year.\n\n";
break;

ect...

The program would essentially tell you how many days there were in a month given the year and the month represented in digits. for example March = 3. How would I use it in my program above, though? I know that someone will show me, so I shall be patient.
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,176
516
126
Also I would go learn the toLower or toUpper functions, and remove all that extra junk in the if statements. Also what happens if someone types in an "o" by accident when he/she meant "p" (fat fingers)? In your code it will probably say "Do you want to play again?"....

You need to learn rule one of programming with user interaction.

1) Never trust the user input

Why? Because it is usually wrong. People can't follow directions because they don't bother to read them. Or they simply make mistakes. You need to code for those mistakes. Do something like create a function called "checkinput" and have it test your cin values. You should also look at the function "toLower" and run that against your user input. This way you only need to do your checks against a "r", "p", or "s" not both "r" || "R", "p" || "P", "s" || "S".

You could also make it into a case statement by converting the string input values into numbers and subtracting the player one's answer from player two's answer (i.e. r = 3, p = 5, s = 13... case 0 "No winner. Both choose same." case -2 "Player 2 wins. Paper covers rock." case -10 "Player 1 wins. Rock smashes scissors." case 2 "Player 1 wins. Paper covers rock." case -7 "Player 2 wins. Scissors cut paper." case 10 "Player 2 wins. Rock smashes scissors." case 7 "Player 1 wins. Scissors cut paper.") You will learn that switch/case statements on numbers run faster in C++ than if statements on string/char's. You also elliminate 3 complete if/else if groups with this method since all values when both people choose the same result in 0.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
As has been stated above, you're code is pretty messy and almost unreadable. Here are a few things that might help a bit.

1. instead of doing TONS of uppercase lowercase stuff. Convert the input to begin with to upper or lower case. That will make your code more readable (toupper will do what you want)

2. In this case, it is easier to check for valid cases first then lump everything else into an invalid statement, like an open else or default in a case statement.

3. try making functions, functions are your friends :D.

Here is an example how I would do the exact same program

#include <iostream>
#include <ctype.h>

using namespace std;

// Checks if player one or two won, Returns both true if theres a tie.
void isWin(char p1Input, char p2Input, bool& p1win, bool& p2win)
{
if(p1Input == p2Input) // Tie game
{
p1win = true;
p2win = true;
}
else
{
if (p1Input == 'R')
p1win = (p2Input == 'S');

if (p1Input == 'P')
p1win = (p2Input == 'R');

if (p1Input == 'S')
p1win = (p2Input == 'P');

p2win = !p1win;
}
}

bool playAgain()
{
char input;
cout << "Would you like to play Rock-Paper-Scissors(Y/N)?";

cin >> input;
input = toupper(input);

return (input == 'Y');
}

// gets and formats the input. Won't quit until input is correct.
char getInput()
{
char input = 'x';
while((input != 'R') && (input != 'P') && (input != 'S'))
{
cout << "Enter R for rock, P for paper or S for Scissors: ";
cin >> input;
input = toupper(input);
}
return input;
}

int main()
{
while(playAgain())
{
char p1;
char p2;

cout << "It's your turn Player 1\n";
p1 = getInput();
cout << "Player 2, it's your turn\n";
p2 = getInput();

bool winner[2];

isWin(p1, p2, winner[0], winner[1]);

if(winner[0] == winner[1])
{
cout << "Its a Tie!\n";
}
else
{
cout << "Player " << winner[1] + 1 << " wins!\n";
}
}
}

Minus some output formating stuff, this code looks much cleaner and handles the situation quite nicely. (and was even 2 lines shorter then your code :p)
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Originally posted by: Fallen Kell
Also I would go learn the toLower or toUpper functions, and remove all that extra junk in the if statements. Also what happens if someone types in an "o" by accident when he/she meant "p" (fat fingers)? In your code it will probably say "Do you want to play again?"....

You need to learn rule one of programming with user interaction.

1) Never trust the user input

Why? Because it is usually wrong. People can't follow directions because they don't bother to read them. Or they simply make mistakes. You need to code for those mistakes. Do something like create a function called "checkinput" and have it test your cin values. You should also look at the function "toLower" and run that against your user input. This way you only need to do your checks against a "r", "p", or "s" not both "r" || "R", "p" || "P", "s" || "S".

You could also make it into a case statement by converting the string input values into numbers and subtracting the player one's answer from player two's answer (i.e. r = 3, p = 5, s = 13... case 0 "No winner. Both choose same." case -2 "Player 2 wins. Paper covers rock." case -10 "Player 1 wins. Rock smashes scissors." case 2 "Player 1 wins. Paper covers rock." case -7 "Player 2 wins. Scissors cut paper." case 10 "Player 2 wins. Rock smashes scissors." case 7 "Player 1 wins. Scissors cut paper.") You will learn that switch/case statements on numbers run faster in C++ than if statements on string/char's. You also elliminate 3 complete if/else if groups with this method since all values when both people choose the same result in 0.

NOT TRUE! what is a char? for all intents and purposes it is an 8 bit hunk of data. (try 'a' - 1 you'll get like 62 or something). For string object comparisons I could see this being true. even for char arrays, but not for a single char character.

*Edit* ok, I was thinking case statements on chars rather then if statements. I guess your response is somewhat valid. but converting from a char to any other character waists more resources then just processing the char in a case statement.
 

bharatwaja

Senior member
Dec 20, 2007
431
0
0
Originally posted by: Fallen Kell
You will learn that switch/case statements on numbers run faster in C++ than if statements on string/char's.

In this program, there is not going to be any difference in speed, any difference that is perceptible to humans i mean....



Originally posted by: anishannayya
I have done switch. as in:

switch (month)
{
case 1:
cout << "There are 31 days in Janurary.\n\n";
break;
case 2:
if (year % 4==0)
cout << "February had 29 days. It is not a leap year.\n\n";
else
cout << "February had 28 days. It is not a leap year.\n\n";
break;

ect...

The program would essentially tell you how many days there were in a month given the year and the month represented in digits. for example March = 3. How would I use it in my program above, though? I know that someone will show me, so I shall be patient.

use case statements on char like this...

switch(variable)
{
case 'a','A':
.........
.........
break;
case 'b','B':
..........
..........
break;
//and so on..........
}

It is better to convert the user's input to upper or lower case as you prefer, cause it will take out the clutter in your programs. Also, in case statements, to assign one case to multiple choices jus use a comma to separate them, like I have used above.

Use comments in your code to make it more understandable. And use functions to modularize your code.
Originally posted by: Cogman
try making functions, functions are your friends :D.
:thumbsup:
 

Sadaiyappan

Golden Member
Nov 29, 2007
1,120
4
81
I would do a few things. I would create a class for rock, class for paper and class for scissors, and I would use a switch statement instead of so many if elses... That is the whole point of the switch statement is in cases like this where there is a lot of if elses... BTW I am a first semester student myself..
 

Sadaiyappan

Golden Member
Nov 29, 2007
1,120
4
81
Or even easier instead of using a class use ENUM for rock paper scissors, with rock = 0, paper = 1, scissors = 2 etc..
 

theawddone

Member
Sep 1, 2006
55
0
0
Originally posted by: Sadaiyappan
Or even easier instead of using a class use ENUM for rock paper scissors, with rock = 0, paper = 1, scissors = 2 etc..

This probably wouldn't work out too well for comparisons, but the previous idea seems somewhat reasonable if you could perhaps implement it to be 'comparable' (a Java concept, but I don't know if you can do it in C++, never used it).
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Originally posted by: Sadaiyappan
I would do a few things. I would create a class for rock, class for paper and class for scissors, and I would use a switch statement instead of so many if elses... That is the whole point of the switch statement is in cases like this where there is a lot of if elses... BTW I am a first semester student myself..

Doing a bunch of object classes for this type of problem is over kill. If you where doing something more then a string comparison then maybe, but since it is just a "Rock beats scissors" type of application it is much easier to just do a quick comparison. (see my solution)

For a class comparison to work, you would have 3 different classes with 9 different operator overloads, mm, yeah, no thank you.