Write your question here.
So I am working on a short Survival based game as part of a project and I am practically finished, but this line of code is registering the AND condition where I need the "sosMade" to be registered as well as individual stats but it is only looking at the OR statement in the first line.
if(sosMade=true && (sanity<25 || hunger<25 || health<25))
{
cout<<"On the horizon early in the morn, you see a ship heading towards your island, you are saved!"<<endl;
cout<<"You arrive on board and are immediately put into the ER, but your physical and mental health are in disarray and greatly decline. You pass away on board."<<endl;
cout<<"YOU FAILED."<<endl;
return(0);
}
if(sosMade=true && sanity>=25 && hunger>=25 && health>=25)
{
cout<<"On the horizon early in the morn, you see a ship heading towards your island, you are saved!"<<endl;
cout<<"You arrive and are rushed to the ER where they treat you. You make a full recovery."<<endl;
cout<<"YOU WIN."<<endl;
return(0);
}
elseif (sosMade==false)
{
cout<<"Help never arrives for you haven't attempted to establish a point of contact."<<endl;
cout<<"You make do for a while but eventually pass due to not having the necessary materials to survive"<<endl;
cout<<"YOU FAILED."<<endl;
return(0);
}
It's sosMade == true not sosMade = true = is for assignment and == is for comparing. So what is happening is that the if condition is assigning true to sosMade and then checking sosMade (which will always be true). That's why the OR condition is enough to execute the if block.
Gotta be careful while using ==. ;)
Also instead of writing sosMade==true &&.. you could just write sosMade &&.
Handy trick eh? And likewise instead of (sosMade==false use (!sosMade)