如何在程序中更正此错误? [英] How do I correct this error in my program?
问题描述
错误:需要L值
来自程序运行..
plz帮我纠正此错误
我的尝试:
error : L value required
comes during program run..
plz help me to correct this error
What I have tried:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char choice1[256];
char choice2[256];
const char * compare(const char * ,const char * ) ; //prototype
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" ; //l value required error
}
else if(compchoice<0.67)
{
choice2="ROCK"; //l value required error
}
else
{
choice2="PAPER"; //l value required error
}
cout<<"User Choice="<<choice1<<endl;
cout<<"Computer Choice="<<choice2<<endl;
cout<<choice1<<"\t"<<"VS"<<"\t"<<choice2<<"="<<compare(choice1,choice2);
getch();
}
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 CHOICE";
}
else if (strcmp(choice1,"ROCK")==0)
{
if ( strcmp(choice2, "ROCK")==0)
return "try again";
else if ( strcmp(choice2,"PAPER") == 0 )
return "PAPER WINS";
else if ( strcmp(choice2, "SCISSORS")==0)
return " ROCK WINS";
else
return "INVALID CHOICE";
}
else if ( strcmp(choice1, "PAPER")==0)
{
if ( strcmp(choice2,"PAPER") == 0 )
return "try again!";
else if ( strcmp(choice2, "SCISSORS")==0)
return "SCISSORS WINS";
else if ( strcmp(choice2, "ROCK")==0)
return "PAPER WINS";
else
return "INVALID CHOICE";
}
else
{
return "INVALID";
}
}
推荐答案
char choice2[256];
...
if (compchoice<0.34)
{
choice2 ="SCISSORS" ; //l value required error
You不能以这种方式将字符串分配(或比较)到数组。您需要使用 strcpy
,正如我在您之前关于此主题的问题中所建议的那样。
您的时间会更好在尝试此应用程序之前,花了很多时间研究了一个很好的C / C ++参考,以便清楚地了解基础知识。
You cannot assign (or compare) a string to an array in this way. You need to use strcpy
as I suggested in your previous question on this subject.
Your time would be better spent studying a good C/C++ reference to get the basics clear, before attempting this application.
为什么要使用字符串呢?因为老师问过你?
在这种情况下使用整数会更容易。
首先定义你的选择
Why use strings at all? Because the teacher asked you to?
In this case it would be so much easier to use integers.
First define your choices
#define STONE 1
#define PAPER 2
#define SCISSORS 3
const char* compare(int, int); //prototype
int choice1;
int 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\nSelect a number\n\n";
cin>>choice1;
if ((choice < STONE) || (choice > SCISSORS))
// Do some error handling
然后你可以用这种方式决定choice2
Then you can decide choice2 in this fashion
choice2 = (int)ceil(compchoice * 3.0);
比较函数也不会混乱:
And the comparison function will be less cluttered as well:
const char* compare(int choice1, int choice2)
{
if (choice1 == choice2)
{
return "Try again...";
}
else
{
// Use if and/or switch statements to do the rest of the logic
switch (choice1)
{
case STONE: ... ; break;
case PAPER: ... ; break;
case SCISSORS: ...; break;
}
}
}
这篇关于如何在程序中更正此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!