How to prevent int becoming less than 0 in while loop ?

Hello, why cant I prevent 'l' from becoming less than 0 in while loop ? It still does one final loop while 'l' is still more than 0, which then turns it to 'l<0'. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{
int v=53,sk=0,h,l=7500;
while(l>=0&&sk<=7)
{
    sk++;
    h=sk*sk;
    l-=h*v;
    cout << "L=" << l << endl;
}
return 0;
}
you can not unless you change
1
2
3
sk++;
h=sk*sk;
l-=h*v;

because it is math
but if you want to avoid showing l<0 ,simply you can do that:
if(l>=0)cout << "L=" << l << endl;
Hi,

Change line 10 to this:

l > (h * v) ? l -= (h * v) : break;

Might need another std::cout after the while loop.

You could do with some white-space around your operators, it's hard to read when it is so dense:

1
2
while( l >=0 && sk <=7 )
{


Declare and initialise all your variables 1 per line.

Use meaningful variable names, I don't know what any of them mean.

Good variable and function names can make the code read like a story telling us what is happening.

Don't have line 2, put std:: before each std thing, believe me it's the best way, and that is what all the experts do.

Good Luck !!

Last edited on
Thank you for your answer. I dont really know yet what
l > (h * v) ? l -= h * v : break;
does, but I tried replacing my old line with this one and im receiving syntax 'break' error. I feel like its similiar to :
1
2
3
4
5
if(l > h * v) 
{
l -= h * v
break;
}

Am I right ?

Also, this code's "story" is :
Elephant with weight of 7500kg(l) died. sk2 animals come each day to eat it. Every animal weights 53kg(v). If they cant finish eating it in 7 days, loop should stop and show how much elephant weights now. If they have finished eating it in less than 7 days, it should show how many days they were eating it.

I havent included days as output yet, because loop isnt working as expected anyway.
Last edited on
Nearly, this works:

1
2
3
4
5
if(l > h * v) {
       l -= h * v;
    } else {
     break;
    }


The ternary operator is a short cut for if then else.

The problem is not being able to incorporate the break

Sorry for not testing the code first :+)
The reason the break doesn't work in the ternary operator is that it doesn't set a value. The standard says that the expression after the : has to be an assignment expression.

There we go. :+)

ThingsLearnt++;
Last edited on
I'd change the code to loop through the days and break out of the loop if the elephant is consumed:
1
2
3
4
5
6
7
8
9
10
11
int elephantWeight = 7500;
int day;
for (day=1 day < 8; ++day) {
    int numAnimals = day*day;
    elephantWeight -= numAnimals * 53;
    if (elephantWeight <= 0) {
        cout << "after " << day << " days the corpse is consumed.\n";
        break;
    }
    cout << "after " << day << " days the corpse weighs " << elephantWeight << ".\n";
}

Hi, I have used for loop before and I succeeded and now im trying with while loop, but I keep jumping into -meat.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    int weight = 7500 ;
    int day_number = 1 ;
    const int multiplier = 53 ;
    const int max_days = 7 ;

    while( weight >= 0 && day_number < (max_days+1) )
    {
        weight -= day_number * day_number * multiplier ;
        ++day_number ;
    }

    if( weight <= 0 ) std::cout << "finished eating in " << day_number << " days.\n" ;
    else std::cout << weight << " kilograms left after " << max_days << " days.\n" ;
}
Topic archived. No new replies allowed.