For my class, I am creating a rock paper scissors game. My code is not all the way done, but I am stuck on a certain part of the process. For some reason, I am getting errors on the "==" in:

while (true)
{
    human = humanChoice();
    computer = computerChoice();
    if (human == 'Q' || human == 'q') break;
    if (human == 'R' || human == 'r')
}

It tells me that no operator was found for the "==". I've used this code block in many other programs but it just isn't working this game.

My full code is:

#include <iostream>
using namespace std;

char computerChoice()
{
	char rock = 'R';
	return rock;
}

char humanChoice()
{
	char choice = "";
	
	while(true)
	{
		cout << "Choose(Rock,Paper,Scissors,or Quit): ";
		cin >> choice;
		cin.ignore(1000, 10);

		if (choice == "Q" || "q") break;
		if (choice == "R" || choice == "r") break;
		if (choice == "S" || choice == "s") break;
		if (choice == "P" || choice == 'p') break;
	}
	return choice;
}
void printScore()
{

}
int main()
{
	// initialize the computer's random number generator
	srand(time(0));

	// declare variables
	string human;
	string computer;

	// start loop
	while (true)
	{
    human = humanChoice();
	computer = computerChoice();
    if (human == 'Q' || human == 'q') break;
    if (human == 'R' || human == 'r')
	}

	
    // determine computer's choice
    // prompt for, and read, the human's choice
    // if human wants to quit, break out of loop
    // print results
    // end loop
    // end program

	cout << "Press ENTER to continue..." << endl;
	cin.get();

	return 0;
}

You're trying to compare a string with a char--

//...

char humanChoice(){
//...
}

//...
	// declare variables
	string human;
//...

 human = humanChoice(); // assignment, not construction - http://www.cplusplus.com/reference/string/string/operator=/

if (human == 'Q' || human == 'q') break; // doesnt work - string == char isn't defined

-- The easy solution is to change the types of your declared variables in main--

// declare variables
	char human;
	char computer;

http://www.cplusplus.com/reference/string/string/string/
http://www.cplusplus.com/reference/string/operators/

Okay, I changed those two variables to char. However, I am now getting the error no const char* to int. I don't understand what that means. It is coming up for the lines in my subprogram.

if (choice == "Q" || "q") break;
if (choice == "R" || choice == "r") break;
if (choice == "S" || choice == "s") break;
if (choice == "P" || choice == 'p') break;

Just for the record--

const char* x = "";

-- is valid because "" means a 0 length string. Anytime you use the quotes versus apostrophes you're dealing with const char* type.

char y = ' ';

-- This uses apostrophes to make a space-char and assign it to y.

Take these two things into account and relook at your code. You'll realize that--

char humanChoice()
{
	char choice = ""; // choice is type char but you're trying to assign it a string
	
	while(true)
	{
		cout << "Choose(Rock,Paper,Scissors,or Quit): ";
		cin >> choice;
		cin.ignore(1000, 10);

                // char versus const char* type not defined
                // where "Q" and "q" is the const char* and choice is the char
		if (choice == "Q" || "q") break;

                // same cases here...
		if (choice == "R" || choice == "r") break;
		if (choice == "S" || choice == "s") break;
		if (choice == "P" || choice == 'p') break;
	}
	return choice;
}

-- try to be consistent and remember what quotes around letters mean versus apostrophes.

char choice = "";

This is still a char vs string since "" is the empty string. Change '' to be a valid char.

Likewise "q" is a string whereas 'q' is a char.

Thanks! I didn't know I still had it like that.

I have another question related to this code. I have to add a function to print the score each time. I'm using a void function. The void function is:

void printScore()
{
	char human;
	char computer;
	human = humanChoice();
	computer = computerChoice();

	if (human == 'R' || human == 'r')
		cout << "Human: R" << endl;
	if (human == 'S' || human == 's')
		cout << "Human: S" << endl;
	if (human == 'P' || human == 'p')
		cout << "Human: P" << endl;
	if (computer == 0)
		cout << "Computer: R" << endl;
	if (computer == 1)
		cout << "Computer: P" << endl;
	if (computer == 2)
		cout << "Computer: S" << endl;
}

