这是我的石头纸剪刀游戏的踪迹.. [英] This is my trail for stone paper scissor game..

查看:59
本文介绍了这是我的石头纸剪刀游戏的踪迹..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但它一直显示错误:



无法将char转换为char *



请识别问题



我的尝试:



  #include   <   iostream.h  >  
#include < conio.h >
#include < stdlib.h >

char compare( char choice1, choice2);
void main()
{
char choice1,choice2;

cout<< \ n \ n欢迎光临石头剪刀< /跨度><< ENDL<< ENDL;
cout<< 输入您的选择<< endl;

cout<< \ n 1:SCISSORS \ n;
cout<< \ n 2:STONE \ n;
cout<< \ n 3:PAPER \\\
;
cout<< \ n \ nENTER选择CAPS \ n \ n;
cin>> choice1;

randomize();
float point = 2 ;
float compchoice;
compchoice = random(point);

if (compchoice< 0。 34
{
choice2 = SCISSORS;

}
else if (compchoice< 0。 67
{
choice2 = ROCK;
}
else
{
choice2 = PAPER;
}
比较(choice1,choice2);
getch();
}
char compare( char choice1, char choice2)
{
if (choice1 == choice2)
{ do {
cout<< 结果是平局! << ENDL;
return 再试一次;
} while (choice1!= choice2);
}
else if (choice1 == ROCK
{
if ( choice2 == PAPER
{
return PAPER WINS);
}
else
return ROCK WINS);

}

else if (choice1 = = PAPER
{
if (choice2 == SCISSORS
{
return SCISSORS WIN );
}
else
return PAPER WINS);
}

else if (choice1 == SCISSORS
{
if (choice2 == ROCK
{
return ROCK WINS; }
else
return SCISSORS WINS;
}
}

解决方案

浏览发生错误的行会告诉您正在尝试将字符串文字分配给字符变量。您的变量应分配为 char * 类型。完成后,您需要删除所有这些相等测试,并将调用替换为 strcmp 功能 [ ^ ]。


从技术上讲,你的问题是比较实现:它应该是

  const   char  * compare( const   char  * choice1, const   char  * choice2)
{
if (strcmp(choice1, SCISSORS)== 0
{
if (strcmp(choice2, SCISSORS)== 0
return 再试一次;
else if (strcmp(choice2, ROCK)== 0
返回 ROCK WINS;
else if (strcmp(choice2, PAPER)== 0
返回 SCISSORS WIN;
else
return INVALID choice2; // 处理错误输入
}
else if (strcmp(choice1, ROCK)== 0
{
// ...
}
else // 这里我们实际上并不需要'else if(strcmp(choice1,PAPER)== 0)'
{
// ...
}
}





但是,只要变量'natural'类型是 enum,你就不应该使用字符串关合作。


But it keeps showing the error:

cannot convert char to char*

Kindly identify the problem

What I have tried:

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

char compare(char choice1,char choice2);
void main()
{
char choice1,choice2;

cout<<"\n\n WELCOME TO STONE PAPER SCISSORS "<<endl<<endl;
cout<<" enter your choice"<<endl;

cout<<"\n 1:SCISSORS \n";
cout<<"\n 2:STONE \n";
cout<<"\n 3:PAPER \n";
cout<<"\n\nENTER CHOICE IN CAPS\n\n"              ;
 cin>>choice1;

 randomize();
 float point =2;
 float compchoice;
 compchoice=random(point);

 if (compchoice<0.34)
     {
     choice2 ="SCISSORS"    ;

     }
   else  if(compchoice<0.67)
   {
   choice2="ROCK";
   }
   else
   {
   choice2="PAPER";
   }
   compare(choice1,choice2);
   getch();
   }
   char compare(char choice1,char choice2)
   {
   if(choice1==choice2)
   { do{
   cout<<"result is a tie!"<<endl;
   return "try again" ;
	}while(choice1!=choice2);
	}
	else if(choice1=="ROCK")
	{
	if(choice2=="PAPER")
	{
	 return ("PAPER WINS")  ;
	    }
	 else
	 return ("ROCK WINS")  ;

	 }

	 else if(choice1=="PAPER")
	 {
	 if(choice2=="SCISSORS")
	 {
	 return ("SCISSORS WIN")    ;
		  }
	 else
	 return  ("PAPER WINS") ;
	 }

   else if(choice1=="SCISSORS")
   {
   if(choice2=="ROCK")
   {
   return "ROCK WINS";  }
   else
   return " SCISSORS WINS";
   }
   }

解决方案

A glance at the line where the error occurred would tell you that you are trying to assign a string literal to a character variable. Your variables should be assigned as char* types. And after doing that you need to remove all those equality tests and replace with calls to the strcmp function[^].


Technically your problem is in compare implementation: it should be

const char * compare (const char * choice1, const char * choice2)
{
  if ( strcmp(choice1, "SCISSORS") == 0 )
  {
    if ( strcmp(choice2, "SCISSORS") == 0 ) 
      return "try again";
    else if ( strcmp(choice2, "ROCK") == 0 )
      return "ROCK WINS";
    else if ( strcmp(choice2, "PAPER") == 0 )
      return "SCISSORS WIN";
    else
      return "INVALID choice2"; // handle wrong input
  }
  else if ( strcmp(choice1, "ROCK") == 0 )
  {
    //...
  } 
  else // here we don't actually need 'else if ( strcmp(choice1, "PAPER") == 0 )'
  {
    //...
  }
}



However, you shouldn't actually use a string whenever the variable 'natural' type is the enumeration.


这篇关于这是我的石头纸剪刀游戏的踪迹..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