looped in loop
Oct 30, 2014 at 1:34pm UTC
i have the following program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int a,b=100;
do
{
cin>>a;
cout<<endl;
b=b-a;
cout<<b;
}
while (b>=0);
{
cout<<"0" ;
}
}
but my problem is that i am trying to get output as "0" as i get my
b
zero or less than it.........
thanks in advance....
progRammer007
Oct 30, 2014 at 1:56pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
using namespace std;
int main() {
int a,b=100;
do
{
cin>>a;
b=b-a;
cout << b << endl;
} while (b>0); // Replaced b>=0 with b>0
cout<<"0" ;
}
That should do what you want.
Last edited on Oct 30, 2014 at 1:56pm UTC
Oct 31, 2014 at 1:28am UTC
this isn't solving my problem.......
what i really want that it should not show negative integers like -15 ......(like happens in a game when someone dies his hp is shown as 0 not a negative............)
Oct 31, 2014 at 3:09am UTC
You need to check it before you decrement it.
1 2 3 4
if (b-a<0)
b=0;
else
b-=a;
Topic archived. No new replies allowed.