How do I use the function to print the scores in int main? I tried doing:

while (true)
{
    human = humanChoice();
    computer = computerChoice();
    if (human == 'Q' || human == 'q') break;
    if (human == 'R' || human == 'r')
       printScore();

But that doesn't work.

I would add that a switch statement is preferable to a long series of if statements.

Just call printScore without a condition or the computer choice since you're performing the game-choices in printScore anyways.

//...

while (true)
{
    cout << "Press Q if you want to quit, or any other key to continue" << endl;
    char theDecision;
    cin >> theDecision;
    if (theDecision == 'Q' || theDecision == 'q') break;
       printScore();

//...
void printScore()
{
	char human;
	char computer;
        cout << "Getting the human choice and computer choice: " << endl;
	human = humanChoice();
	computer = computerChoice();

	if (human == 'R' || human == 'r')
		cout << "\tHuman: R" << endl;
	else if(human == 'S' || human == 's')
		cout << "\tHuman: S" << endl;
	else if(human == 'P' || human == 'p')
		cout << "\tHuman: P" << endl;
        else cout << "ERROR! Invalid Human Choice!" << endl;

	if (computer == 0)
		cout << "\tComputer: R" << endl;
	else if (computer == 1)
		cout << "\tComputer: P" << endl;
	else if (computer == 2)
		cout << "\tComputer: S" << endl;
}

-- It's also good to add output between function calls to see which function you're stepping into or which line of instruction is reached.

I can't really see where the error is unless it's a logic error? Can you post all of your new code for us to see (i.e. changes to humanChoice or computerChoice)?

I call to the function after my break statement but it doesn't print the human choice.

This is everything

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

char computerChoice()
{
	char rock = 'R';
	return rock;
}

char humanChoice()
{
	char choice;
	
	while(true)
	{
		// prompt for, and read, the human's choice
		cout << "Choose(Rock,Paper,Scissors,or Quit): ";
		cin >> choice;
		cin.ignore(1000, 10);

		// if human wants to quit, break out of loop
		if (choice == 'Q' || 'q') break;
		if (choice == 'R' || choice == 'r') break;
		if (choice == 'S' || choice == 's') break;
		if (choice == 'P' || choice == 'p') break;
	}
	return choice;
}
void printScore()
{
	char human;
	char computer;
	human = humanChoice();
	computer = computerChoice();

	// human choice
	if (human == 'R' || human == 'r')
		cout << "Human: R" << endl;
	else if (human == 'S' || human == 's')
		cout << "Human: S" << endl;
	else if (human == 'P' || human == 'p')
		cout << "Human: P" << endl;
	else cout << "Invalid Choice" << endl;

	// computer choice
	if (computer == 0)
		cout << "Computer: R" << endl;
	else if (computer == 1)
		cout << "Computer: P" << endl;
	else if (computer == 2)
		cout << "Computer: S" << endl;
}
int main()
{
	// initialize the computer's random number generator
	srand(time(0));

	// declare variables
	char human;
	char computer;

	// start loop
	while (true)
	{
    human = humanChoice();
	computer = computerChoice();
    if (human == 'Q' || human == 'q') break;
    printScore();
	}

	
    // determine computer's choice
    // print results
	// end loop
	// end program

	cout << "Press ENTER to continue..." << endl;
	cin.get();

	return 0;
}

I call to the function after my break statement but it doesn't print the human choice.

This is everything I have done so far. I wanted to leave the humanChoice in there because I am calling to that function for the user input.

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

char computerChoice()
{
	char rock = 'R';
	return rock;
}

char humanChoice()
{
	char choice;
	
	while(true)
	{
		// prompt for, and read, the human's choice
		cout << "Choose(Rock,Paper,Scissors,or Quit): ";
		cin >> choice;
		cin.ignore(1000, 10);

		// if human wants to quit, break out of loop
		if (choice == 'Q' || 'q') break;
		if (choice == 'R' || choice == 'r') break;
		if (choice == 'S' || choice == 's') break;
		if (choice == 'P' || choice == 'p') break;
	}
	return choice;
}
void printScore()
{
	char human;
	char computer;

	human = humanChoice();
	computer = computerChoice();

	// human choice
	if (human == 'R' || human == 'r')
		cout << "Human: R" << endl;
	else if (human == 'S' || human == 's')
		cout << "Human: S" << endl;
	else if (human == 'P' || human == 'p')
		cout << "Human: P" << endl;
	else cout << "Invalid Choice" << endl;

	// computer choice
	if (computer == 0)
		cout << "Computer: R" << endl;
	else if (computer == 1)
		cout << "Computer: P" << endl;
	else if (computer == 2)
		cout << "Computer: S" << endl;
}
int main()
{
	// initialize the computer's random number generator
	srand(time(0));

	// declare variables
	char human;
	char computer;

	// start loop
	while (true)
	{
    human = humanChoice();
	computer = computerChoice();
    if (human == 'Q' || human == 'q') break;
    printScore();
	}

	
    // determine computer's choice
    // print results
	// end loop
	// end program

	cout << "Press ENTER to continue..." << endl;
	cin.get();

	return 0;
}

Just noticed this, but you're comparing the computer(s) characters to integers --

// computer choice
	if (computer == 0)
		cout << "Computer: R" << endl;
	else if (computer == 1)
		cout << "Computer: P" << endl;
	else if (computer == 2)
		cout << "Computer: S" << endl;

-- that would be fine if you were using the appropriate ASCII char masks to compare against 'R', 'P' and 'S' but you are not. You should change the numbers to the appropriate character.

In addition, I still do not understand why you want to call humanChoice twice (once before printScore and then after). You can use cin to a varible so you aren't printing weird data to the screen... it's not really intuitive to the action you are performing.

When I use cin instead of what it is now, I have to press a key before it asks the user for R,P,S or Q. And that is not what I wanted to do. However, it is printing this way.

I have it so it does print but after I input any key. I added the computer choice so it makes a bit more sense as to why I have the values 0,1,and 2 compared to char characters. And that part works. The other problem it is now having is that it is not quitting out when I put q in. I don't understand what I did to my loop to make it not quit. It just displays invalid choice as it would for any character other than p,s,r, and q. It quits out when I call to the computerChoice and humanChoice function in the loop. Here is what I have so far:

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

char computerChoice()
{
	char compChoice = rand() % 3;
	return compChoice;
}

char humanChoice()
{
	char choice;
	
	while(true)
	{
		// prompt for, and read, the human's choice
		cout << "Choose(Rock,Paper,Scissors,or Quit): ";
		cin >> choice;
		cin.ignore(1000, 10);

		// if human wants to quit, break out of loop
		if (choice == 'Q' || 'q') break;
		if (choice == 'R' || choice == 'r') break;
		if (choice == 'S' || choice == 's') break;
		if (choice == 'P' || choice == 'p') break;
	}
	return choice;
}
void printScore()
{
	char human;
	char computer;
	human = humanChoice();
	computer = computerChoice();

	// human choice
	if (human == 'R' || human == 'r')
		cout << "Human: R ";
	else if (human == 'S' || human == 's')
		cout << "Human: S ";
	else if (human == 'P' || human == 'p')
		cout << "Human: P ";
	else cout << "Invalid Choice" << endl;

	// computer choice
	if (computer == 0)
		cout << "Computer: R " << endl;
	else if (computer == 1)
		cout << "Computer: P " << endl;
	else if (computer == 2)
		cout << "Computer: S " << endl;
}
int main()
{
	// initialize the computer's random number generator
	srand(time(0));

	// declare variables
	char human = humanChoice();
	char computer;

	// start loop
	while (true)
	{
		if (human == 'Q' || human == 'q') break;
		printScore();
	}

	
    // determine computer's choice
    // print results
	// end loop
	// end program

	cout << "Press ENTER to continue..." << endl;
	cin.get();

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.