Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
So I'm new to coding and I'm doing this for a class project. How do I make it where the while loop stops if it's <= to 0. If I put <= to 0 it doesn't do the loop at all and if I put it !=
it works, but it sometimes goes negative resulting in the loop never stopping. Is there something I'm missing? I have no idea why <= 0 wouldn't work
int main()
{
while (health != 0 && enemy_health != 0) {
std::cout << "Player " " Health: " << health << " Level: " << level << "\n";
std::cout << "Inventory " << "Gold: " << gold << " Keys: " << keys << " Health Potions: " << health_potions << "\n\n";
std::cout << "Fist: " << fist_damage << " damage " << " Torch: " << torch_damage << " damage \n" << " Status Effects:\n\n" <<" Burn: " << burn_damage << " damage\n\n";
std::cout << "Use: ";
std::cin >> input;
if (input == Fist) {
std::cout << "\nYou did " << fist_damage << " damage\n\n";
std::cout << "Monsters Health: " << enemy_health << "\n";
playerAttack = true;
}
else if (input == Torch) {
std::cout << "\nYou did " << torch_damage << " damage\n" << "You did " << burn_damage << " burn damage\n\n";
int total = enemy_health - (torch_damage + burn_damage);
enemy_health = total;
std::cout << "Monsters Health: " << enemy_health;
torch_durability--;
playerAttack = true;
isBurning = true;
}
while (playerAttack != true) {
std::cout << "\n\nPlease enter a valid answer\n\n";
std::cout << "Use: ";
std::cin >> input;
if (input == Fist) {
std::cout << "\nYou did " << fist_damage << " damage\n\n";
std::cout << "Monsters Health: " << enemy_health << "\n";
playerAttack = true;
}
else if (input == Torch) {
std::cout << "\nYou did " << torch_damage << " damage\n" << "You did " << burn_damage << " burn damage\n\n";
int total = enemy_health - (torch_damage + burn_damage);
enemy_health = total;
std::cout << "Monsters Health: " << enemy_health;
torch_durability--;
playerAttack = true;
isBurning = true;
}
}
if (playerAttack == true) {
std::cout << "\n\nThe enemy attacks\n\n";
}
if (dodge_chance == 5) {
std::cout << "\n\nYou dodged the enemies attack\n\n";
}
else if (dodge_chance != 5) {
std::cout << "The enemy landing a crushing blow\n\n";
playerHit = enemy1_damage;
int health_real = health - playerHit;
health = health_real;
std::cout << "The enemy does " << playerHit << " damage\n\n";
}
}
}
When asking questions or just for debugging, it is a good idea to isolate the part that is giving you trouble. Most of the code you have posted is irrelevant and makes it harder to read and test.
From what I understand, you have tried putting
while (health <= 0 && enemy_health <= 0)
instead of your current loop ?
If so, then it is simply a logic error. You are asking the code to loop while player health is negative or zero. Simply changing to
while (health > 0 && enemy_health > 0)
would work.
Related
In the below code, I've got some bugs and I don't know why it doesn't work.
I used a fmod to do the decimals but I got some bugs in the part where it gives you the 0.10 and 0.05 $ and most of the time I never get the good amount that I'm supposed to give back if there is.
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
int iAmount_due = { 0 };
int iGiven_money = { 0 };
int iMoney_back;
iMoney_back = iGiven_money - iAmount_due;
cout << "Enter the amount due please: " << endl;
cin >> iAmount_due;
cout << "Enter the amount given please: " << endl;
cin >> iGiven_money;
if (iGiven_money >= iAmount_due) {
iMoney_back = iGiven_money - iAmount_due;
cout << "We will give you : " << iMoney_back << " $ back" << endl;
}
else {
cout << "No money back" << endl;
}
if (iMoney_back >= 100) {
cout << "You will receive: " << iMoney_back % 100 << " x 100$" << endl;
}
else {
cout << "No 100$ bill" << endl;
}
if (iMoney_back >= 50) {
cout << "You will receive: " << iMoney_back % 50 << " x 50$" << endl;
}
else {
cout << "No 50$ bill" << endl;
}
if (iMoney_back >= 20) {
cout << "You will receive: " << iMoney_back % 20 << " x 20$" << endl;
}
else {
cout << "No 20$ bill" << endl;
}
if (iMoney_back >= 10) {
cout << "You will receive: " << iMoney_back % 10 << " x 10$" << endl;
}
else {
cout << "No 10$ bill" << endl;
}
if (iMoney_back >= 5) {
cout << "You will receive: " << iMoney_back % 5 << " x 5$" << endl;
}
else {
cout << "No 5$ bill" << endl;
}
if (iMoney_back >= 0.25) {
cout << "You will receive: " << fmod (iMoney_back, 0.25) << " x 0.25$ " << endl;
}
else {
cout << "No 0.25$ " << endl;
}
if (iMoney_back >= 0.10) {
cout << "You will receive: " << fmod (iMoney_back, 0.10) << " x 0.10$ " << endl;
}
else {
cout << "No 0.10$ " << endl;
}
if (iMoney_back >= 0.05) {
cout << "You will receive: " << fmod (iMoney_back, 0.05) << " x 0.05$ " << endl;
}
else {
cout << "No 0.05$ " << endl;
}
return 0;
}
int holds only integer values. 0.05 is not an integer value. You might be tempted to use double values instead that probably work for your simple problem; however, they generally have rounding errors which is something you really do not want to have with currency values.
The better solution will be to still use integers. You would need to define 1 as the smallest possible unit (e.g. 1 cent), meaning that all your values will be in cent. You would need to write extra code to be able to enter and display values as dollars instead of cents.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to set the different times to equal each other so that I can continue with the else if statement. But for some reason, it seems to not want to do it. Not only that, it seems to only recognize the first "TIE" that I have, even though it should be recognizing the other ones as well. What am I doing wrong?
if (time1 < time2 && time3)
{
cout << "\nCongratualations " << racer1 << "!!! " << "You are the winner!!" << endl;
cout << "\n***** Your winning time is: " << time1 << " *****" << endl;
}
else if (time2 < time1 && time3)
{
cout << "\nCongratualations " << racer2 << "!!! " << "You are the winner!!" << endl;
cout << "\n***** Your winning time is: " << time2 << " *****" << endl;
}
else if (time3 < time1 && time2)
{
cout << "\nCongratualations " << racer3 << "!!! " << "You are the winner!!" << endl;
cout << "\n***** Your winning time is: " << time3 << " *****" << endl;
}
else if ((time1 == time2) < time3)
{
cout << "\nWe have a TIE " << racer1 << " and " << racer2 << " win!!" << endl;
cout << "\n***** Your winning time is: " << time1 << " *****" << endl;
}
else if ((time2 == time3) < time1)
{
cout << "\nWe have a TIE " << racer2 << " and " << racer3 << " win!!" << endl;
cout << "\n***** Your winning time is: " << time2 << " *****" << endl;
}
else if ((time3 == time1) < time2)
{
cout << "\nWe have a TIE " << racer1 << " and " << racer3 << " win!!" << endl;
cout << "\n***** Your winning time is: " << time3 << " *****" << endl;
}
if (time1 == (time2 == time3))
{
cout << "\nWe have a 3 way TIE!! No winner for this Race..." << endl;
cout << "\n***** Your winning time is: " << time1 << " *****" << endl;
}
This: (time1 < time2 && time3) means time1 is greater than time2 and time3 is true (non-zero).
What you probably meant was: (time1 < time2 && time1 < time3)
This: (time1 == (time2 == time3)) means time1 is equal to true (1) if time2 is equal to time3, otherwise time1 is equal to false (0).
What you probably meant was (time1 == time2 && time1 == time3).
All of your other comparisons have similar problems. Each binary comparison results in a simple bool. You can't join up comparisons the way you do in speech.
I have this code that I have been working on for fun, it is as basic as it gets, becuase I am a beginner, and it works fine, but I can't seem to be able to figure out how to make it show the least and the most amount of pancakes ate. Thank you a lot in advance.
#include <iostream>
using namespace std;
int main(){
int pancakes[10];
int x,i;
cout << "Hello user!" << endl;
cout << endl;
cout << "Please enter how many pancakes did each of the 10 people eat:" << endl;
cout << endl;
for (i=0;i<10;i++ ){
cin >> x;
pancakes[i]=x;
}
cout << "1st person ate" << " " << pancakes[0] << " " << "pancakes" << endl;
cout << "2nd person ate" << " " << pancakes[1] << " " << "pancakes" << endl;
cout << "3rd person ate" << " " << pancakes[2] << " " << "pancakes" << endl;
cout << "4th person ate" << " " << pancakes[3] << " " << "pancakes" << endl;
cout << "5th person ate" << " " << pancakes[4] << " " << "pancakes" << endl;
cout << "6th person ate" << " " << pancakes[5] << " " << "pancakes" << endl;
cout << "7th person ate" << " " << pancakes[6] << " " << "pancakes" << endl;
cout << "8th person ate" << " " << pancakes[7] << " " << "pancakes" << endl;
cout << "9th person ate" << " " << pancakes[8] << " " << "pancakes" << endl;
cout << "10th person ate" << " " << pancakes[9] << " " << "pancakes" << endl;
return 0;
}
Since you are a beginner, I will put a simple solution using a loop.
int max = 0;
for(i = 0; i < 10; i++) {
if(pancakes[i] > max) max = pancakes[i];
}
cout << "Most amount of pancakes eaten by a single person: " << max << endl;
You can use min_element and max_element from the standard library to do this:
#include <algorithm>
cout << "The smallest number of pancakes was " << *min_element(pancakes, pancakes + 10) << endl;
cout << "The largest number of pancakes was " << *max_element(pancakes, pancakes + 10) << endl;
Firstly, instead of having around 10 cout's, you can use a loop to print them :
for (i=0;i<10;i++ ){
if(i==0)
cout <<i+1<< "st person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==1)
cout << i+1<<"nd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==2)
cout << i+1<<"rd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else
cout <<i+1<< "th person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
}
Secondly, you can directly enter values into your array, no need for an intermediate variable x:
for (i=0;i<10;i++ ){
cin >> pancakes[i];
}
For your max and min problem, take two variables, say - max and min. Initialise them to any arbitrary smallest (say 0, if you are not dealing with negative numbers) and largest value (say, INT_MAX) respectively. Alternatively, you can initialise them to the first element of your array.
For finding max and min, you can traverse the entire array, while checking if the elements are greater or lesser than your max and min variables. If they are, then assign them to your variables:
for (i=0;i<10;i++ ){
if(pancakes[i]>max)
max = pancakes[i];
if(pancakes[i]<min)
min = pancakes[i];
}
You can add std::cout inside for loop like below,
for (int i = 0; i < 10; i++ )
{
std::cin >> pancakes[i];
std::cout << i+1 <<"st person ate" << " " << pancakes[i] << " " << "pancakes" << std::endl;
}
int maxpancakes = pancakes[0];
int minpancakes = pancakes[0];
for(int i = 0; i < pancakes.length(); i++ )
{
if( pancakes[i] < minpancakes )
minpancakes = pancakes[i];
if( pancakes[i] > maxpancakes )
maxpancakes = pancakes[i];
}
std::cout << "The smallest pan cake had is :" << minpancakes << std::endl;
std::cout << "The max pan cake had is :" << maxpancakes << std::endl;
Well basically the condition for healthtwo causes the program to stop but not healthone for some reason
complete code: http://en.textsave.org/CmN
if (chance<=rando) {
cout << " " << endl;
cout << "Hit! Dealing " << attackp << " Damage!" << endl;
healthtwo=healthtwo-attackp;
}
else {
cout << " " << endl;
cout << "Miss!" << endl;
}
chance=1+rand()%23;
if (chance<=rando) {
cout << "Comp Used " << comattackname << "!" << " Hit!" << " Dealing " << attackcp << " Damage" << endl;
cout << " " << endl;
healthone=healthone-attackcp;
}
else {
cout << "Comp Used " << comattackname << "!" << " Miss!" << endl;
cout << " " << endl;
}
} while (healthone>=0 || healthtwo>=0);
That should be a logical and (&&). After all, you want to check whether the health of both contestants is greater or equal to zero.
I need to use -1 to terminate but still display the summary.
Every time that I've tried and have gotten it to terminate the program, it doesn't go ahead and display the summary.
There are up to 10 trials, if you don't have enough information for 10 and you want to stop at 8, you type -1 and it goes to the summary and then terminates the program
while(i<10)
{
do
{
cout << "Enter result """ << i+1 << """ (or -1 if no more results): ";
cin >> score[i];
if(score[i] >=0 && score[i] <=49)
{
cout << "Grade " << "U" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=50&& score[i] <=59)
{
cout << "Grade " << "P" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=60 && score[i] <=69)
{
cout << "Grade " << "C" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=70 && score[i] <=89)
{
cout << "Grade " << "B" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=90 && score[i] <=100)
{
cout << "Grade " << "A" << " will be assigned to this result\n";
bool test=true;
i++;
}
else
{
test=false;
cout << "Invalid Input!\n";
}
}
while(test);
}
cout << "\nSummary of the results:\n";
for(int a=0;a< 10;a++)
{
std::cout << std::fixed << std::setprecision(2) << "Result " << a+1 << " " << score[a] << " Grade " << determine_grade(score[a]) << "\n";
}
cout << "\nThe average of the results = " << calc_average(score) << "\n";
cout << "The lowest of the results = " << find_lowest(score) << "\n";
cout << "The highest of the results = " << find_highest(score) << "\n";
system("Pause");
You don't want two loops, only one. You need to combine your two conditions into one i<10 && test.
Also you have declared your test variable in the wrong places. You should declare it once at the beginning of your loop.
bool test = true;
while(i<10 && test)
{
cout << "Enter result """ << i+1 << """ (or -1 if no more results): ";
if(score[i] >=0 && score[i] <=49)
{
cout << "Grade " << "U" << " will be assigned to this result\n";
i++;
}
...
else
{
test=false;
cout << "Invalid Input!\n";
}
}
Try to use break; when -1 is inputted inside the while loop. Also, you can use 1 loop, instead of two as john mentioned above.
Another thing to look for is your last for loop, it goes from 0 to 9, but in case someone used -1 and only inputted 3 grades, there might be odd values for the solutions.