Need Assistance; cannot find the error in my code; R/P/S game.
Mar 24, 2014 at 1:17am UTC
Need help, I keep getting errors in my char computeWinner function, and I do not understand what the compiler means by "invalid conversion from 'const char*' to 'char.'" I can provide full code if you deem it necessary.
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
char computeWinner ( int computer_choice , int user_choice ){;
char winner;
//Case One
if (user_choice == 1 && computer_choice ==3){
winner = "U" ;
}
else if (user_choice==1 && computer_choice==2){
winner = "C" ;
}else {
winner = "T" ;
}
//Case Two
if (user_choice == 2 && computer_choice ==1){
winner = "U" ;
}
else if (user_choice==2 && computer_choice==2){
winner = "T" ;
}else {
winner = "C" ;
}
//Case Three
if (user_choice == 3 && computer_choice ==1) {
winner="C" ;
}
else if (user_choice==3 && computer_choice==2){
winner="U" ;
}else {
winner="T" ;
}
return winner;
Last edited on Mar 24, 2014 at 1:18am UTC
Mar 24, 2014 at 1:26am UTC
The second part I have issues with is calling in the computeWinner variable in my void playGame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void playGame(){
int c,i;
char w;
i = userChoice();
c = computer_choice;
printComputerChoice;
w = computeWinner(c,i);
cout<<w;
if (w=='U' ){
cout<<"User Won." ;
}
else if (w=='C' ){
cout<<"Computer Won." ;
}else {
cout<< "Its a tie." ;
}
void playGame();
}
Mar 24, 2014 at 1:36am UTC
..."invalid conversion from 'const char*' to 'char.'"
1 2
char ch = 'U' ;
const char * = "U" ;
Double quotes are for character strings and single quotes are for individual characters.
The second part I have issues with is calling in the computeWinner variable...
Can you provide the error message? And it would be nice to know how playGame and computeWinner are declared, i.e. if they are global, the order they are declared, etc.
For example:
1 2 3 4
void playGame(){computeWinner(4, 5);}
char computeWinner(int , int );
//Because computeWinner is declared after playGame, playGame does not know
// that computeWinner exists.
Random Note:
computerWinner is a function, not a variable.
Last edited on Mar 24, 2014 at 1:37am UTC
Mar 24, 2014 at 1:37am UTC
This is the Complete Code.
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
int computerChoice() {
const int maxrandnum = 3;
srand(time(NULL));
int computer_choice = rand()%maxrandnum+1;
return computer_choice;
}
int userChoice() {
cout << "Choose rock=1, Paper=2, Scissors=3 " <<endl;
int choice;
cin>>choice;
return choice;
}
void printComputerChoice ( int computer_choice ){
cout<<"The computer chose " <<computer_choice<<endl;
}
void playGame(){
int c,i;
char w;
i = userChoice();
c = computer_choice;
printComputerChoice;
w = computeWinner(c,i);
cout<<w;
if (w=='U' ){
cout<<"User Won." ;
}
else if (w=='C' ){
cout<<"Computer Won." ;
}else {
cout<< "Its a tie." ;
}
playGame;
}
char computeWinner ( int computer_choice , int user_choice ){;
char winner;
//Case One
if (user_choice == 1 && computer_choice ==3){
winner ="U" ;
}
else if (user_choice==1 && computer_choice==2){
winner ="C" ;
}else {
winner ="T" ;
}
//Case Two
if (user_choice == 2 && computer_choice ==1){
winner ="U" ;
}
else if (user_choice==2 && computer_choice==2){
winner ="T" ;
}else {
winner ="C" ;
}
//Case Three
if (user_choice == 3 && computer_choice ==1) {
winner="C" ;
}
else if (user_choice==3 && computer_choice==2){
winner="U" ;
}else {
winner="T" ;
}
return winner;
}
int main(){
playGame();
return 0;
}
Mar 24, 2014 at 2:00am UTC
Thank you; that fixed it. In the future, would creating a prototype function make the program independent of the ordering?
Mar 24, 2014 at 2:03am UTC
Yes. The second benefit is that you'll have a readable list of available functions without all those definitions in the way.
Topic archived. No new replies allowed.