While loop condition not working - c++

Hey basically i want both the player and the wolves to attack each other until one another are dead. But the while loop is infinite so obviously the condition is not met. But i cant see where i am going wrong with this if ( choice1 == 1) // if statement is used throughout the game to allow the user to interact through the game with choices.
while((Status.health != 0) && (Wolves.health != 0) )
{
int playerAttack = Status.strength + hitPoints() + Rock.attack;
cout<< "This is the player attack" << playerAttack;
Wolves.health = Wolves.health - playerAttack;
cout << "This is the wolves health" << Wolves.health;
if (Wolves.health <= 0)
{
cout << "\nThe wolves are dead\n ";
}
int wolfAttack = Wolves.attack + hitPoints();
Status.health - wolfAttack;
if(Status.health <= 0)
{
gameOver();
}// print out of object health.
}
Can anybody help ?

Compare:
Wolves.health = Wolves.health - playerAttack;
vs
Status.health - wolfAttack;
Notice any difference?

Well, i think the health was not exact 0 - because your condition looks only for != 0
it should be bigger than 0
while((Status.health > 0) && (Wolves.health > 0))
...
edit: also the missing = John Dibling found first

Are you sure that this is correct:
Status.health - wolfAttack;
This actually is a no-operation. Perhaps you meant:
Status.health -= wolfAttack;

in computer some of numbers cannot be represented for example 0.1, so if you calculate 1 - (0.1 * 10) its result not equal to zero. You checked only !=0 and ==0 condition. Try this:
=0 or <=0 also if your "health" variable is integer you give a error tolerance such as:
=(0.5) or <=(0.5) etc...

Most likely both values are never zero. That sounds likely, because you yourself use the <= condition.

I think your are getting negative values, I recomend to use
while((Status.health > 0) && (Wolves.health > 0) )

Related

C++/SDL2 Mixer - One ternary works correctly, but a nearly-identical one doesn't?

I'm writing some simple test audio code to make sure SDL_mixer works for my application, but in the code to change the volume when given certain characters from std::cin everything works except for subtracting 10 from the volume. Adding 10 works, subtracting 1 works, but subtracting 10 only subtracts 1 and not 10.
In the code below, the variable sound is a struct containing 2 maps called mus and eff, indexed by strings and containing Mix_Music* and Mix_Chunk variables respectively. "Moon Patrol" is my music audio and "Pew" is my test effect audio. Also, sorry about the big if/else statement, I'm going to be using GLUT keys for input in the actual program.
Relevant Code:
Mix_PlayMusic(sound.mus["Moon Patrol"], -1);
char i = ' ';
std::cout << "i / k: volume +/- 1 | u / j: volume +/- 10 | q: quit | v: pew sound" << std::endl;
while (i != 'q')
{
std::cin >> i;
int cVolume = Mix_VolumeMusic(-1);
if (i == 'i') Mix_VolumeMusic(cVolume + ((1) ? cVolume < 128 : 0)); // +1 Works
else if (i == 'k') Mix_VolumeMusic(cVolume - (1 ? cVolume > 0 : 0)); // -1 Works
else if (i == 'u') Mix_VolumeMusic(cVolume + (10 ? cVolume + 10 <= 128 : 0)); // +10 Works
else if (i == 'j') Mix_VolumeMusic(cVolume - (10 ? cVolume > 9 : 0)); // -10 Doesn't Work
else if (i == 'p') std::cout << cVolume << std::endl; // Showing Volume Works
else if (i == 'v') Mix_PlayChannel(-1, sound.eff["Pew"], 0); // Effect Works
} // Exiting works
Things I've tried that didn't work:
Replacing cVolume with Mix_VolumeMusic(-1) everywhere
Putting the 10 in parentheses
Changing the key from 'j' to 'f'
Things I've tried that worked as written (but not what I want):
Changing the 10 to a 1 (code subtracted 1 as told)
Commenting out the ternary, getting else if (i == 'j') Mix_VolumeMusic(cVolume - (10));// ? cVolume > 9 : 0)); (subtracts 10 correctly but doesn't protect from negative values)
This shows that it's something about the ternary, but the code for subtracting 1 and subtracting 10 is essentially identical. Is there something I'm missing?
The ternary operator works like this:
variable = (condition) ? expressionTrue : expressionFalse;

Why will my elseif statment never executed

