gpt4 book ai didi

c++ - 编写if语句时出现 “' =' : left operand must be l-value”错误

转载 作者:行者123 更新时间:2023-12-02 11:12:20 25 4
gpt4 key购买 nike

我必须编写此代码才能运行骰子游戏,这是我的第一个游戏程序,并且遇到了一些以前从未见过的错误,因此我已尽力解决了这些问题。因此,我剩下的唯一错误是:

'=' : left operand must be l-value

对于最后三个if语句。我知道这可能只是我问题的开始,但我是新手,因此任何建议都将不胜感激。
#include <iostream>
#include <ctime> //Include Header file for time functions
#include <cstdlib> //Include Header file for random number generators
#include "graph1.h"
using namespace std;
int main()
{
//Variable Declaration
int rv1 = 0; //Random number for the 1st dice
int rv2 = 0; //Random number for the 2nd dice
int seed=0;
int wager=0;

//Display graphics
displayGraphics();

cout<< "Wager";
cin>>wager;



//Initialize random number generator
srand(time(0));

//Generate two random numbers between 1 and 6 inclusive
rv1 = (rand()%6)+1; /* Function rand() generate a random number,
(rand()%6+1) generate a random number between 1&6 */
rv2 = (rand()%6)+1;

//rv1
if(rv1=1)
{cout<<displayBMP("1.bmp",200,150);}

else
{ if (rv1=2)
cout<<displayBMP("2.bmp",200,150);}

if (rv1=3)
{cout<<displayBMP("3.bmp",200,150);}


if (rv1=4)
{cout<<displayBMP("4.bmp",200,150);}

if (rv1=5)
{cout<<displayBMP("5.bmp",200,150);}

if (rv1=6)
{cout<<displayBMP("6.bmp",200,150);}
//rv2
if(rv2=1)
{cout<<displayBMP("1.bmp",200,250);}

if (rv2=2)
{cout<<displayBMP("2.bmp",200,250);}

if (rv2=3)
{cout<<displayBMP("3.bmp",200,250);}

if (rv2=4)
{cout<<displayBMP("4.bmp",200,250);}

if (rv2=5)
{cout<<displayBMP("5.bmp",200,250);}

if (rv2=6)
{cout<<displayBMP("6.bmp",200,250);}


if (wager<5)
{
cout<<displayBMP("smiley.bmp",250,100);
gout<<setPos(150,280)<<"Your wager of"<<wager<<"is less than required $5/";
gout<<setPos(150,295)<<"Please re-run program and enter larger amount!/n";
return-1;
}
if (((rv1+rv2)%2)=0)
{ gout<<setPos(200,280)<<"Dice total"<<(rv1+rv2)<<endg;
cout<<"You win $2 extra for even roll!/n";
cout<<"Your winnings:$2+Original Wager:$"<<wager<<"=$"<<wager+2<<endg;
}

if(rv1+rv2=7)
{gout<<setPos(200,280)<<"Dice total"<<(rv1+rv2)<<endg;
cout<<"Youre lucky today!/n";
cout<<"You double your wager/n";
cout<<"Your winnings:$10+Original Wager=$"<<wager<<"=$"<<wager*2<<endg;
}
if(((rv1+rv2)%2)=1)
{gout<<setPos(200,280)<<"Dice total"<<(rv1+rv2)<<endg;
cout<<"You lose $2 for odd roll!/n";
cout<<"Original Wager:$"<<wager<<"Losses:$2=$"<<wager-2<<endg;
}
return 0;
}

最佳答案

=用于C++中的变量分配。您需要使用==进行比较测试:

if (((rv1+rv2)%2)==0)

还要注意,if语句的条件是这样写的:
if(rv1=1)

由于 rv1=1返回的结果是 1,因此将始终为true。这就是为什么某些C++程序员编写如下条件的原因:
if(1==rv1)

这可以防止您意外忘记第二个 =,因为:
if(1=rv1)

是编译时错误。当然,这样的书写条件主要是风格问题。我个人不使用此方法,因为我认为该方法的阅读效果不佳。

关于c++ - 编写if语句时出现 “' =' : left operand must be l-value”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com