Output always 0
Jan 30, 2017 at 5:25am UTC
Hi,
When I enter 20 for weight_t and 10 for weight_r, I want each of them to divide by 100 which would give me a result of .20 and .10. However, it always outputs 0 not matter what number I put in and I cant figure out why.
Can anybody explain to me what is wrong with my program?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
using namespace std;
int main () {
float weight_t = 0, weight_r = 0, weight_l = 0, weight_a = 0;
cout << "Enter quiz weight percentage" << endl;
cin >> weight_t;
cout << "Enter design weight percentage" << endl;
cin >> weight_r;
cout << "Enter critique weight percentage" << endl;
cin >> weight_l;
cout << "Enter critique weight percentage" << endl;
cin >> weight_a;
while (weight_t != 0)
weight_t = weight_t/100;
while (weight_l != 0)
weight_l = weight_l/100;
while (weight_r != 0)
weight_r = weight_r/100;
while (weight_a != 0)
weight_a = weight_a/100;
cout << "weight_t is " << weight_t << endl;
cout << "weight_r is " << weight_r << endl;
return 0;
}
Jan 30, 2017 at 5:33am UTC
Change the
while statements to
if statement.
will always be
true , therefore it will continue to loop until
weight_t is 0.
Last edited on Jan 30, 2017 at 5:35am UTC
Jan 30, 2017 at 5:33am UTC
1 2
while (weight_t != 0)
weight_t = weight_t/100;
This loop will keep dividing weight_t by 100, until it reaches 0. Hence, why it will always output 0 for every number you input.
Topic archived. No new replies allowed.