Any idea why the else if statment will be never executed ? The value of difference is constantly changing when the program runs.
double difference = abs(reale_x[0] - reale_x[1]);
if (0 <= difference < 45) {
timer_counter += 1;
if (timer_counter == 30) {
cout << "CLICK" << '\n';
}
}
else if (difference > 50) {
timer_counter = 0;
}
That is not how comparation works in c++.
What this code
if (0 <= difference < 45) {
does is it first compares if 0 is smaller or equal to difference. It is then "replaced" by a bool value either true or false. And then a bool value (so either 1 or 0) is compared to 45. And it will always be smaller than 45. What you have there is an always true statement.
So the way you would write this if statement is
if (difference >= 0 && difference < 45){
Note that because of your else if statement it will not execute if the difference is >44 and <51
if (0 <= difference < 45) will be executed as if ((0 <= difference) < 45), which will be either 0<45 or 1<45 and will always be true. That's why the else part is not getting executed.
in mathematics, we see and write 0 <= x < 45 or something like that to define the range of the variable x. But in order to tell the computer the same thing, you have to tell more clearly. Saying, to have to tell the compiler, that the value of x is greater than or equal to zero and at the same time, that value will be less than 45, and you can tell the compiler by this statement: difference >= && difference < 45 . the && is an 'AND' operator in most of the languages.

How to deal with overlapping values in c++?

I'm currently trying to solve a programming problem that involves different ranges of values that overlap. The task is to accept input, in E-notation, and that is where the overlap of range inevitably occurs.
I have 2 ranges that overlap at 1E-11. 1E-11 and lower and 1E-11 and higher
The output would be 1E-11 is either x or it is y. Programmatically i would solve it like this:
(X_MIN would be 1E-11 and X_MAX 1E-8)
(Y_MAX would be 1E-11 and Y_MIN 1E-13)
(lengthOfRange <= X_MIN) && (lengthOfRange >= Y_MAX) ?
cout << "This value entered indicates that it is x\n" :
cout << "It is y";
Expressed this way if i input IE-11 it shows me "This value entered indicates ..." but will never show me it is y (understandably - overlap!)
The other way around would be expressing it this way:
(lengthOfRange <= X_MIN) && (lengthOfRange != Y_MAX) ?
cout << "This value entered indicates that it is x\n" :
cout << "It is y";
The output would always be "... It is y ..." (Same difference - overlap!) There is no other determining factor that would tell range is x or y coming in to play there as of right now.
...
if (lengthOfRange <= X_MIN) && (lengthOfRange == Y_MAX)
{
cout << "The input indicates that it could be either x or y\n";
}
...
Even if i were to solve the problem in a way such as defining the range with different values, would in the end lead to the very same problem. I COULD define MIN and MAX as constants in lengthOfFrequency, which is totally different, bit then i would have to say: lengthOfFrequency = 1E-11; and voila same problem once again. 1 input 2 ranges that are technically different, getting the same one and only correct value in E-notation.
Is there a way around this without involving to simply say input is either x || y? Which it is technically of course, and if it were to be solved physically there are ways of telling it apart that 1E-11 is not 1E-11 though it is. (I hope i make sense here). But, again, ... is there such way, and how would i go about writing it? (Not asking for code specifically though it would be highly welcome, just a pointer in the right direction.) Or should i rather go with saying input is either x || y?
Thanks in advance for any answer!
**Minimum Complete Code:**
#include <iostream>
using std::cout;
using std::cin;
int main()
{
/* Constants for ranges, min and max */
const double X_RAYS_MIN = 1E-13,
X_RAYS_MAX = 1E-11,
Y_RAYS_MIN = 1E-11,
Y_RAYS_MAX = 1E-8,
Z_RAYS_MIN = 1E-7,
Z_RAYS_MAX = 3.8E-7;
double lengthOfRange;
/* Test output and validation */
cout << "Enter value in scientifc notation: ";
cin >> lengthOfRange;
/* X_RAYS_MIN < is 1E-14, 1E-15, 1E-16 etc. > 1E-12, 1E-11 etc.. */
if (lengthOfRange >= X_RAYS_MIN && lengthOfRange <= X_RAYS_MAX)
{
cout << "X_RAYS\n";
}
else if (lengthOfRange >= Y_RAYS_MIN && lengthOfRange <= Y_RAYS_MAX)
{
cout << "Y_RAYS\n";
}
system("pause");
return 0;
}
Output is: 1E-10 is Y_RAYS, 1E-9 is Y_RAYS, 1E-11 X_RAYS, 1E-12 X_RAYS
Somehow i found the solution for my problem myself without going any roundabout ways ... By hovering over the 1E-13:
X_RAYS_MIN = 1E-13
VS showed me 1.(numberofzeros)3E-13, and guess what ... if instead the input for 1E-11 is 2E-11, the output for X_RAYS becomes Y_RAYS ... so the problem "magically" solved itself ... lucky me i guess ... :)

How could you write the "if - then" logic for the game Yacht?

https://en.wikipedia.org/wiki/Yacht_(dice_game)
I created 5 dice in my c++ program and they each roll random numbers from 1-6.
So if you get all 1's its really simple. It's just:
if (dice1 == 1 && dice2 == 1 && dice3 == 1 && dice4 == 1 && dice5 == 1)
{
int total = 50;
}
Also, summing all the dice is easy too. But how could you write the if-statement for "if two to four dice are the same then sum up those dice"? Is there a simple way you could do that?
Try to use tables and make variable which count 1 in table. Then u can compare it.

C++ infinite loop

I am attempting to write a loop that will repeat until the user enters one of the correct choices (either 1 or 0). For some reason when I have the loop written as below it creates an infinite loop.
I am intending for the loop to only execute while control is not 0 OR not 1, but for some reason it will always execute and becomes an infinite loop.
cout<<"Please enter 1 for another customer or 0 to quit : ";
cin>>control;
while ((control != 0 )|| (control != 1))
{
cout<<"Invalid Entry! Please enter a 1 to enter another customer or 0 to quit: ";
cin>>control;
}
I changed it to be while control less than 0 OR greater than 1, which works but I am still confused as to why the other loop is not working.
You have to use && operator.
while ((control != 0 ) && (control != 1))
(control != 0) || (control != 1)
is equivalent to,
!(control == 0 && control == 1)
but,
(control == 0 && control == 1)
is always false (there is no such number).
Therefore, the whole expression will always get true value.
The only way to break out
while ((control != 0 )|| (control != 1))
is
!(control != 0) && !(control != 1)
which is equivalent to
control == 0 && control == 1
which is impossible for all integers.