Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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";
}
}
Posted
Updated 26-May-16 14:00pm
v3
Comments
ZurdoDev 26-May-16 12:24pm    
Step through and debug it.
jeron1 26-May-16 12:29pm    
Have you seen strncpy() or memcpy()?
Philippe Mori 27-May-16 9:38am    
Make your code more readable by properly indenting it. If you don't make any effort, then you don't deserve help!

C++
char choice2[256];

...
 
if (compchoice<0.34)
{
choice2 ="SCISSORS" ; //l value required error

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.
 
Share this answer
 
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
C++
#define STONE    1
#define PAPER    2
#define SCISSORS 3

const char* compare(int, int); //prototype


C++
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


Then you can decide choice2 in this fashion
C++
choice2 = (int)ceil(compchoice * 3.0);


And the comparison function will be less cluttered as well:
C++
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;
        }
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-May-16 23:22pm    
Good point, a 5.
—SA
George Jonsson 27-May-16 2:08am    
Thanks Sergey.
Never liked to do string operations in C, so I avoid it if possible.
Sergey Alexandrovich Kryukov 27-May-16 8:53am    
I always say that null-terminated strings is the most devastating invention in the whole technology.
Their unsafe nature and poor performance should be obvious, and yet they survive and keep making trouble decade after decade...
—SA
George Jonsson 27-May-16 9:17am    
I tend to agree. I can't remember how many times I have fixed bugs caused by a one character buffer overrun, where the null character was forgotten.
What made things even worse was that in debug mode it usually worked, due to some extra byte allocation by Visual Studio, if I remember correctly.
Sergey Alexandrovich Kryukov 27-May-16 9:26am    
Exactly.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900